【问题标题】:extract string including whitespaces within string (java)提取字符串,包括字符串中的空格(java)
【发布时间】:2012-07-12 21:56:26
【问题描述】:

我有一个字符串,其中包含 2 个整数,由空格分隔,后跟任何包含空格的字符串。

示例:

23 14 this is a random string

如何提取this is a random string

整数不能保证是两位数,因此我不知道如何使用 indexOf 和 substring 来提取这些数据。

提前致谢。

【问题讨论】:

  • exampleString.substring(exampleString.indexOf(" ", exampleString.indexOf(" ")+1) + 1);
  • 你可以看看java.util.Scanner类

标签: java string whitespace substring extract


【解决方案1】:

使用split(String regex, int limit):

String s = "23 14 this is a random string";
String[] arr = s.split(" ", 3);
System.out.println(arr[2]);

输出:

这是一个随机字符串

【讨论】:

  • 这是最简单的解决方案,我认为您应该将其标记为答案。 :)
  • @Geoff 顺便说一句,这不是最简单的解决方案(请参阅我的解决方案),如果剩余文本为空白(即输入为 "12 34",不排除在问题),因为拆分中不会有第三个元素
【解决方案2】:
String str = "23 14 this is a random string";
str = str.replaceAll("[0-9]", "");
str = str.trim();

【讨论】:

  • .. 如果“随机字符串”也包含数字怎么办?
  • 然后你使用 Eng Fouad 的解决方案。 :)
【解决方案3】:

我会使用以上答案的组合,regex + replaceFirst

String s = "23 14 this is a random string";
String formattedS = s.replaceFirst("\\d+ \\d+ ", "");

这将删除由空格分隔的前两个数字,无论数字有多大。

【讨论】:

    【解决方案4】:

    哇。我不敢相信有些人写了多少代码来做最简单的事情......

    这是一个优雅的单行解决方案:

    String remains = input.replaceAll("^(\\d+\\s*){2}","");
    

    这是一个测试:

    public static void main( String[] args ) {
        // rest of String contains more numbers as an edge case
        String input = "23 14 this is a random 45 67 string";
        String remains = input.replaceAll("^(\\d+\\s*){2}","");
        System.out.println( remains );
    }
    

    输出:

    this is a random 45 67 string
    

    【讨论】:

      【解决方案5】:

      您可以使用 StringTokenizer,如果您知道您将有 2 个数字,只需忽略数组中的前两个元素。

      StringBuffer sb = new StringBuffer();
      StringTokenizer st = new StringTokenizer("23 14 this is a random string");
      
      int i = 1; // counter: we will ignore 1 and 2 and only append
      
      while (st.hasMoreTokens()) {
         // ignore first two tokens
         if (i > 2) {
                   sb.append(st.nextToken()); // adds remaining strings to Buffer
         }
      
         i++;  // increment counter
      } // end while
      
      // output content
      sb.toString();  
      

      【讨论】:

        【解决方案6】:

        随便用

        StringTokenizer st=new StringTokenizer(youString, " "); //whitespaces as delimeter
        
        int firstInteger=Integer.parseInt(st.nextToken());
        int secondInteger=Integer.parseInt(st.nextToken());
        

        其余的令牌类似..

        你可以做一个明确的数组..

        并将其余标记存储在这样的字符串数组中..

        while(st.hasMoreTokens())
        {
         ar[i]=st.nextToken();
        }
        

        【讨论】:

          【解决方案7】:

          老实说...如果您的String 遵循某种模式并且您需要从中提取一些内容,请尝试使用Regex。就是为此而生的。

          Pattern regex = Pattern.compile("^\\d+ \\d+ (.*)$");
          Matcher matcher = regex.matcher("23 14 this is a random string");
          
          if (matcher.find())
              System.out.println(matcher.group(1));
          

          输出:

          this is a random string
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-01-13
            • 2020-10-22
            • 1970-01-01
            • 2015-07-06
            • 2020-11-13
            • 2019-10-15
            相关资源
            最近更新 更多