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 红包祝福语简短10字
18 周六早晨祝福语简短