字符串函数 replace() 函数来替换单个字符。replaceFirst() 替换第一个的regex匹配项,replaceAll()替换所有的regex匹配项, String的replaceAll跟replaceFirst使用了正则表达式!
public class Test{
public static void main(String args[]){
String str="Hello World";
System.out.println( str.replace( 'l','q' ) );
System.out.println( str.replaceFirst("Hello", "Hi") );
System.out.println( str.replaceAll("ll", "o") );
System.out.println( str.replaceAll(".", "o") );
}
}
}
/* 输出结果:
Heqqo Worqd
Hi World
Heoo World
ooooooooooo
*/ 字符串函数 substring() 函数来提取字符串中介于两个指定下标之间的字符
public class Test{
public static void main(String args[]) {
String str = "this is Java String";
System.out.println(removeStr(str, 4));
}
public static String removeStr(String s, int pos) {
return s.substring(0, pos) + s.substring(pos + 4);
}
}
/* 输出结果:thisJava String */ String 类的 indexOf() 方法在字符串中查找子字符串出现的位置,如果存在返回下标
public class Test{
public static void main(String[] args) {
String str = "Best wish for lx";
int intIndex = str.indexOf("lx");
if(intIndex == - 1){
System.out.println("没有找到字符串");
}else{
System.out.println("字符串位置 " + intIndex);
}
}
}
/* 输出结果:字符串位置 14 */ 使用 split(string) 方法通过指定分隔符将字符串分割为数组
public static void main(String args[]){
String str = "www-lxacy-com";
String[] temp = str.split("-");
for (String x:temp){
System.out.println(x);
}
String str1 = "www.lxacy.com";
String[] temp1 = str1.split("\\."); // 分割字符串
for(String x : temp1){
System.out.println(x);
}
}也可以使用 StringTokennizer 设置不同分割符来分隔字符串 默认的分割符是:空格、制表符(\t)、换行符(\n)、回车符(\r)
import java.util.StringTokenizer;
public class Test{
public static void main(String args[]){
String str = "www lxacy ,com";
StringTokenizer st = new StringTokenizer(str);
while (st.hasMoreElements()) {
System.out.println(st.nextElement());
}
StringTokenizer st2 = new StringTokenizer(str, ",");
while (st2.hasMoreElements()) {
System.out.println(st2.nextElement());
}
}
}
/* 输出结果:
www lxacy ,com
www lxacy com
*/ 使用StringBuffer类的反转函数 reverse() 将字符串反转
public class Test{
public static void main(String[] args){
String string="bset lx";
String reverse = new StringBuffer(string).reverse().toString();
System.out.println("字符串反转后:"+reverse);
}
}
/* 输出结果:字符串反转后:xl tesb */ 字符串函数 :
比较两个字符串,并返回字符串中第一个字母ASCII的差值。
public class Test{
public static void main(String args[]){
String str1 = "Hello World";
String str2 = "hello world";
Object objStr = str1;
System.out.println( str1.compareTo(str2) ); //返回字符串中第一个字母ASCII的差值。
System.out.println( str1.compareToIgnoreCase(str2) ); //忽略大小写
System.out.println( str1.compareTo(objStr.toString()));
}
}
/* 输出结果: -32 0 0 */ 字符串函数 lastIndexOf(string) 来查找子字符串 string 最后一次出现的位置
public class Test{
public static void main(String[] args) {
String str = "Hello world ,Hello Lx";
int lastIndex = str.lastIndexOf("Lx");
if(lastIndex == - 1){
System.out.println("没有找到");
}else{
System.out.println("Lx 字符串最后出现的位置: "+ lastIndex);
}
}
}
/* 输出结果: 19 */ 使用了 String. toUpperCase() 方法将字符串从小写转为大写
public class Test{
public static void main(String[] args) {
String str = "string";
String strUpper = str.toUpperCase();
System.out.println("转换为大写: " + strUpper);
}
}
/* 输出结果:转换为大写: STRING */ 使用 regionMatches() 方法判断两个字符串区域是否相等。

public class Test{
public static void main(String[] args){
String str1 = "Welcome to BeiJing";
String str2 = "welcome to beijing";
boolean match1 = str1.regionMatches(true, 2, str2, 2, 3);
System.out.println("返回值:" + match1);
}
}
/* 输出结果:返回值:true */ 通过 format() 方法来格式化字符串
import java.util.*;
public class Test{
public static void main(String[] args){
double e = Math.PI;
System.out.format("%f%n", e);
System.out.format(Locale.CHINA , "%-10.4f%n%n", e);
}
}
/* 输出结果:
3.141593
3.1416
*/