拼接 (concat())、替换 (replace()) 和修整 (trim()) Java 中的字符串。
String 类中的 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
String 类中的此 replace() 方法返回一个新字符串,结果是替换此字符串中所有出现的 oldChar 到 newChar。
示例
public class Test {
public static void main(String args[]) {
String Str = new String("Welcome to Tutorialspoint.com");
System.out.print("Return Value :" );
System.out.println(Str.replace('o', 'T'));
System.out.print("Return Value :" );
System.out.println(Str.replace('l', 'D'));
}
}输出
Return Value :WelcTme tT TutTrialspTint.cTm Return Value :WeDcome to TutoriaDspoint.com
String 类中的此 trim() 方法返回一个字符串副本,其中省去了前导和尾随空白。
示例
import java.io.*;
public class Test {
public static void main(String args[]) {
String Str = new String(" Welcome to Tutorialspoint.com ");
System.out.print("Return Value :" );
System.out.println(Str.trim() );
}
}输出
Return Value :Welcome to Tutorialspoint.com
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP