concat(),replace()和trim()Java中的字符串。
concat()String类的方法将一个String追加到另一个的末尾。该方法返回一个String,并将传递给该方法的String的值附加到String的末尾,用于调用此方法。
示例
public class Test {
public static void main(String args[]) {
String s = "Strings are immutable";
s = s.concat(" all the time");
System.out.println(s);
}
}输出结果
Strings are immutable all the time
replace()String类的此方法返回一个新字符串,该字符串是用newChar替换此字符串中所有出现的oldChar的结果。
示例
public class Test {
public static void main(String args[]) {
String Str = new String("Welcome to Nhooo.com");
System.out.print("返回值:" );
System.out.println(Str.replace('o', 'T'));
System.out.print("返回值:" );
System.out.println(Str.replace('l', 'D'));
}
}输出结果
返回值:WelcTme tT TutTrialspTint.cTm 返回值:WeDcome to TutoriaDspoint.com
trim()String类的此方法返回字符串的副本,其中省略了前导和尾随空格。
示例
import java.io.*;
public class Test {
public static void main(String args[]) {
String Str = new String(" Welcome to Nhooo.com ");
System.out.print("返回值:" );
System.out.println(Str.trim() );
}
}输出结果
返回值:Welcome to Nhooo.com
热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短