【问题标题】:Find next substring after a string/token in a big string在大字符串中的字符串/标记之后查找下一个子字符串
【发布时间】:2014-02-14 06:04:38
【问题描述】:

在 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]"

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

我该怎么做?

我试过了

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

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

【问题讨论】:

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

标签: java string substring next


【解决方案1】:

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

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】:
    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

    【讨论】:

      【解决方案3】:

      这里的问题是逗号在字符串中出现了好几次。以下是有效的解决方案:

      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.
      

      【讨论】:

        【解决方案4】:

        你可以试试这些代码::

        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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-06-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-07
          • 1970-01-01
          • 2015-08-31
          • 2017-03-26
          相关资源
          最近更新 更多