【问题标题】:Trouble splitting up a String using indexOf in java在 java 中使用 indexOf 拆分字符串时遇到问题
【发布时间】:2017-11-19 08:05:28
【问题描述】:

我正在使用 pircbot 执行 Java IRC API 项目,该项目需要我实现天气 API。但是,在测试它的过程中遇到了一些问题,那就是处理消息字符串。我正在尝试这样做-用户输入:

(城市)天气(分量)

例如:奥斯汀的阵风

这表明用户想要使用天气 API 来获取 Austin 中的 gust 信息。 为此,我想“拆分”字符串并将 (City) 和 (component) 子字符串放入它们自己的字符串中。我试着这样做:

else if (message.contains("weather")) {
        String component;
        String city;
        int indexA = 0;
        int indexB = message.indexOf(" weather");

        int indexC = (message.indexOf("weather") + 6);
        int indexD = message.length() + 1;

        city = message.substring(indexA, indexB);
        component = message.substring(indexC, indexD);

        startWebRequestName(city, component, sender, channel);
    }

这似乎不起作用,所以我开始在测试类中进行实验:

public static void main (String args[]) {
        String message = "Austin weather gust";

        int firstIndex = message.indexOf("weather");
        System.out.println(firstIndex);
    }

在搞砸之后,indexOf 似乎对“Austin”中包含的每个字符和子字符串都有效,但在那之后就不行了。对于“Austin”之后的任何内容,例如“weather”、“w”或“gust”,indexOf 返回 -1,这很奇怪,因为我很肯定这些东西都在里面哈哈。有什么想法吗?

另外,如果我需要详细说明,请告诉我。我觉得这解释得有点糟糕。

【问题讨论】:

  • 你使用的是什么版本的Java(java -version)?

标签: java string api indexof irc


【解决方案1】:

如果您可以确定输入字符串是always上面给出的格式并且没有例外,您可以简单地使用以下方法快速获取城市和组件值。

    String inputString = "Austin weather gust";
    String[] separatedArray = inputString.split(" ");
    String city = separatedArray[0];
    String component = separatedArray[2];

【讨论】:

    【解决方案2】:

    请找到以下 cmets。

    private static void cityFetcher(String message) {
        if (message.contains("weather")) {
            String component;
            String city;
            int indexA = 0;
            int indexB = message.indexOf(" weather");
    
            int indexC = (message.indexOf("weather") + 6);
            System.out.println(indexC); //13
            int indexD = message.length() + 1;
            System.out.println(indexD);//20
            city = message.substring(indexA, indexB);
            System.out.println(city);//Austin
            component = message.substring(indexC, indexD);//if no -1 then ooi bound exception as your trying to access until 20th element. When your input size is only 19.
            System.out.println(component);// r gust
    
    }
    

    解决方案。如果您的输入格式与您声称的相同,则可以使用这两种格式中的任何一种。

    private static void cityFetcher(String input){
        //Solution 1
        String[] params=input.split("\\s");
        for(String s:params){
            System.out.println(s);
        }
    
        //Solution 2
    
        String city=input.substring(0,input.indexOf(" "));
        System.out.println(city);
        String component=input.substring(input.lastIndexOf(" ")+1,input.length());
        System.out.println(component);
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多