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 祝福语勤奋大全简短英语