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