Java字符串操作

举报
冬晨夕阳 发表于 2022/03/30 01:11:36 2022/03/30
【摘要】 java字符串操作 字符串替换字符串切片字符串查找字符串分割字符串反转字符串比较首字母的ASCII差值查找字符串最后一次出现的位置字符串小写转大写判断两个字符串区域是否相等字符串格式化 ...


字符串替换

字符串函数 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
*/ 

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

字符串切片

字符串函数 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   */ 

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

字符串查找

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 */ 

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

字符串分割

使用 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);
        }
    }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

也可以使用 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 
*/ 

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

字符串反转

使用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 */ 

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

字符串比较首字母的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    */ 

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

查找字符串最后一次出现的位置

字符串函数 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   */ 

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

字符串小写转大写

使用了 String. toUpperCase() 方法将字符串从小写转为大写

public class Test{
    public static void main(String[] args) {
        String str = "string";
        String strUpper = str.toUpperCase();
        System.out.println("转换为大写: " + strUpper);
    }
}
/* 输出结果:转换为大写: STRING  */ 

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

判断两个字符串区域是否相等

使用 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  */ 

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

字符串格式化

通过 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  
*/ 

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

文章来源: blog.csdn.net,作者:考古学家lx,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_43582101/article/details/106538796

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。