【问题标题】:Android Split stringAndroid 拆分字符串
【发布时间】:2011-04-13 13:33:57
【问题描述】:

我有一个名为CurrentString 的字符串,其形式如下 "Fruit: they taste good".
我想使用:作为分隔符来拆分CurrentString
这样"Fruit"这个词将被拆分成自己的字符串,"they taste good"将是另一个字符串。
然后我只想使用两个不同的TextViews 中的SetText() 来显示该字符串。

解决此问题的最佳方法是什么?

【问题讨论】:

  • 您可以尝试阅读正则表达式。它们也可以正常工作。
  • @Falmarri - Stack Overflow 上欢迎任何关于编程的独特问题。

标签: java android string


【解决方案1】:

字符串 s = "字符串="

String[] str = s.split("="); //现在str[0]是“hello”,str[1]是“goodmorning,2,1”

添加这个字符串

【讨论】:

    【解决方案2】:
    String currentString = "Fruit: they taste good";
    String[] separated = currentString.split(":");
    separated[0]; // this will contain "Fruit"
    separated[1]; // this will contain " they taste good"
    

    您可能希望删除第二个字符串的空格:

    separated[1] = separated[1].trim();
    

    如果你想用点(.)这样的特殊字符分割字符串,你应该在点之前使用转义字符\

    例子:

    String currentString = "Fruit: they taste good.very nice actually";
    String[] separated = currentString.split("\\.");
    separated[0]; // this will contain "Fruit: they taste good"
    separated[1]; // this will contain "very nice actually"
    

    还有其他方法可以做到这一点。例如,您可以使用StringTokenizer 类(来自java.util):

    StringTokenizer tokens = new StringTokenizer(currentString, ":");
    String first = tokens.nextToken();// this will contain "Fruit"
    String second = tokens.nextToken();// this will contain " they taste good"
    // in the case above I assumed the string has always that syntax (foo: bar)
    // but you may want to check if there are tokens or not using the hasMoreTokens method
    

    【讨论】:

    • 谢谢!在创建新的 Time 对象时也可用于分隔小时和分钟。
    • 谢谢! .split() 方法在 Android 中根本不起作用! StringTokenizer 工作正常。
    • 是的……你遇到了什么问题?
    • android 中的split 接收正则表达式而不是简单的字符串分隔符。
    • @HardikParmar use etPhoneNo.getText().toString().replaceAll("\\D", ""); 它说替换所有不是数字的
    【解决方案3】:

    android 用逗号分割字符串

    String data = "1,Diego Maradona,Footballer,Argentina";
    String[] items = data.split(",");
    for (String item : items)
    {
        System.out.println("item = " + item);
    }
    

    【讨论】:

      【解决方案4】:

      您可能还需要考虑 Android 特定的 TextUtils.split() 方法。

      TextUtils.split() 和 String.split() 的区别用 TextUtils.split() 记录:

      当要拆分的字符串为空时,String.split() 返回 ['']。这将返回 []。这不会从结果中删除任何空字符串。

      我发现这是一种更自然的行为。本质上 TextUtils.split() 只是 String.split() 的一个瘦包装器,专门处理空字符串的情况。 The code for the method其实很简单。

      【讨论】:

      • 使用 TextUtils.split() 而不是直接在字符串上调用 split() 有什么好处?
      • 已编辑答案以阐明 TextUtils.split() 和 String.split() 之间的区别
      • 谢谢,我实际上阅读了 TextUtils.split() 的文档,但由于某种原因我错过了这个细节。我想我已经厌倦了去理解它实际上在说什么。
      【解决方案5】:
           String s = "having Community Portal|Help Desk|Local Embassy|Reference Desk|Site News";
           StringTokenizer st = new StringTokenizer(s, "|");
              String community = st.nextToken();
              String helpDesk = st.nextToken(); 
              String localEmbassy = st.nextToken();
              String referenceDesk = st.nextToken();
              String siteNews = st.nextToken();
      

      【讨论】:

        【解决方案6】:

        .split 方法会起作用,但它使用正则表达式。在这个例子中,它会是(从 Cristian 那里偷东西):

        String[] separated = CurrentString.split("\\:");
        separated[0]; // this will contain "Fruit"
        separated[1]; // this will contain " they taste good"
        

        另外,这来自: Android split not working correctly

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-08-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-28
          相关资源
          最近更新 更多