Sunday, 17 March 2019

How to Get First and Last word from String using Java

/* java program to get first and last word of a string */

class wordDisplay{
public static void main(String args[]){

String str = "Object Oriented Protramming" ;

//get first word
int firstSpacePosition = str.indexOf(" ");

String firstWord = str.substring(0,firstSpacePosition); // Object

//get last word

String lastWord = str.substring(str.lastIndexOf(" ")+1); //Programming

//print
System.out.println("first word : "+firstWord);
System.out.println("Last word: "+lastWord);
}
}

1 comment:

JTable Hide and Show in Java Swing