J’ai ce code:
Ssortingng s = "A very long ssortingng containing " + "many many words and characters. " + "Newlines will be entered at spaces."; SsortingngBuilder sb = new SsortingngBuilder(s); int i = 0; while ((i = sb.indexOf(" ", i + 20)) != -1) { sb.replace(i, i + 1, "\n"); } System.out.println(sb.toSsortingng());
La sortie du code est la suivante:
A very long ssortingng containing many many words and characters. Newlines will be entered at spaces.
Le code ci-dessus encapsule la chaîne après l’espace suivant de 30 caractères, mais je dois envelopper la chaîne après l’espace précédent de tous les 30 caractères, comme pour la première ligne:
A very long ssortingng
Et la 2ème ligne sera
containing many
S’il vous plaît donner une solution appropriée.
Vous pouvez utiliser le fichier WordUtils.wrap () d’ Apache-common .
Utilisez lastIndexOf
au lieu de indexOf
, par exemple
SsortingngBuilder sb = new SsortingngBuilder(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());
Cela produira la sortie suivante:
A very long ssortingng containing many many words and characters. Newlines will be entered at spaces.
Vous pouvez essayer ce qui suit:
public static Ssortingng wrapSsortingng(Ssortingng s, Ssortingng deliminator, int length) { Ssortingng result = ""; int lastdelimPos = 0; for (Ssortingng 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; }
appelez wrapSsortingng (“asd xyz afz”, “\ n”, 5)
Je sais que c’est une vieille question, mais. . . Sur la base d’une autre réponse que j’ai trouvée ici, mais je ne me souviens pas du nom des affiches. Félicitations à lui pour m’avoir orienté dans la bonne direction.
public Ssortingng truncate(final Ssortingng content, final int lastIndex) { Ssortingng result = ""; Ssortingng retResult = ""; //Check for empty so we don't throw null pointer exception if (!TextUtils.isEmpty(content)) { result = content.subssortingng(0, lastIndex); if (content.charAt(lastIndex) != ' ') { //Try the split, but catch OutOfBounds in case ssortingng is an //uninterrupted ssortingng with no spaces try { result = result.subssortingng(0, result.lastIndexOf(" ")); } catch (SsortingngIndexOutOfBoundsException e) { //if no spaces, force a break result = content.subssortingng(0, lastIndex); } //See if we need to repeat the process again if (content.length() - result.length() > lastIndex) { retResult = truncate(content.subssortingng(result.length(), content.length()), lastIndex); } else { return result.concat("\n").concat(content.subssortingng(result.length(), content.length())); } } //Return the result concatenating a newline character on the end return result.concat("\n").concat(retResult);; //May need to use this depending on your app //return result.concat("\r\n").concat(retResult);; } else { return content; } }
public static void main(Ssortingng args[]) { Ssortingng s1="This is my world. This has to be broken."; SsortingngBuffer buffer=new SsortingngBuffer(); 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
cela peut être un moyen d'atteindre
“\ n” fait un wordwrap.
Ssortingng s = "A very long ssortingng containing \n" + "many many words and characters. \n" + "Newlines will be entered at spaces.";
cela résoudra votre problème