【问题标题】:how can I, character and words count from string to make a line break insert in android我如何从字符串中计算字符和单词以在 android 中插入换行符
【发布时间】:2016-08-13 04:02:40
【问题描述】:

如何从字符串中计算字符和单词以在 android 中插入换行符

我有一个包含最多 150 个字母的单词的文本输出,并且希望所有 50 个字母都带有一个换行符而不分隔单词。 我该如何实施? 这是一个代码 sn -p ....

ll = (TableLayout) rootview.findViewById(R.id.displayLinear);
TableRow row = new TableRow(getActivity());
consoleLocalStore.storeConsoleData(consolex);

LayoutInflater inflater = getLayoutInflater(getArguments());
View theInflatedView = inflater.inflate(R.layout.activity_consolex,
        null);

TextView attid = (TextView) theInflatedView.findViewById(R.id.output);

attid.setText(String.valueOf(consolex.output));
attid = new TextView(getActivity());
row.addView(attid);

row.setGravity(Gravity.CENTER_HORIZONTAL);
row.addView(theInflatedView);


ll.addView(row, i);

【问题讨论】:

    标签: android count words letters


    【解决方案1】:

    假设您想在接近 50 的索引处插入 \n。您可能会使用 StringBuilder :

     StringBuilder builder = new StringBuilder(yourString);
     boolean inserted;
     int index = 50;
    
     do{
    
          if(yourString.charAt(index)==' '){
                   builder.insert(index,'\n');
                    inserted=true;
            }
           index++;
            /* or index-- if you want to insert \n before 50th character*/
           }while(inserted == false);
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2017-03-22
      • 1970-01-01
      • 2011-05-04
      • 2023-03-21
      • 2014-02-20
      相关资源
      最近更新 更多