2010-11-18 18 views
13

tôi có mã này:Bó chuỗi sau một số ký tự word-khôn ngoan trong 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()); 

Đầu ra của mã này là:

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

Đoạn mã trên được gói các chuỗi sau không gian tiếp theo của mỗi 30 ký tự, nhưng tôi cần bọc chuỗi sau khoảng trống trước đó của mỗi 30 ký tự, giống như dòng đầu tiên, nó sẽ là:

A very long string

Và dòng thứ 2 sẽ

containing many 

Hãy đưa ra một số giải pháp thích hợp.

Trả lời

13

Sử dụng lastIndexOf thay vì indexOf, ví dụ:

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

này sẽ cho kết quả sau:

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

Cảm ơn trả lời của bạn. Trong ví dụ này, nếu tôi cung cấp chuỗi là "Một chuỗi rất dài122222222222222222222222222222 chứa nhiều từ và ký tự. Các dòng mới sẽ được nhập vào dấu cách." sau đó đặt ra không phải là gói sau khi từ dài ("string122222222222222222222222222222") và bất ngờ gói trước khi từ dài. – Suvonkar

+0

@Suvonkar: Bạn có thể xem xét nguồn của [WordUtils.wrap()] (http://kickjava.com/src/org/apache/commons/lang/WordUtils.java.htm) và thực hiện theo cách bạn muốn. – Emil

1

Bạn có thể thử như sau:

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

gọi như 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(); 
        } 
       } 
      } 
     } 
    } 

điều này có thể là một cách để đạt được

0

"\ n" tạo một từ ngữ.

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

này sẽ giải quyết vấn đề của bạn

Các vấn đề liên quan