V
V
Volodar Znatsky2016-07-20 19:25:16
Java
Volodar Znatsky, 2016-07-20 19:25:16

How to get the first and second word into variables from a String?

I can't find a solution at all. I recognize the string with the scanner, let's say "Hello, huge world!", To the str1 variable. You need to get the first word from the str1 variable into the str2 variable (str2 = "Hello"), and then the second word into the str3 variable (str3 = "huge")
PS I could split the entire string into words into an array and get both the first and second value, but in my case, where there can be 50 words in a variable, I don’t want to do this

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
exenza, 2016-07-21
@ja8mraz

yes, with StringTokeizer it's kind of simple:

StringTokenizer tokenizer = new StringTokenizer(inputSentence);
String firstArg = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : "";
String secondArg = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : "";

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question