How can I do this in Java or Android like:
String name = "Jhon Doe";
textView.setText("Hi " + name);
// I want only "Jhon" not "Doe"
I want only first name ("Jhon") not last name ("Doe")
How can I do this in Java or Android like:
String name = "Jhon Doe";
textView.setText("Hi " + name);
// I want only "Jhon" not "Doe"
I want only first name ("Jhon") not last name ("Doe")
Just use the java .split() function. It gives an array, with parts of the name, split by a space.
String name = "Jhon Doe";
String[] parts = string.split(" ");
textView.setText("Hi " + parts[0]);
Use can try below code
String name = "Jhon Doe";
String[] separated = currentString.split("\\s+");
String name = separated[0]; // this will contain "Jhon"
You can use Java.String.split()
String string = "Jhon Doe";
String[] parts = string.split(" ");
String name = parts[0]; // Jhon
String surname = parts[1]; // Doe
textView.setText("Hi " + name);
Easiest way to do it.
String name = "Jhon Doe";
textView.setText("Hi " + name.split(" ")[0]);
If you prefer regex why not
String firstName = "John Doe".replaceFirst("^(\\w+).*$","$1")
Have fun.