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

how can I, character and words count from string to make a line break insert in android

我如何,字符和单词从字符串中计数以在android

中插入换行符

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

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);

假设您想在接近 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);

希望这对您有所帮助。