[SOLVED] How to get first two word from a string value


I have two String: "I am a Beginner", "Hello Dear App Developer"
How can I get only the first two words using Java "I am" "Hello Dear"

String TextOne = "I am a Beginner"
String TextTwo = "Hello Dear App Developer"
I need only "I am" and "Hello Dear"


You can split string by space(" ")

String TextOne = "I am a Beginner";
String[] strList = TextOne.split(" "); //["I", "am", "a", "Beginner"]
String finalTextOne = strList[0] + " " + strList[1];  
Log.e("finalTextOne: ", finalTextOne); // It will print "I am"

Answered By – Jinal Patel

Answer Checked By – David Marino (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *