Anybody help me please. I don't know how to append the String with format in android similar to this:
String key = "";
append("%d-%d-%d", 0, 1, 2));
Please give me an example. Thank you in advance.
Use StringBuilder or StringBuffer but with adequate initial capacity to avoid re-allocation. The default capacity is 16, so once you exceed that, the data has to be copied into a new bigger place. Use append not +.
int integer = 5;
StringBuilder s = new StringBuilder(100);
s.append("something");
s.append(integer);
s.append("more text");
You can use either StringBuffer or StringBuilder
public class Test {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Test");
sb.append(" String Buffer");
System.out.println(sb);
}
}
This produces following result:
Test String Buffer
StringBuilder:
StringBuilder strBuilder = new StringBuilder("foo");
strBuilder.append("bar");
strBuilder.append("baz");
String str = strBuilder.toString();
For format, try
String.format("Hello %1$s, your name is %1$s and the time is %2$t", name, time)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With