在大字符串中查找字符串/标记后的下一个子字符串

时间:2014-02-14 06:04:39

标签: java string substring next

在Java中, 我有这样的字符串:

String str = "PropertyNameAndStringValue[VERSION,5.3.1.20]],family=kwaas,filesize=939888640,imageType=OVA_SW(10000),name=ISR4451X-WAAS-5.3.1.20.ova,updatedTime=February 13, 2014 3:41:22 PM IST,version=5.3.1.20,instanceId=13325317,authEntityId=13325317,authEntityClass=-72900124,_orderedListOEIndex=<Integer>,_creationOrderIndex=<Integer>,instanceVersion=0]"

我需要提取名称:它位于“name =”旁边。我需要在“next =”之后找到下一个直接的单词,在本例中为“ISR4451X-WAAS-5.3.1.20.ova”。

我该怎么做?

我试过这个

    Scanner sc = new Scanner(str);
    String nameOVA = sc.next("name=");

我收到了java.util.InputMismatchException。请帮忙

4 个答案:

答案 0 :(得分:0)

int indexOfName = str.indexOf("name=");
indexOfName += 5;
int indexOfComma = str.indexOf(",", indexOfName);

string name = str.substring(indexOfName, indexOfComma);

这将为您提供名称的起始索引及其结束位置。然后使用substring方法在这两个索引之间提取字符串。

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#indexOf%28java.lang.String,%20int%29

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

答案 1 :(得分:0)

简单地尝试一下。我知道这不是最优雅的方式,但它应该提供所需的输出。

String str="PropertyNameAndStringValue[VERSION,5.3.1.20]],family=kwaas,filesize=939888640,imageType=OVA_SW(10000),name=ISR4451X-WAAS-5.3.1.20.ova,updatedTime=February 13, 2014 3:41:22 PM IST,version=5.3.1.20,instanceId=13325317,authEntityId=13325317,authEntityClass=-72900124,_orderedListOEIndex=,_creationOrderIndex=,instanceVersion=0]";

System.out.println(str.split("name=")[1].split(",")[0]);

答案 2 :(得分:0)

你可以试试这些代码::

static String str = "PropertyNameAndStringValue[VERSION,5.3.1.20]],family=kwaas,filesize=939888640,imageType=OVA_SW(10000),name=ISR4451X-WAAS-5.3.1.20.ova,updatedTime=February 13, 2014 3:41:22 PM IST,version=5.3.1.20,instanceId=13325317,authEntityId=13325317,authEntityClass=-72900124,_orderedListOEIndex=<Integer>,_creationOrderIndex=<Integer>,instanceVersion=0]";

public static void main(String[] args) {
 String[] property = str.split(",");

    for (int i = 0; i < property.length; i++) {

        String[] name = property[i].split("=");

        for (int j = 0; j < name.length; j++) {

            if (name[j].equals("name")) {

                System.out.println("Name : " + name[j + 1]);
            }
        }
    }
}

输出将是:: Name : ISR4451X-WAAS-5.3.1.20.ova

答案 3 :(得分:0)

这里的问题是COMMA在字符串中出现了好几次。以下是工作解决方案:

package com.tokenizing;

import java.util.StringTokenizer;


public class ParseString {
    static String str = "PropertyNameAndStringValue[VERSION,5.3.1.20]],family=kwaas,filesize=939888640,imageType=OVA_SW(10000),name=ISR4451X-WAAS-5.3.1.20.ova,updatedTime=February 13, 2014 3:41:22 PM IST,version=5.3.1.20,instanceId=13325317,authEntityId=13325317,authEntityClass=-72900124,_orderedListOEIndex=<Integer>,_creationOrderIndex=<Integer>,instanceVersion=0]";
    public static void main(String[] args) {
        String [] strArray = str.split(",");
        for(String s : strArray) {
            if(null != s && !s.equalsIgnoreCase("")) {
                StringTokenizer st = new StringTokenizer(s, "=");
                String key = "";
                String value = "";
                while(st.hasMoreElements()) {
                    key = (String) st.nextElement();
                    try {
                        value = (String) st.nextElement();
                    } catch(Exception e) {
                        System.out.println("String is not formatted correctly for key [" + key + "], ignore and proceed go next element.");
                    }

                    if(key.equals("name")) {
                        System.out.println("Value of 'name' attribute is: " + value);
                    }
                }
            }
        }
    }
}

输出将是:

String is not formatted correctly for key [PropertyNameAndStringValue[VERSION], ignore and proceed go next element.
String is not formatted correctly for key [5.3.1.20]]], ignore and proceed go next element.
Value of 'name' attribute is: ISR4451X-WAAS-5.3.1.20.ova
String is not formatted correctly for key [ 2014 3:41:22 PM IST], ignore and proceed go next element.