Vijay Kumar
Vijay Kumar

Reputation: 63

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 {

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();
    }
  }
}

Upvotes: 0

Views: 1456

Answers (5)

Brajesh Tiwary
Brajesh Tiwary

Reputation: 19

String var = "/"; String query = "INSERT INTO Recieptnumbersetup VALUES('"+Prestring+"' '"+var+"','"+var+"' '"+post string+"')" ;

PS = connection.PrepareStatement(query);

Use this i have used slash over here i was having same problem.

Upvotes: 0

sanbhat
sanbhat

Reputation: 17622

You can do it with the help of substring() and replaceAll() methods

String value="MOTOR PRIVATE CAR-PACKAGE POLICY";
value = value.substring(0, value.indexOf("-")); //get the string till -
value = value.replaceAll("\\s", "-"); //replace all the space chars with -
System.out.println(value);

I have used String.replaceAll() instead of String.replace() to use the regex for white space

\s stands for white space character and and while adding it as regex, we need to escape it with an extra \ so --> \\s

indexOf("-") method returns the index of first occurrence of the String passed, which should be the 2nd parameter to substring method, which is the endIndex

Upvotes: 8

akaHuman
akaHuman

Reputation: 1352

You can split the string with '-' which gives you the part of the string in which you need to insert ' '. Split the string again with ' ' and insert '-' b/w the words.

String value="MOTOR PRIVATE CAR-PACKAGE POLICY";
String[] phrase = value.split("-");
String[] words = phrase[0].split(" ");

String newValue;
for(int i = 0; i < words.length; i++)
  newValue += words[i] + "-";

Upvotes: 0

Janny
Janny

Reputation: 691

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+$", ""));

   value = value.substring(0,value.indexOf("-")); // get the words before "-"
   value = value.replace(" ", "-"); // replace space with hiphen
   System.out.println(value);
} catch (Exception e) {

   e.printStackTrace();
}


}

}

Upvotes: 1

Juned Ahsan
Juned Ahsan

Reputation: 68715

You can do it in two steps:

  1. To get all the words from the string before "-", you can use String substring and indexOf methods.
  2. To replace empty spaces with hiphen(-), you can use the String replace method.

Here is the code:

String value="MOTOR PRIVATE CAR-PACKAGE POLICY";
value = value.substring(0,value.indexOf("-")); // get the words before "-"
value = value.replace(" ", "-"); // replace space with hiphen
System.out.println(value);

Upvotes: 1

Related Questions