Wednesday, 28 August 2013

String modification in java

String modification in java

Hi i am having string like " MOTOR PRIVATE CAR-PACKAGE POLICY " . Now i
want remove last two words and add hyphen between words finally i want
string like " MOTOR-PRIVATE-CAR' . I tried many times using string methods
in java but could not find exactly. Can anyone give a solution for that .
Give me a code is plus for me.
Thanks in advance
public class StringModify {
/**
* @param args
*/
public static void main(String[] args) {
try {
String value="MOTOR PRIVATE CAR-PACKAGE POLICY";
System.out.println("Value-------------------->"+value.replaceFirst("\\s*\\w+\\s+\\w+$",
""));
} catch (Exception e) {
e.printStackTrace();
}
}
}

No comments:

Post a Comment