【问题标题】:how to Split string to character Strings [duplicate]如何将字符串拆分为字符串[重复]
【发布时间】:2018-10-18 19:06:09
【问题描述】:

我有一个字符串,例如:“Adam”,我会将一个字符串拆分为一个单字符字符串数组。("a","d","a","m")

你的建议?

【问题讨论】:

    标签: java android string split


    【解决方案1】:
    String string = "Adam";
    String[] splitString = string.split(""); // Split string by each char
      for (String character : splitString) {
        System.out.println(character); // You can initiate object TextView here and add it to separate list
      }
    

    来自文档:

    公共字符串[] 拆分(字符串正则表达式)

    围绕给定正则表达式的匹配拆分此字符串。

    这个方法的工作方式就像是通过调用两个参数的 split 方法 给定的表达式和零的极限参数。尾随空 因此字符串不包含在结果数组中。

    例如,字符串“boo:and:foo”会产生以下结果 用这些表达方式:

    Regex     Result
    :     { "boo", "and", "foo" }
    o     { "b", "", ":and:f" }
    

    参数: regex - 分隔正则表达式 返回: 通过围绕给定正则表达式的匹配拆分此字符串计算的字符串数组

    【讨论】:

      【解决方案2】:
      String str = "Adam";
      TextView[] textViews = new TextView[4];
      textViews[0] = textView1;
      textViews[1] = textView2;
      textViews[2] = textView3;
      textViews[3] = textView4;
      for(int i=0; i<str.length(); i++) {
          textViews[i] = str.charAt(i);
      }
      

      【讨论】:

      • 为什么这被否决了?
      猜你喜欢
      • 2016-12-01
      • 1970-01-01
      • 2013-08-19
      • 2014-06-24
      • 1970-01-01
      • 1970-01-01
      • 2012-03-31
      • 2016-01-16
      • 2017-04-17
      相关资源
      最近更新 更多