2014-02-14 101 views
-2

在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。請幫助

+0

你的意思是'split'?看'String#split()'方法。 –

回答

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

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

這會讓您知道名稱的起始索引以及它的結束位置。然後你使用子串方法來提取這兩個索引之間的字符串。

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

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]); 
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

0

問題是,逗號是字符串中出現了好幾次。下面是工作的解決方案:

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. 
相關問題