Results 1 to 7 of 7
  1. #1
    mortoneff is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Question substring problem

    Hello. I am really new to Java and i wonder if anyone can help me to the following:

    I have an array of Strings that each line has different length.
    Is it possible to save the substring of a specific index to the last character of each string? I mean that i want to keep the string from the character at index 5 until the last character which sometimes can be at index 9 or sometimes can be at index 10 or 11...

    Is there a correct way of doing the following:

    Java Code:
    for ( int i = 0; i < myArray.length; i++ ) {
        out.write(myArray[i].substring(5,myArray[i].charAt(myArray[i].length()-1)));
    
    
    }
    it throws Error: String index out of range...

    Any help would be great. Thanks!

  2. #2
    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

    Yes it's possible. But have to care of the original string length.

    When you are working on sub-string, you have to define start index and end index. End index is 1 less than length of the string. Start index is the one you want to change. Ok, here you get an index out of range error , means one of the index is not found. Seems to me that's the start index. So you have to validate the both index(actually the start index) before get the substring. Did you do that?

    Debug the code and check the both index when you comes with the error, with the relevant string.

  3. #3
    DiamondDog is offline Member
    Join Date
    Aug 2009
    Posts
    18
    Rep Power
    0

    Default

    It's possible to pass the substring method just one parameter.

    substring(5), for example, returns the substring starting at the character with index = 5, to the end of the string.

    If the string contains less than 6 characters (ie. it has no character with index =5) it returns an empty string.

    Example:

    Java Code:
    String[] nations = new String[]{"Italy","England","Portugal","Argentina","Australia","Cambodia","United States"};
    
            for(int i = 0; i<nations.length; i++)
            {
                    String out = nations[i].substring(5);
    
                    System.out.println(out);
            }
    Gives the following output:

    (Empty string, because 'Italy' only has 5 characters)
    nd
    gal
    tina
    alia
    dia
    d States
    Last edited by DiamondDog; 08-18-2009 at 03:16 PM.

  4. #4
    mortoneff is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    Thank you so much! This is exactly what i wanted. I was looking for such an example, but i couldn't manage to find one. Really appreciate your help.
    Cheers!

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    804
    Rep Power
    6

    Default

    The above code will be failed if the array elemnt length is < 5.

    Do this check for not throwing execption

    String[] nations = new String[]{"Ita","England","Portugal","Argentina","Australia ","Cambodia","United States"};

    for(int i = 0; i<nations.length; i++)
    {
    if(nations[i].length() >= 5 )
    {
    String out = nations[i].substring(5);
    System.out.println(out);
    }//if
    }//for
    Last edited by RamyaSivakanth; 08-19-2009 at 11:42 AM.
    Ramya:cool:

  6. #6
    DiamondDog is offline Member
    Join Date
    Aug 2009
    Posts
    18
    Rep Power
    0

    Default

    Thanks, Ramya.

    That's useful. I'm a learner, too! :)

    Many Thanks for your help.

  7. #7
    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

    Quote Originally Posted by RamyaSivakanth View Post
    The above code will be failed if the array elemnt length is < 5.
    :rolleyes: I think OP comes with the quite similar issue on his first post.

    Anyway, sharing thoughts among all our members in this way is really nice to see lol. Keep it up lols.

Similar Threads

  1. differens between regex and substring
    By aruna1 in forum New To Java
    Replies: 12
    Last Post: 04-11-2009, 05:08 PM
  2. Help with substring
    By hiklior in forum New To Java
    Replies: 12
    Last Post: 06-28-2008, 06:48 AM
  3. How can i get substring
    By 82rathi.angara in forum New To Java
    Replies: 14
    Last Post: 06-27-2008, 04:35 AM
  4. How to use subString method
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:44 PM
  5. substring
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:15 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
  •