Java字符串处理

1 字符串截取

1.1substring(int beginIndex)

提取从索引位置开始至结尾处的字符串部分

1.2 substring(int beginIndex,int endIndex)

截取的字符串中包括起始索引对应的字符;endIndex 表示结束索引,截取的字符串中不包括结束索引对应的字符

 public static void main(String[] args) {
        // 下标从0开始
        String str = "Java";
        System.out.println("substring(2)结果:"+str.substring(2)); // 从下标为2的字符串开始截到最后
        System.out.println("substring(0,1)结果:"+str.substring(0,1));
        System.out.println("substring(0,4)结果:"+str.substring(1,4));
    }
substring(2)结果:va
substring(0,1)结果:J
substring(0,4)结果:ava

2 分割字符串

2.1 str.split(String sign)

2.2 str.split(String sign,int limit)

str 为需要分割的目标字符串。
sign 为指定的分割符,可以是任意字符串。
limit 表示分割后生成的字符串的限制个数,如果不指定,则表示不限制,直到将整个目标字符串完全分割为止

    public static void main(String[] args) {
        String family = "Grandpa,grandma,Dad,Mum,Baby";
        String[] arr1 = family.split(","); // 不限制元素个数
        String[] arr2 = family.split(",", 3); // 限制元素个数为3
        System.out.println("所有家庭成员为:");
        for (int i = 0; i < arr1.length; i++) {
            System.out.println(arr1[i]);
        }
        System.out.println("前三组家庭成员为:");
        for (int j = 0; j < arr2.length; j++) {
            System.out.println(arr2[j]);
        }
    }
所有家庭成员为:
Grandpa
grandma
Dad
Mum
Baby
前三组家庭成员为:
Grandpa
grandma
Dad,Mum,Baby

3 字符串比较(4种方法)

3.1 equals()

equals() 方法将逐个地比较两个字符串的每个字符是否相同

3.2 equalsIgnoreCase()

equalsIgnoreCase() 方法的作用和语法与 equals() 方法完全相同,唯一不同的是 equalsIgnoreCase() 比较时不区分大小写

    public static void main(String[] args) {
        String str1 = "abc";
        String str2 = new String("abc");
        String str3 = "ABC";
        System.out.println(str1.equals(str2)); // 输出 true
        System.out.println(str1.equals(str3)); // 输出 false
        System.out.println(str1.equalsIgnoreCase(str3)); // 输出 true
    }

3.3 equals()与==的比较

equals() 方法比较字符串对象中的字符,而==运算符比较两个对象引用看它们是否引用相同的实例

    public static void main(String[] args) {
        String s1 = "java";
        String s2 = new String(s1);
        System.out.println(s1.equals(s2)); // 输出true
        System.out.println(s1 == s2); // 输出false
    }

3.4 compareTo() 方法

compareTo() 方法用于按字典顺序比较两个字符串的大小,该比较是基于字符串各个字符的 Unicode 值

public static void main(String[] args) {
        String str = "J";
        String str1 = "j";
        System.out.println("str.compareTo(str1)的结果是:" + str.compareTo(str1));
        System.out.println("str1.compareTo(str)的结果是:" + str1.compareTo(str));
        System.out.println("str1.compareTo('a')的结果是:" + str1.compareTo("j"));
    }
str.compareTo(str1)的结果是:-32
str1.compareTo(str)的结果是:32
str1.compareTo('a')的结果是:0

4 String字符串和整型int的相互转换

4.1 String 字符串转整型 int

Integer.parseInt(str)
Integer.valueOf(str).intValue()

    public static void main(String[] args) {
        String str = "123";
        System.out.println("Integer.parseInt(str) : " + Integer.parseInt(str));
        System.out.println("Integer.valueOf(str).intValue() : " + Integer.valueOf(str).intValue());
    }
Integer.parseInt(str) : 123
Integer.valueOf(str).intValue() : 123

4.2 int转换为String

String s = String.valueOf(i);
String s = Integer.toString(i);
String s = “” + i;

    public static void main(String[] args) {
        int num = 10;
        System.out.println("String.valueOf(num):" + String.valueOf(num));
        System.out.println("Integer.toString:" + Integer.toString(num));
        String str3 = num + "";
        System.out.println("str3:" + str3);
    }
String.valueOf(num):10
Integer.toString:10
str3:10

5 去除字符串中的空格

public static void main(String[] args) {
        String str = " hello ";
        System.out.println(str.length());    // 输出 7
        System.out.println(str.trim().length());    // 输出 5
    }

