concat()、replace() 和 trim() Java 字符串。
String 类的 concat() 方法将一个字符串追加到另一个字符串的末尾。该方法返回一个字符串,其中包含传递给该方法以及用于调用此方法的字符串末尾附加的字符串的值。
示例
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