开发者

Android Character insert to String

开发者 https://www.devze.com 2023-04-07 09:09 出处:网络
How can I insert \'\\n\' ( a new line) into a string, to be custom displayed on the device\'s screen ? The code :

How can I insert '\n' ( a new line) into a string, to be custom displayed on the device's screen ? The code :

String s3="";
for(int i=0; i<timp.size(); i++)
           {
s3 = s3 + sbt.get(i);
}

That's how I form the string . Now I would like to put '\n' on the s3.charAt(55)开发者_开发技巧 position . Thanks.

PS : I Don't want to replace the character on the s3.charAt(55) , I just want to add the newline there .


String yourLongString = "...A huge line of string";
String preLongStr = yourLongString.subString(0, 55);
String postLongStr =  yourLongString.subString(55);

String finalString = preLongStr + "\n" + postLongStr;


You could do it like this:

String first = s3.substring( 0, 55 );
String second = s3.substring( 56, s3.length );
s3 = first + "\n" + second;

Or change your for-loop by adding this after you append to s3 (if the string just needs to break as soon as it exceeds 55 characters.)

if( s3.length > 55 )
  s3 += "\n";


You can use StringBuilder to edit Strings.

StringBuilder total = new StringBuilder();
String first = "Hello";
String second = " World";

total.append(first);
total.append("\n");
total.append(second);
0

精彩评论

暂无评论...
验证码 换一张
取 消