我试图用±(alt + 0177)符号分割我的字符串,但是它没有检测到它。 我也尝试过indexOf(),但它不起作用
String myString = "20±1";
if(myString.indexOf('±')>-1){
System.out.println("We are in here.........");
}
答案 0 :(得分:1)
使用功能split()
String myString = "20±1";
String result[] = myString.split("±");
//result[0] = 20
//result[1] = 1
答案 1 :(得分:1)
您可以使用ascii值作为'±'符号。
轻松获取此回复here
中显示的ascii值在你的情况下:
final int ascii = (int) '±';
final String myString = "20±1";
if(myString.indexOf(ascii)>-1){
System.out.println("We are in here.........");
}
答案 2 :(得分:0)
/*Your String*/
String myString = "20±1";
/*If you want to split String you can use String.split("your string regex here")
* and it will create String array without specified string regex with left, right
* side of string or multiple strings depending on occurrence of specified string regex*/
String[] splitted = myString.split("±");
/*Just to validate output*/
System.out.println(Arrays.toString(splitted));
答案 3 :(得分:0)
您也可以使用StringTokenizer
解决此问题:
import java.io.*;
import java.util.*;
class happy {
public static void main(String args[])
{
String myString = "20±1";
StringTokenizer st=new StringTokenizer(myString,"±");
String a="";
while(st.hasMoreTokens())
{
a=st.nextToken();
System.out.println(a);
}
}
}