Hui Ting Ko Hui Ting Ko - 7 months ago 10
Java Question

How can I take out the element from a String?

For example, if I have a string "Hello how are you". After I use the

split()
to separate all the elements, I want to put "Hello" to a variable, also same as the remainings, which method should I use to do it as it is not an ArrayList so I can't use the index.
Lots of thanks.

Answer

You can achieve the same like this :-

String input = "Hello how are you";
String[] splittedString = input.split(" ");
System.out.println(splittedString[0]);