Java字符串操作


字符串替换

字符串函数 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差值

字符串函数 :

  • compareTo (string)
  • compareToIgnoreCase(String)
  • compareTo(object string)

比较两个字符串,并返回字符串中第一个字母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() 方法判断两个字符串区域是否相等。
在这里插入图片描述

  • 第一个参数,ignoreCase=True表示忽略大小写区别
  • 第二个参数,toffset 表示将 str1 字符串从第2个字符开始和str2比较
  • 第三个参数,String 是要进行比较的字符串str2
  • 第四个参数,ooffset 表示从str2的第2个字符开始比较
  • 第五个参数,len 表示匹配的位数
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  
*/ 
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

考古学家lx(李玺)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值