【问题标题】:Find variable ID's in a string with random text在带有随机文本的字符串中查找变量 ID
【发布时间】:2013-07-21 18:01:19
【问题描述】:

我有类似以下的字符串:http://pastebin.com/8QxSC8zJ

我真正需要的是 SteamID,例如“STEAM_0:1:49093894”。 “STEAM_0”部分始终相同,但之后的两个数字是可变的。将这些 ID 作为字符串数组检索的最佳方法是什么?

更新:我查找了 Jim Garrison 建议的正则表达式,并得出以下结论:

String out = //see pastebin
String[] substrings = out.split("\\s+"); //splits at whitespace

for(String a : substrings) {
    if(a.matches("STEAM_0.*")) {
        System.out.println(a);
        //add to string array/list/whatever
    }
}

这确实打印了所有 SteamID(我可以将它们添加到字符串数组中),但是如果字符串中没有空格,我该怎么做呢?

【问题讨论】:

  • 只在 冒号 (:) 上分割。
  • 您阅读过java.util.regex.Pattern 的Javadoc 吗?这种类型的问题听起来像“请为我做我的工作”。
  • 请注意STEAM_1 是一个有效的 SteamID。

标签: java string search steam


【解决方案1】:

如果你想要字符串数组,你必须知道长度,以便初始化数组。假设它是 3。你可以这样做:

public class Main {

public static void main(String[] args) {
    public static void main(String[] args) {

        String Str = new String("STEAM_0:1:49093894");
        String[] array= new String[2];

            array[0]=Str.substring(8,9) ;
            array[1]=Str.substring(9);
            }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    • 2011-10-13
    • 2018-07-17
    • 1970-01-01
    • 2017-01-31
    相关资源
    最近更新 更多