[英]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.
现在,我想删除最后两个单词,并在单词之间添加连字符。最后,我想要像“ MOTOR-PRIVATE-CAR'这样的字符串。我在Java中使用字符串方法尝试了很多次,但找不到确切的位置。任何人都可以为此提供解决方案。代码对我来说是加号。
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();
}
}
}
You can do it with the help of substring()
and replaceAll()
methods 您可以在
substring()
和replaceAll()
方法的帮助下完成此操作
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 我用
String.replaceAll()
代替String.replace()
将正则表达式用于空白
\\s
stands for white space character and and while adding it as regex , we need to escape it with an extra \\
so --> \\\\s
\\s
代表空格字符,并且在将其添加为正则表达式时 ,我们需要使用一个额外的\\
对其进行转义,所以-> \\\\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
indexOf("-")
方法返回传递的String的首次出现的索引,该索引应该是substring
方法的第二个参数,即endIndex
You can do it in two steps: 您可以分两个步骤进行操作:
substring
and indexOf
methods. substring
和indexOf
方法。 replace
method. replace
方法。 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);
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();
}
}
}
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] + "-";
String var = "/"; 字符串var =“ /”; String query = "INSERT INTO Recieptnumbersetup VALUES('"+Prestring+"' '"+var+"','"+var+"' '"+post string+"')" ;
字符串查询=“ INSERT INTO Recieptnumbersetup VALUES('” + Prestring +“''” + var +“','” + var +“''” +发布字符串+“')”;
PS = connection.PrepareStatement(query); PS = connection.PrepareStatement(query);
Use this i have used slash over here i was having same problem. 使用这个我在这里使用了斜杠我有同样的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.