6 字符串大小写转换

6.1 toLowerCase()

将字符串中的字母全部转换为小写,非字母不受影响

6.2 toUpperCase()

将字符串中的字母全部转换为大写,非字母不受影响

public static void main(String[] args) {
        String en = "Snow White";
        System.out.println("原字符串:"+en);
        System.out.println("使用 toLowerCase() 方法之后为:"+en.toLowerCase());
        System.out.println("使用 toUpperCase() 方法之后为:"+en.toUpperCase());
    }
原字符串:Snow White
使用 toLowerCase() 方法之后为:snow white
使用 toUpperCase() 方法之后为:SNOW WHITE

7 字符串替换

7.1 replace() 方法

replace() 方法用于将目标字符串中的指定字符(串)替换成新的字符(串)

    public static void main(String[] args) {
        String words = "hello java";
        System.out.println("原始字符串是'"+words+"'");
        System.out.println("replace(\"l\",\"D\")结果:"+words.replace("l","D"));
        System.out.println("replace(\"hello\",\"你好\")结果:"+words.replace("hello","你好 "));
    }
原始字符串是'hello java'
replace("l","D")结果:heDDo java
replace("hello","你好")结果:你好  java

7.2 replaceFirst()

将目标字符串中匹配某正则表达式的第一个子字符串替换成新的字符串

    public static void main(String[] args) {
        String words = "hello Dad,hello Mum";
        String newStr = words.replaceFirst("hello","你好 ");
        System.out.println(newStr);    // 输出:你好 Dad,hello Mum
    }

7.3 replaceAll()

public static void main(String[] args) {
        String words = "hello Dad,hello Mum";
        String newStr = words.replaceAll("hello","你好 ");
        System.out.println(newStr);    // 输出:你好 Dad,你好  Mum
    }

8 查找字符串

8.1 indexOf() 方法

indexOf() 方法用于返回字符(串)在指定字符串中首次出现的索引位置,如果能找到,则返回索引值,否则返回 -1

    public static void main(String[] args) {
        String s = "Hello Java";
        System.out.println(s.indexOf('v')); // 输出结果为8
        System.out.println(s.indexOf('m')); // 找不到字母m的位置,输出结果为-1
    }

8.2 lastlndexOf() 方法

lastIndexOf() 方法用于返回字符(串)在指定字符串中最后一次出现的索引位置,如果能找到则返回索引值,否则返回 -1

    public static void main(String[] args) {
        String words="Friday,Sunday";
        System.out.println("原始字符串是'"+words+"'");
        System.out.println("lastIndexOf(\"day\")结果:"+words.lastIndexOf("day")); // lastIndexOf("day")结果:10
    }

9 StringBuffer 类

9.1 创建StringBuffer

StringBuffer() 构造一个空的字符串缓冲区,并且初始化为 16 个字符的容量。
StringBuffer(int length) 创建一个空的字符串缓冲区,并且初始化为指定长度 length 的容量。
StringBuffer(String str) 创建一个字符串缓冲区,并将其内容初始化为指定的字符串内容 str,字符串缓冲区的初始容量为 16 加上字符串 str 的长度。

  public static void main(String[] args) {
        StringBuffer str1 = new StringBuffer();
        StringBuffer str2 = new StringBuffer(5);
        StringBuffer str3 = new StringBuffer("我爱java");

        System.out.println(str1.capacity());    // 输出 16
        System.out.println(str2.capacity());    // 输出 5
        System.out.println(str3.capacity());    // 输出 22
    }

9.2 追加字符串 append()

    public static void main(String[] args) {
        StringBuffer buffer = new StringBuffer("hello,");
        String str = "World!";
        buffer.append(str);
        System.out.println(buffer);    // 输出:Hello,World!
    }

9.3 替换字符 setCharAt()

    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("hello");
        sb.setCharAt(1,'E');
        System.out.println(sb);    // 输出:hEllo
    }

9.4 删除字符串

    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("She");
        sb.deleteCharAt(2);
        System.out.println(sb);    // 输出:Sh
    }

10 String、StringBuffer和StringBuilder类的区别

线程安全:
StringBuffer:线程安全
StringBuilder:线程不安全

速度:
一般情况下,速度从快到慢为 StringBuilder > StringBuffer > String,当然这是相对的,不是绝对的。

使用环境:
操作少量的数据使用 String。
单线程操作大量数据使用 StringBuilder。
多线程操作大量数据使用 StringBuffer。
  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值