【问题标题】:How to find a specific value after a specific word in Java?如何在Java中的特定单词之后找到特定值?
【发布时间】:2012-07-20 15:18:56
【问题描述】:

我从BufferedReader 收到一条文本,我需要在特定字符串中获取特定值。

这是正文:

    aimtolerance = 1024;
    model = Araarrow;
    name = Bow and Arrows;
    range = 450;
    reloadtime = 3;
    soundhitclass = arrow;
    type = Ballistic;
    waterexplosionclass = small water explosion;
    weaponvelocity = 750;

        default = 213;
        fort = 0.25;
        factory = 0.25;
        stalwart = 0.25;
        mechanical = 0.5;
        naval = 0.5;

我需要得到准确的数字 默认 =;

这是“213”

【问题讨论】:

  • 如果你将文件读入内存(读入字符串),你可以使用字符串函数。

标签: java string find bufferedstream


【解决方案1】:

这样的……

String line;
while ((line = reader.readLine())!=null) {
   int ind = line.indexOf("default =");
   if (ind >= 0) {
      String yourValue = line.substring(ind+"default =".length(), line.length()-1).trim(); // -1 to remove de ";"
      ............
   }
}

【讨论】:

  • A.Poletti 我尝试使用上面的代码它只抛出-1。
  • @RanjanGupta 也许你的行中有一个特殊的字符,因为它与 indexOf 不匹配
【解决方案2】:

如果只关心最终结果,即从“=”分隔值文本文件中获取内容,您可能会发现内置的 Properties 对象很有帮助?

http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html

这可以满足您的大部分需求。当然,如果您特别想手动执行此操作,那可能不是正确的选择。

【讨论】:

    【解决方案3】:

    您可以使用Properties 类加载字符串并从中查找任何值

    String readString = "aimtolerance = 1024;\r\n" + 
    "model = Araarrow;\r\n" + 
    "name = Bow and Arrows;\r\n" + 
    "range = 450;\r\n" + 
    "reloadtime = 3;\r\n" + 
    "soundhitclass = arrow;\r\n" + 
    "type = Ballistic;\r\n" + 
    "waterexplosionclass = small water explosion;\r\n" + 
    "weaponvelocity = 750;\r\n" + 
    "default = 213;\r\n" + 
    "fort = 0.25;\r\n" + 
    "factory = 0.25;\r\n" + 
    "stalwart = 0.25;\r\n" + 
    "mechanical = 0.5;\r\n" + 
    "naval = 0.5;\r\n";
    readString = readString.replaceAll(";", "");
    Properties properties = new Properties();
    
    System.out.println(properties);
    try {
        properties.load(new StringReader(readString));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println(properties);
    
    String requiredPropertyValue = properties.getProperty("default");
    System.out.println("requiredPropertyValue : "+requiredPropertyValue);
    

    【讨论】:

      【解决方案4】:

      在“default =”上拆分字符串,然后使用 indexOf 查找“;”的第一次出现。做一个从 0 到索引的子字符串,你就有了你的价值。

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

      【讨论】:

        【解决方案5】:

        使用正则表达式:

        private static final Pattern DEFAULT_VALUE_PATTERN
                = Pattern.compile("default = (.*?);");
        
        private String extractDefaultValueFrom(String text) {
            Matcher matcher = DEFAULT_VALUE_PATTERN.matcher(text);
            if (!matcher.find()) {
                throw new RuntimeException("Failed to find default value in text");
            }
            return matcher.group(1);
        }
        

        【讨论】:

          猜你喜欢
          • 2022-01-04
          • 1970-01-01
          • 2021-08-01
          • 2021-11-21
          • 2021-12-18
          • 1970-01-01
          • 2019-10-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多