0

I have a string :

String str = "sces123 4096 May 27 16:22 sces123 abc";

I want to get sces123 abc from the string. My code is :

String[] line = str.split("\\s+");
String name = str.substring(str.indexOf(line[5]));

It returns the whole string.

Dont know how to do.

any help appreciated!

2
  • 1
    This happens because sces123 is not only the fifth term of the string, but also the first. Commented May 27, 2015 at 12:06
  • 1
    What is the rule here ? I do not understand the logic. Commented May 27, 2015 at 12:08

6 Answers 6

1

Your code should be

String[] line = str.split("\\s+");
String name = str.substring(str.lastIndexOf(line[5]));

because str.lastindexOf(line[5]) returns 0 and then the substring returns the whole String.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried it too. I want to get all string after 16:22 .(this is the time) The code you write will not work if string after time is sces123 sces123
0

In your case you just need to change str.indexOf -> str.lastIndexOf.

Comments

0

This is one easy solution :

String str = "sces123 4096 May 27 16:22 sces123 abc";
//split spaces
String[] line = str.split(" ");
//get 2 last columns
String name = (line[5] + " " + line[6]);
System.out.println(name);

Comments

0

As Glorfindel said in the comment sces123 which is the content of if line[5] also contain as the first substring in the main String str. That why you are getting the full string.

Whats really happening here is:

indexOf( line[ 5 ]) --> returning 0
str.substring(0) -->  returning substring form 0 to last which is the main string

If you are only doing the hard codded things then i don't see the purpose of you here.

But What you want you get in this way (if it serve your purpose ) :

String name = str.substring( str.indexOf( line[ 5 ]+" "+line[6] ) );

Comments

0

Try This:

        String str = "sces123 4096 May 27 16:22 sces123 abc";
        String[] line = str.split("\\s+");

        System.out.println(str.substring(str.lastIndexOf(line[5])));

1 Comment

I did not check the answers, my mistake. I was trying on my eclipse. this code is working fine.
0

You could use a Matcher to find the end of the 5th match:

String str = "sces123 4096 May 27 16:22 sces123 abc";
Pattern p = Pattern.compile("\\s+");
Matcher m = p.matcher(str);

for (int i = 0; i < 5; i++) {
    m.find();
}

String name = str.substring(m.end());

In my opinion this is better than using lastIndexOf on to concatenating elements at indices 5 and 6, for the following reasons:

  • It does not require line[5] to be the last occurence of that string.

    Using lastIndexOf doesn't work for input

    "sces123 4096 May 27 16:22 sces123 sces123"
    
  • It also works for seperator strings of arbirtrary length.

    Using line[ 5 ]+" "+line[6] doesn't work for input

    "sces123 4096 May 27 16:22 sces123             abc"
    
  • It does not require the number elements after the split to be 7.

    Using line[ 5 ]+" "+line[6] doesn't work for input

    "sces123 4096 May 27 16:22 sces123 abc def"
    

1 Comment

He have a standar format he didnt say for another one to make your code like this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.