2010-11-18 15 views
13

ho questo codice:Avvolgere la stringa dopo un certo numero di caratteri di parola-saggio in Java

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

L'uscita del codice è:

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

Il codice di cui sopra è avvolgente la stringa dopo lo spazio successivo di ogni 30 caratteri, ma ho bisogno di avvolgere la stringa dopo lo spazio precedente di ogni 30 caratteri, come per la prima riga sarà:

A very long string

E la seconda linea sarà

containing many 

si prega di fornire una soluzione adeguata.

risposta

13

Utilizzare lastIndexOf anziché indexOf, ad es.

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

Questo produrrà il seguente output:

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

Grazie per la risposta. In questo esempio, se fornisco una stringa come "Una stringa molto lunga12222222222222222222222222222222 contenente molte parole e caratteri. Le nuove righe saranno inserite negli spazi." quindi l'out put non viene spostato dopo la parola lunga ("stringa122222222222222222222222222222") e in modo imprevisto prima della parola lunga. – Suvonkar

+0

@Suvonkar: Puoi cercare in [WordUtils.wrap()] (http://kickjava.com/src/org/apache/commons/lang/WordUtils.java.htm) e implementarlo nel modo che preferisci. – Emil

1

Si può provare la seguente:

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

chiamata come 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(); 
        } 
       } 
      } 
     } 
    } 

questo può essere un modo per raggiungere

0

"\ n" esegue un wordwrap.

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

questo risolverà il vostro problema

Problemi correlati