Sha*_*raf 9 java string replace
我有一个字符串,我通过一个html表单得到了价值,所以当我得到它的值时,它会出现在URL中,所以我想删除特定字符之前的所有字符=,我也想删除这个字符.我只想保存之后的值,=因为我需要从变量中获取该值.
编辑:我需要删除它,=因为我试图获取字符串后的字符/值...
Ita*_*rG3 28
你可以使用.substring():
String s = "the text=text";
String s1 = s.substring(s.indexOf("=")+1);
s1.trim();
Run Code Online (Sandbox Code Playgroud)
然后s1包含=原始字符串中的所有内容.
.trim() 删除字符串(前导空格)的第一个字符(不是空格,如字母,数字等)之前的空格,并删除最后一个字符(尾随空格)后的空格.
虽然有很多答案。这是一个正则表达式示例
String test = "eo21jüdjüqw=realString";
test = test.replaceAll(".+=", "");
System.out.println(test);
// prints realString
Run Code Online (Sandbox Code Playgroud)
解释:
.+匹配任何字符(行终止符除外)
+量词 - 匹配一次和无限次,尽可能多次,根据需要回馈(贪婪)
=匹配字符 = 字面意思(区分大小写)
这也是来自https://regex101.com/的阴暗复制粘贴,您可以在其中尝试正则表达式。
您可以将字符串从 = 拆分并分隔到数组中,并在 = 符号之后取数组的第二个值,例如:
String CurrentString = "Fruit = they taste good";
String[] separated = CurrentString.split("=");
separated[0]; // this will contain "Fruit"
separated[1]; //this will contain "they teste good"
然后 separator[1] 包含原始字符串中 = 之后的所有内容。
| 归档时间: |
|
| 查看次数: |
39592 次 |
| 最近记录: |