1

I have two Strings

One is

String s1 = "I have 1000 dollars";

Second is

String s2 = "I want my pet";

I need to ONLY get "have" , "1000", "dollars" in s1. Similarly, I need to ONLY get "want", "my" and "pet" in s2.

I know how to get the "I" using the code

String newS1 = s.substring(0, s.indexOf(" "));

Is there a way I can achieve this using substring?

jkclaro
  • 13
  • 1
  • 1
  • 4

3 Answers3

4

If you always want to use second ,third ,and fourth words in any strings you have, I would recommend you to use split functions.

Code:

    String s1 = "I have 1000 dollars";
    String[] sp = s1.split(" ");
    System.out.println("second word is " + sp[1]);
    System.out.println("third word is " + sp[2]);
    System.out.println("Fourth words is " +sp[3]);

Output:

second word is have
third word is 1000
fourth word is dollars
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
1

You could try the below rgex to get the second, third, fourth words.

^\\S+\\s*(\\S+)\\s*(\\S+)\\s*(\\S+).*$

DEMO

Group index 1 contains the first word, index 2 contains the 2nd word and index 3 contains the third word.

Pattern regex = Pattern.compile("^\\S+\\s*(\\S+)\\s*(\\S+)\\s*(\\S+).*$");
 Matcher matcher = regex.matcher("I have 1000 dollars");
 while(matcher.find()){
        System.out.println(matcher.group(1));
        System.out.println(matcher.group(2));
        System.out.println(matcher.group(3));

}

Output:

have
1000
dollars
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
1
String trimFirstWord(String s) {
    return s.contains(" ") ? s.substring(s.indexOf(' ')).trim() : "";
}
allenwoot
  • 891
  • 1
  • 8
  • 16
  • he wants second and third words – Kick Buttowski Aug 31 '14 at 04:05
  • 1
    This returns all but the first word - once the quotes on s.contains() are corrected - it doesn't support a character for input. Also, the trim() isn't really necessary - change `s.indexOf(' ')` to `s.indexOf(' ') + 1` to drop the space after the first word but keep the rest of leading and trailing spaces. – ash Aug 31 '14 at 04:15
  • its fine. I can work with this. if I can remove the first word then I'd be able to use the wordRemovedFirstSpace and use that again. – jkclaro Aug 31 '14 at 04:15
  • it does not even run?!!!!!! Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.lang.String.contains – Kick Buttowski Aug 31 '14 at 04:16
  • @foooree it is not right to accept the answer that even does not run. It will be misleading for other who check your question. I do not want you to accept my answer but at least accept the right answer not something that does not run ?!!!! – Kick Buttowski Aug 31 '14 at 04:20
  • I've changed the contains to accept a space string instead of a char. I used trim for the case where there is more than one space between the first and second word. Using indexOf(' ') + 1 can throw an index out of bounds exception if the string is just a space. Also, the question seemed to ask to for a trim first word method, not just just second and third. – allenwoot Aug 31 '14 at 04:52
  • 1
    This is what worked for me String removeFirstSpace = s.contains(" ") ? s.substring(s.indexOf(" ")).trim() : ""; – jkclaro Aug 31 '14 at 04:54