从 java 中的字符串中获取元素的快速方法
fast way to get an element from a string in java
我试图从我的字符串中获取一个元素,我已经从 JList 中获得了 getSelectedValue().toString
。
它returns[1] testString
我想做的只是从字符串中获取 1。有没有办法只从字符串中获取该元素或从字符串中删除所有其他元素?
我试过:
String longstring = Customer_list.getSelectedValue().toString();
int index = shortstring.indexOf(']');
String firstPart = myStr.substring(0, index);
你有很多方法可以做到,比如
- 正则表达式
String#replaceAll
String#substring
查看下面的代码以使用所有方法。
import java.util.*;
import java.lang.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Test {
public static void main(String args[]) {
String[] data = { "[1] test", " [2] [3] text ", " just some text " };
for (String s : data) {
String r0 = null;
Matcher matcher = Pattern.compile("\[(.*?)\]").matcher(s);
if (matcher.find()) {
r0 = matcher.group(1);
}
System.out.print(r0 + " ");
}
System.out.println();
for (String s : data) {
String r1 = null;
r1 = s.replaceAll(".*\[|\].*", "");
System.out.print(r1 + " ");
}
System.out.println();
for (String s : data) {
String r2 = null;
int i = s.indexOf("[");
int j = s.indexOf("]");
if (i != -1 && j != -1) {
r2 = s.substring(i + 1, j);
}
System.out.print(r2 + " ");
}
System.out.println();
}
}
但是结果可能会有所不同,例如,String#replaceAll
会在输入与您的预期不符时给出错误的结果。
1 2 null
1 3 just some text
1 2 null
对我来说最有效的是 String#replace(charSequence, charSequence)
结合 String#substring(int,int)
我做了如下操作:
String longstring = Customer_list.getSelectedValue().toString();
String shortstring = longstring.substring(0, longstring.indexOf("]") + 1);
String shota = shortstring.replace("[", "");
String shortb = shota.replace("]", "");
我的字符串已被缩短,[ 和 ] 已分两步删除。
我试图从我的字符串中获取一个元素,我已经从 JList 中获得了 getSelectedValue().toString
。
它returns[1] testString
我想做的只是从字符串中获取 1。有没有办法只从字符串中获取该元素或从字符串中删除所有其他元素?
我试过:
String longstring = Customer_list.getSelectedValue().toString();
int index = shortstring.indexOf(']');
String firstPart = myStr.substring(0, index);
你有很多方法可以做到,比如
- 正则表达式
String#replaceAll
String#substring
查看下面的代码以使用所有方法。
import java.util.*;
import java.lang.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Test {
public static void main(String args[]) {
String[] data = { "[1] test", " [2] [3] text ", " just some text " };
for (String s : data) {
String r0 = null;
Matcher matcher = Pattern.compile("\[(.*?)\]").matcher(s);
if (matcher.find()) {
r0 = matcher.group(1);
}
System.out.print(r0 + " ");
}
System.out.println();
for (String s : data) {
String r1 = null;
r1 = s.replaceAll(".*\[|\].*", "");
System.out.print(r1 + " ");
}
System.out.println();
for (String s : data) {
String r2 = null;
int i = s.indexOf("[");
int j = s.indexOf("]");
if (i != -1 && j != -1) {
r2 = s.substring(i + 1, j);
}
System.out.print(r2 + " ");
}
System.out.println();
}
}
但是结果可能会有所不同,例如,String#replaceAll
会在输入与您的预期不符时给出错误的结果。
1 2 null
1 3 just some text
1 2 null
对我来说最有效的是 String#replace(charSequence, charSequence)
结合 String#substring(int,int)
我做了如下操作:
String longstring = Customer_list.getSelectedValue().toString();
String shortstring = longstring.substring(0, longstring.indexOf("]") + 1);
String shota = shortstring.replace("[", "");
String shortb = shota.replace("]", "");
我的字符串已被缩短,[ 和 ] 已分两步删除。