如何在Java中提取字符串
例如,我有一个字符串,如下所示:如何在Java中提取字符串,java,string,substring,Java,String,Substring,例如,我有一个字符串,如下所示: <http://www.w3.org/2000/01/rdf-schema#label> "Telecommunications law"@en <http://en.wikipedia.org/wiki/> 请注意,子字符串包含一个空格。String.split()在“上选择字符串,然后选择返回数组中的第二个元素: String tokens[] = yourString.split("\""); // tokens[1] wil
<http://www.w3.org/2000/01/rdf-schema#label> "Telecommunications law"@en <http://en.wikipedia.org/wiki/>
请注意,子字符串包含一个空格。String.split()
在“
上选择字符串,然后选择返回数组中的第二个元素:
String tokens[] = yourString.split("\"");
// tokens[1] will contain Telecommunications law
String.split()
String tokens[] = yourString.split("\"");
// tokens[1] will contain Telecommunications law
“提取字符串”是什么意思
获取字符串的第一次出现是通过:
int index = string.indexOf("Telecommunications law");
获取第一个括号和第二个括号之间内容的最有效方法是:
final String test="http://www.w3.org/2000/01/rdf-schema#label \"Telecommunications law\"@en http://en.wikipedia.org/wiki/";
final int firstIndex=test.indexOf('\"');
final int lastIndex=test.indexOf('\"',firstIndex+1);
final String result=test.substring(firstIndex+1,lastIndex);
System.out.println(result);
“提取字符串”是什么意思
获取字符串的第一次出现是通过:
int index = string.indexOf("Telecommunications law");
获取第一个括号和第二个括号之间内容的最有效方法是:
final String test="http://www.w3.org/2000/01/rdf-schema#label \"Telecommunications law\"@en http://en.wikipedia.org/wiki/";
final int firstIndex=test.indexOf('\"');
final int lastIndex=test.indexOf('\"',firstIndex+1);
final String result=test.substring(firstIndex+1,lastIndex);
System.out.println(result);
您可以使用模式和匹配器:
Pattern p = Pattern.compile("\".*\"");
Matcher m = p.matcher(s);
if(m.find()){
String resultString = m.group();
}
在您的情况下,resultString将包含[“Telecommunications law”],如果不想保留双引号,您可以修剪双引号。您可以使用Pattern and Matcher:
Pattern p = Pattern.compile("\".*\"");
Matcher m = p.matcher(s);
if(m.find()){
String resultString = m.group();
}
public static void main(String args[]){
String yourString = "<http://www.w3.org/2000/01/rdf-schema#label> \"Telecommunications law\"@en <http://en.wikipedia.org/wiki/>";
String tokens[] = yourString.split("\"");
for(int i = 0; i < tokens.length; i++){
if(tokens[i].equals("Telecommunications law")){
System.out.println(tokens[i]);
}
}
}
在您的情况下,resultString将包含[“Telecommunications law”],如果不想保留双引号,您可以修剪双引号。public static void main(字符串args[]){
public static void main(String args[]){
String yourString = "<http://www.w3.org/2000/01/rdf-schema#label> \"Telecommunications law\"@en <http://en.wikipedia.org/wiki/>";
String tokens[] = yourString.split("\"");
for(int i = 0; i < tokens.length; i++){
if(tokens[i].equals("Telecommunications law")){
System.out.println(tokens[i]);
}
}
}
String yourString=“\”电信法\“@en”;
字符串标记[]=yourString.split(“\”);
for(int i=0;i
公共静态void main(字符串参数[]){
String yourString=“\”电信法\“@en”;
字符串标记[]=yourString.split(“\”);
for(int i=0;i
我想用正则表达式提取子字符串。我想用正则表达式提取子字符串。重复一系列其他问题,而不是其中的最小问题。重复一系列其他问题,而不是其中的最小问题。@andriod developer从该字符串中我只需要远程通信法。同样,你想知道括号之间是什么吗?或者您只是想检查指定的字符串是否存在?不管怎样,我都用我写的代码回答了这两个问题。@andriod在第二个问题(想检查指定的字符串是否存在)中向您介绍了wright,并询问了括号之间的内容。我不明白。如果您的意思是如何检查字符串是否存在,只需检查从第一个代码中得到的整数是否为非负。@andriod developer从该字符串中我只需要远程通信法则。同样,您想知道括号之间是什么吗?或者您只是想检查指定的字符串是否存在?不管怎样,我都用我写的代码回答了这两个问题。@andriod在第二个问题(想检查指定的字符串是否存在)中向您介绍了wright,并询问了括号之间的内容。我不明白。如果您的意思是如何检查字符串是否存在,那么只需检查从第一个代码获得的整数是否为非负。