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 生日广州男生祝福语简短