2010-11-18 7 views
13

mam ten kod:Wrap ciąg po liczba znaków słowo-mądry w Javie

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 + 20)) != -1) { 
     sb.replace(i, i + 1, "\n"); 
    } 

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

Wyjście Code jest:

A very long string containing 
many many words and 
characters. Newlines 
will be entered at spaces. 

Powyższy kod jest owijanie ciąg po następna przestrzeń co 30 znaków, ale muszę zawinąć ciąg po poprzedniej przestrzeni co 30 znaków, tak jak w przypadku pierwszej linii:

A very long string

i 2. linia będzie

containing many 

Proszę podać jakieś właściwe rozwiązanie.

Odpowiedz

13

Użyj lastIndexOf zamiast , np.

StringBuilder sb = new StringBuilder(s); 

int i = 0; 
while (i + 20 < sb.length() && (i = sb.lastIndexOf(" ", i + 20)) != -1) { 
    sb.replace(i, i + 1, "\n"); 
} 

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

To będzie następujący wynik:

A very long string 
containing many 
many words and 
characters. 
Newlines will be 
entered at spaces. 
+0

Dzięki za odpowiedź. W tym przykładzie, jeśli podam ciąg jako "Bardzo długi ciąg122222222222222222222222222222 zawierający wiele słów i znaków, znaki Newlines zostaną wprowadzone w przestrzeniach." wtedy out put nie owija się po długim słowie ("string122222222222222222222222222222") i nieoczekiwanie zawija przed długim słowem. – Suvonkar

+0

@Suvonkar: Możesz zajrzeć do źródła [WordUtils.wrap()] (http://kickjava.com/src/org/apache/commons/lang/WordUtils.java.htm) i zaimplementować je tak, jak lubisz. – Emil

1

można spróbować wykonać następujące czynności:

public static String wrapString(String s, String deliminator, int length) { 
    String result = ""; 
    int lastdelimPos = 0; 
    for (String token : s.split(" ", -1)) { 
     if (result.length() - lastdelimPos + token.length() > length) { 
      result = result + deliminator + token; 
      lastdelimPos = result.length() + 1; 
     } 
     else { 
      result += (result.isEmpty() ? "" : " ") + token; 
     } 
    } 
    return result; 
} 

połączenie jako wrapString ("asd xyz afz", "\ n", 5)

-1
public static void main(String args[]) { 

     String s1="This is my world. This has to be broken."; 
     StringBuffer buffer=new StringBuffer(); 

     int length=s1.length(); 
     int thrshld=5; //this valueis threshold , which you can use 
     int a=length/thrshld; 

     if (a<=1) { 
      System.out.println(s1); 
     }else{ 
      String split[]=s1.split(" "); 
      for (int j = 0; j < split.length; j++) { 
       buffer.append(split[j]+" "); 

       if (buffer.length()>=thrshld) { 

        int lastindex=buffer.lastIndexOf(" "); 

        if (lastindex<buffer.length()) { 

         buffer.subSequence(lastindex, buffer.length()-1); 
         System.out.println(buffer.toString()); 
         buffer=null; 
         buffer=new StringBuffer(); 
        } 
       } 
      } 
     } 
    } 

może to być jeden ze sposobów uzyskania

0

"\ n" wykonuje zawijanie słów.

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

to rozwiąże Twój problem

Powiązane problemy