java 如何包装一个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10990919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to wrap a string
提问by sheetal_r oswal
I have a very long string like "The event for mobile" they can be any length strings ,i want to show .... after say certain length ,how to do this ?
我有一个很长的字符串,比如“移动事件”,它们可以是任何长度的字符串,我想显示......在说一定长度之后,如何做到这一点?
回答by Subhrajyoti Majumder
Use Apache Common library's WordUtilsclass.
使用 Apache Common 库的WordUtils类。
static String wrap(java.lang.String str,
int wrapLength, java.lang.String newLineStr, boolean wrapLongWords)
example -
例子 -
String str = "This is a sentence that we're using to test the wrap method";
System.out.println("Original String 1:\n" + str);
System.out.println("\nWrap length of 10:\n" + WordUtils.wrap(str, 10));
System.out.println("\nWrap length of 20:\n" + WordUtils.wrap(str, 20));
System.out.println("\nWrap length of 30:\n" + WordUtils.wrap(str, 30));
回答by Pramod Kumar
You can do something like this -
你可以做这样的事情 -
String str = "The event for mobile is here";
String temp = "";
if(str !=null && str.length() > 10) {
temp = str.substring(0, 10) + "...."; // here 0 is start index and 10 is last index
} else {
temp = str;
}
System.out.println(temp);
output would be - The event ....
输出将是 - 事件 ....
回答by fardjad
If you want full words then do this:
如果您想要完整的单词,请执行以下操作:
public static void shortenStringFullWords(String str, int maxLength) {
StringBuilder output = new StringBuilder();
String[] tokens = str.split(" ");
for (String token: tokens) {
if (output.length() + token.length <= maxLength - 3) {
output.append(token);
output.append(" ");
} else {
return output.toString().trim() + "...";
}
}
return output.toString().trim();
}
回答by Felix Christy
You can do this like this
你可以这样做
String longString = "lorem ipusum ver long string";
String shortString = "";
int maxLength = 5;
if(longString != null && longString.length() > maxLength) {
shortString = longString.substring(0,maxLength - 1)+"...";
}
Here, you can change maxLength to your desired number.
在这里,您可以将 maxLength 更改为您想要的数字。
回答by Raghu Nagaraju
String myString ="my lengthy string";
int startIndex=0, endIndex=myString.length(), lengthLimit = 10;
while(startIndex<endIndex) {
System.out.println(myString.substring(startIndex, startIndex+lengthLimit));
startIndex = startIndex+lengthLimit+1;
}
回答by mprabhat
String template = "Hello I Am A Very Long String";
System.out.println(template.length() > 10 ? template.substring(0, 10) + "..." : template);
You can just look at the length and then do a substring
.
你可以只看长度,然后做一个substring
.
Or if you can use Apache's common Util then use this WordUtils.wrap().
或者,如果您可以使用 Apache 的通用 Util,则使用此WordUtils.wrap()。
I didn't do a good search earlier, this SO Postis exactly what you want
我之前没有好好搜索,这篇 SO Post正是你想要的