Results 1 to 6 of 6
  1. #1
    javanew is offline Member
    Join Date
    Mar 2010
    Posts
    49
    Rep Power
    0

    Question Please help me with Split method> String

    HI , all:confused::confused:
    please help me,,
    i want to know how does split()method in String class works..

    Coz i want to create a new split method that returns an array of strings with delimiters,

    for example ("ab$sc$11$" , "$") >>returns an array that contains
    ab,$,sc,#,11,$

    please help with code ,,or with the alogarithm at least,,
    I dont know how to start !!!
    please Help !!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,523
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by javanew View Post
    HI , all:confused::confused:
    please help me,,
    i want to know how does split()method in String class works..

    Coz i want to create a new split method that returns an array of strings with delimiters,

    for example ("ab$sc$11$" , "$") >>returns an array that contains
    ab,$,sc,#,11,$

    please help with code ,,or with the alogarithm at least,,
    I dont know how to start !!!
    please Help !!
    If that delimeter is always just a single character have a look at the two argument split( ... ) method. You still have to insert those delimeters in the resulting array yourself though. Pay special attention when that delimeter occurs at the end of the original String. The two argument split( ... ) method can tell you if that is so.

    kind regards,

    Jos

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Simply I think you can count the number of element in return array and as Jos says pay attention on delimiters at the end of the original string.

  4. #4
    javanew is offline Member
    Join Date
    Mar 2010
    Posts
    49
    Rep Power
    0

    Default

    Can u check please ?? am i on the right direction ??

    String string = "John-is-very-angry";
    String []arr;
    String s;
    int j;
    int x;
    int i =0;
    int j = 0;
    while(i < string.length()){
    if((string.indexOf('-', i ) != -1)){
    x = string.indexOf('-', i );
    s = string.substring(i, x);
    i = x;
    System.out.println(s);
    i++;}

    }

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You define variable j twice in wrong way.

    Java Code:
    int j;
    int x;
    int i =0;
    int j = 0;
    It should be,

    Java Code:
    int j = 0;
    int x;
    int i =0;

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you run your code? If so what happen?

    Java Code:
    while (i < string.length()) {
    In the above line of code you compare the length of the string, that's the number of characters. And see how many time you increase the variable i. Only if you find - sign int the string you've. How many, 4. So what happen, after that the i is not increase. So that,

    Java Code:
    while (i < string.length()) {
    never evaluate as false. So that there is no way to exist the loop. You comes into a infinite loop.

Similar Threads

  1. reverse string split
    By Fittersman in forum Advanced Java
    Replies: 4
    Last Post: 03-09-2010, 12:29 AM
  2. string split
    By gisler in forum New To Java
    Replies: 6
    Last Post: 12-17-2009, 02:23 PM
  3. split method question
    By Chasingxsuns in forum New To Java
    Replies: 3
    Last Post: 11-19-2009, 07:19 PM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •