JAVA - adding a new line to the next space after 30 characters

I have a large block of text (200 + characters per String) and you need to insert new lines in the next space after 30 characters in order to save the words. Here is what I am now (NOT working):

String rawInfo = front.getItemInfo(name);
String info = "";
int begin = 0;
for(int l=30;(l+30)<rawInfo.length();l+=30) {
    while(rawInfo.charAt(l)!=' ')
        l++;
    info += rawInfo.substring(begin, l) + "\n";
    begin = l+1;
    if((l+30)>=(rawInfo.length()))
        info += rawInfo.substring(begin, rawInfo.length());
}

Thanks for any help

+3
source share
5 answers

As kdgregory suggested, using would StringBuilderprobably be a simpler way of dealing with string manipulations.

Since I was not sure that the number of characters before entering a new line is a word before or after 30 characters, I decided to go to the word after 30 characters, as the implementation is probably simpler.

, " ", 30 , StringBuilder.indexOf. , a \n StringBuilder.insert.

( , \n - , , System.getProperty("line.separator");).

:

String s = "A very long string containing " +
    "many many words and characters. " +
    "Newlines will be entered at spaces.";

StringBuilder sb = new StringBuilder(s);

int i = 0;
while ((i = sb.indexOf(" ", i + 30)) != -1) {
    sb.replace(i, i + 1, "\n");
}

System.out.println(sb.toString());

:

A very long string containing many
many words and characters. Newlines
will.

, , String, . , .

while, for, .

, StringBuilder.insert StringBuilder.replace, , replace, insert.

+16

.

import junit.framework.TestCase;

public class InsertLinebreaksTest extends TestCase {
    public void testEmptyString() throws Exception {
        assertEquals("", insertLinebreaks("", 5));
    }

    public void testShortString() throws Exception {
        assertEquals("abc def", insertLinebreaks("abc def", 5));
    }

    public void testLongString() throws Exception {
        assertEquals("abc\ndef\nghi", insertLinebreaks("abc def ghi", 1));
        assertEquals("abc\ndef\nghi", insertLinebreaks("abc def ghi", 2));
        assertEquals("abc\ndef\nghi", insertLinebreaks("abc def ghi", 3));
        assertEquals("abc def\nghi", insertLinebreaks("abc def ghi", 4));
        assertEquals("abc def\nghi", insertLinebreaks("abc def ghi", 5));
        assertEquals("abc def\nghi", insertLinebreaks("abc def ghi", 6));
        assertEquals("abc def\nghi", insertLinebreaks("abc def ghi", 7));
        assertEquals("abc def ghi", insertLinebreaks("abc def ghi", 8));
    }

    public static String insertLinebreaks(String s, int charsPerLine) {
        char[] chars = s.toCharArray();
        int lastLinebreak = 0;
        boolean wantLinebreak = false;
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < chars.length; i++) {
            if (wantLinebreak && chars[i] == ' ') {
                sb.append('\n');
                lastLinebreak = i;
                wantLinebreak = false;
            } else {
                sb.append(chars[i]);
            }
            if (i - lastLinebreak + 1 == charsPerLine)
                wantLinebreak = true;
        }
        return sb.toString();
    }
}
+4

: StringBuilder, / . indexOf(), , .

: tez codez:

public static String breakString(String str, int size)
{
    StringBuilder work = new StringBuilder(str);
    int pos = 0;
    while ((pos = work.indexOf(" ", pos + size)) >= 0)
    {
        work.setCharAt(pos, '\n');
    }
    return work.toString();
}
+2

30. 30, .

psuedocode, :

charInLine=0
iterateOver each char in rawString
    if(charInLine++ > 30 && currChar==' ')
        charInLine=0
        currChar='\n'
+1

, - .

s =... s = s.substring(0, 30) + s.substring(30).replace('', '\n');

This replaces all spaces with newlines, starting with character 30.

Its a little less efficient, but for 200 characters it really doesn't matter (which is extremely small for data manipulation).

Bruce

0
source

Source: https://habr.com/ru/post/1711101/


All Articles