有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中的字符串如何提取

例如,我有一个字符串,如下所示:

<http://www.w3.org/2000/01/rdf-schema#label> "Telecommunications law"@en <http://en.wikipedia.org/wiki/> 

提取子字符串的最简单方法是什么:

Telecommunication law

请注意,子字符串包含一个空格


共 (5) 个答案

  1. # 1 楼答案

    “提取字符串”是什么意思

    获取字符串的第一次出现是通过:

    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);
    
  2. # 2 楼答案

    您可以使用模式和匹配器:

    Pattern p = Pattern.compile("\".*\"");
    Matcher m = p.matcher(s);
    
    if(m.find()){
       String resultString = m.group();
    }
    

    在您的情况下,resultString将包含[“Telecommunications law”],如果不想保留双引号,您可以修剪双引号

  3. # 3 楼答案

    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]);
                }
            }
        }
    
  4. # 4 楼答案

    String.split()"上选择字符串,然后选择返回数组中的第二个元素:

    String tokens[] = yourString.split("\"");
    
    // tokens[1] will contain Telecommunications law
    
  5. # 5 楼答案

        public static void main(String[] args) {
     String str = "http://www.w3.org/2000/01/rdf-schema#label \"Telecommunications law\"@en http://en.wikipedia.org/wiki/" ;
    
     String temp = str.substring(str.indexOf('\"')+1, str.indexOf('\"',str.indexOf('\"')+1));
     System.out.print(temp);
    
        }