[Q#10990919][A#10990974] How to wrap a string
https://stackoverflow.com/q/10990919
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 ?
Answer
https://stackoverflow.com/a/10990974
You can do something like this - output would be - The event ….
APIzation
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);
package com.stackoverflow.api;
public class Human10990974 {
public static String wrap(String str, int length) {
String temp = "";
if (str != null && str.length() > length) {
temp = str.substring(0, length) + "....";
} else {
temp = str;
}
return temp;
}
}
package com.stackoverflow.api;
/**
* How to wrap a string
*
* @author APIzator
* @see <a href="https://stackoverflow.com/a/10990974">https://stackoverflow.com/a/10990974</a>
*/
public class APIzator10990974 {
public static String wrapString(String str) throws Exception {
String temp = "";
if (str != null && str.length() > 10) {
// here 0 is start index and 10 is last index
temp = str.substring(0, 10) + "....";
} else {
temp = str;
}
return temp;
}
}