简体   繁体   English

如何在Java中的特定词后找到特定值?

[英]How to find a specific value after a specific word in Java?

I got a text from a BufferedReader and I need to get a specific value in a specific string. 我从BufferedReader获得了文本,我需要在特定字符串中获取特定值。

This is the text: 这是文本:

    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;

I need to get the exact number between default = and ; 我需要获取默认=;之间的确切数字

Which is "213" 就是“ 213”

Something like this.... 像这样...

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 ";"
      ............
   }
}

If just care about the end result, ie getting stuff out of your '=' separated values text file, you might find the built in Properties object helpful? 如果只关心最终结果,即从“ =”分隔值文本文件中获取内容,您可能会发现内置的Properties对象有用吗?

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

This does much of what you need. 这可以满足您的大部分需求。 Of course if you're specifically wanting to do this manually, it's maybe not the right option. 当然,如果您特别想手动执行此操作,则可能不是正确的选择。

Split the string on "default =" and then use indexOf to find the first occurrence of ";". 分割字符串的“默认值=”,然后使用的indexOf找到的第一次出现“;”。 Do a substring from 0 to the index and you have your value. 做一个从0到索引的子字符串,您便拥有了自己的价值。

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

Using regular expressions: 使用正则表达式:

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

you can use Properties class to load the string and find any value from it 您可以使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM