Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8250105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:46:37+00:00 2026-06-07T23:46:37+00:00

I have a string as str=123|apple|apple|apple|dog$ i like to seperate string using | character.

  • 0

I have a string as

str=123|apple|apple|apple|dog$

i like to seperate string using “|” character.

    String str = "123|apple|apple|apple|dog$";
    StringTokenizer strString = new StringTokenizer(str..nextToken(), "|");
    int i;
    while (strString.hasMoreElements()) {
        if (i == 0)
            str1 = Loginstring.nextToken();
        else if (i == 1)
            str2 = Integer.Loginstring.nextToken();
        else if (i == 2) {
            str3 = Loginstring.nextToken();
            break;
        }
        i = i + 1;
    }

but it exit on first loop onwards(that means it exit from loop if i more than 1),, really i dont know y its happens, whats wrong with it

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T23:46:38+00:00Added an answer on June 7, 2026 at 11:46 pm

    this is what you are trying to achieve

    String str1 = "", str2 = "", str3 = "", str4 = "", str5 = "";
    String str = "123|apple|apple|apple|dog$";
    StringTokenizer strString = new StringTokenizer(str, "|");
    int k = 0;
    while (strString.hasMoreElements()) {
        if (k == 0)
            str1 = strString.nextToken();
        else if (k == 1)
            str2 = strString.nextToken();
        else if (k == 2) {
            str3 = strString.nextToken();
        } else if (k == 3) {
            str4 = strString.nextToken();
        } else if (k == 4) {
            str5 = strString.nextToken();
            break;
        }
        k = k + 1;
    }
    System.out.println(str1);
    System.out.println(str2);
    System.out.println(str3);
    System.out.println(str4);
    System.out.println(str5);
    

    but this can be easily done in one statement by

    String[] tokens = str.split("\\|"); // this is that single statment
    for (String token : tokens)
        System.out.println(token);
    
    str1 = tokens[0];
    str2 = tokens[1];
    str3 = tokens[2];
    str4 = tokens[3];
    str5 = tokens[4];
    
    System.out.println(str1);
    System.out.println(str2);
    System.out.println(str3);
    System.out.println(str4);
    System.out.println(str5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like: string str = https://abce/MyTest; I want to check if
I have a string like the following: String str = 4*5; Now I have
Supposed I have the following string: string str = <tag>text</tag>; And I would like
I have the following function: public static byte[] StringToByte(string str) { int length =
I have a string str[] where str[0]=My-Name; str[1]=is-drew; I want to get a new
I have something like: NSString *str = hello this is 123.; I want it
suppose I have a string: QString str={time:123,age:{aaa:123,bbb:456},address:aaa,hight:{zzz:111,ccc:{rrr:333,uuu:555},xxx:222}}; is it possible using regular expression to
Assuming I have a string $str = "abc*efg*hij*"; and an array $arr = array("123","456","789");
I have a string str = xyz\123 and I want to print it as
I have a string: String str = a + b - c * d

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.