【问题标题】:Spliting a string in an array of striings of limited size在有限大小的字符串数组中拆分字符串
【发布时间】:2022-07-08 00:12:22
【问题描述】:

我有一串随机地址,比如

String s="H.N.-13/1443 laal street near bharath Dental lab near thana qutubsher modern bakery saharanpur uttar pradesh 247001";

我想用两个条件将它拆分成字符串数组:

#该字符串数组的每个元素的长度小于或等于20。

#字符串数组的元素没有尴尬的结尾。例如:-

[

{H.N.-13/1443 laal st},

{reet near bharath de},

{thana 附近的国家实验室},

{qutubsher 靠近现代},

{n 面包店撒哈拉普尔}

]

正确的输出应该是 [

{H.N.-13/1443 拉尔},

{bharath 附近的街道},

{附近的牙科实验室},

{thana qutubsher 附近},

{现代面包店}

{撒哈拉普尔} ]

注意字符串数组中的每个元素如何小于或等于 20。

以上是我对这段代码的输出:

\\

         static  String[] split(String s,int max){

       int total_lines = s.length () / 24;

if (s.length () % 24 != 0)
  {
total_lines++;
  }

String[] ans = new String[total_lines];

int count = 0;

int j = 0;

for (int i = 0; i < total_lines; i++)
  {
for (j = 0; j < 20; j++)
  {
    if (ans[count] == null)
      {
    ans[count] = "";
      }

    if (count > 0)
      {
    if ((20 * count) + j < s.length ())
      {
        ans[count] += s.charAt (20 * count + j);
      }
    else
      {
        break;
      }
      }
    else
      {
    ans[count] += s.charAt (j);
      }

  }


String a = "";

a += ans[count].charAt (0);

if (a.equals (" "))
  {
    ans[i] = ans[i].substring (0, 0) + "" + ans[i].substring (1);

  }

System.out.println (ans[i]);

count++;
  }
return ans;

}

         public static void main (String[]args)

                   {

           String add =
  
                  "H.N.-13/1663 laal street near bharath dental lab near thana qutubsher near modern bakery";
String city = "saharanpur";
String state = "uttar pradesh";
String zip = "247001";
String s = add + " " + city + " " + state + " " + zip;
String[]ans = split (s);


         }

【问题讨论】:

    标签: java arrays string algorithm data-structures


    【解决方案1】:

    代码不是很清楚,但乍一看,您似乎是在逐个字符地构建,这就是您获得所见输出的原因。相反,如果您想保留一个单词并在必要时将其溢出到下一个数组,您可以逐字逐句。更有希望的代码是:

    static String[] split(String s, int max) {
        String[] words = s.split("\s+");
        List<String> out = new ArrayList<>();
            
        int i = 0;
        while (i < words.length) {
            int len = 0;
            StringBuilder sb = new StringBuilder();
            while (i < words.length && len < max) {
                len += words[i].length();
                if (len <= max) {
                    sb.append(words[i]+ " ");
                    i++;
                }
            }
            out.add(sb.toString().trim());
        }
        return out.toArray(new String[] {});
    }
    

    注意:它适用于您的示例输入,但您可能需要对其进行调整,使其适用于包含超过 20 个字符的长词等情况。

    【讨论】:

    • 我想说空格应该包含在长度中。如果您只在新单词的开头添加空格,那么您可以使用 StringBuilder 的大小(反过来,可以将其初始化为 20 的容量)。
    【解决方案2】:

    查找以非空格开头并以单词边界结尾的最多 20 个字符的所有匹配项,并将它们收集到一个列表中:

    List<String> parts = Pattern.compile("\\S.{1,19}\\b").matcher(s)
      .results()
      .map(MatchResult::group)
      .collect(Collectors.toList());
    

    live demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-05
      • 1970-01-01
      相关资源
      最近更新 更多