字符串处理

  1. 创建字符串
String str = "hello word";
String str2 = new String("hello word");
char[] arry = {'a', 'b', 'c'};
String str3 = new String(arry);

String是字符串变量的类型,字符串要用双引号括起来。

2.字符串比较

String str = "hello";
String str2 = "hello";
System.out.println(str1 == str2);//true

String str3 = new String("hello");
String str4 = new String("hello");
System.out.println(str3 == str4);//false

如"hello"这样带双引号的字面值常量,都存放在字符串常量池中。如果用new这样的方式,会在常量池中存放一份,再在堆上另外开辟空间来存储"hello",也就是说,存储了两份"hello"。此处的==是用来比较身份的。str1和str2都引用的是字符串常量池中的"hello ",所以比较的引用(地址)相同,而str3和str4中虽然都有常量池的"hello"生成,但比较的是堆中开辟新空间的"hello"的身份,所以引用(地址)是不同的。
Java中比较字符串的内容要用equals方法。

String str1 = new String("hello");
String str2 = new String("hello");
System.out.println(str1.equals(str2));//true

String str3 = new String("hello");
System.out.println("hello".equals(str3));//推荐
System.out.println(str3.equals("hello"));//str3为null时会空指针异常

3.字符串常量池
String类的设计使用了共享设计模式:JVM底层自动维护一个对象池(字符串常量池)。
3.1 直接赋值

String str2 = "hello";

直接赋值时会进行String类的对象实例化,那么这个实例化对象自动保存到对象池中。如果下次继续使用直接赋值,若池中有内容,将直接引用。若没有,则开辟新的字符串对象再保存于对象池中。
3.2 构造方法

String str3 = new String("hello");

先在池中生成"hello",再在堆上新开辟一块空间生成"hello",str3指向新开辟的"hello"。

4.字符串不可变
String 为public final class String 类,是常量,不可改变。

String str = "hello";
str = str + " world";
str = str + "!!!";
System.out.println(str);

输出结果为:hello world!!!
这里只是str引用到了其他的对象。

5.字符,字节和字符串
5.1字符与字符串

public static void main(String[] args) {
        String str = "helloword";
        char[] data = str.toCharArray(); //拆分成字符
        for (int i = 0; i < data.length; i++){
            System.out.print(data[i] + " ");
        }
        System.out.println();
        System.out.println(new String(data));//合成字符串
        System.out.println(Arrays.toString(data));
//        h e l l o w o r d
//        helloword
//        [h, e, l, l, o, w, o, r, d]
}

判断一个字符串是否都是由数字组成

public static boolean isNumber(String str){
    char[] arry = str.toCharArray();
    for (int i = 0; i < arry.length; i++){
        if (arry[i] < '0' || arry[i] > '9'){
            return false;
        }
    }
    return true;
}

5.2字节与字符串

public static void main(String[] args) throws UnsupportedEncodingException {
    String str = "helloworld";
    byte[] data = str.getBytes();
    for (int i = 0; i < data.length; i++){
        System.out.print(data[i] + " ");
    }
    System.out.println();
    System.out.println(new String(data));
    String string2 = "中";
    byte[] bytes2 = string2.getBytes("unicode");
    System.out.println(Arrays.toString(bytes2));
}

6.字符串常见操作
6.1 字符串比较
区分大小写 str1.equals(str2); boolean类型
不区分大小写 str1.equalsIgnoreCase(str2);boolean类型
比较字符大小 “A”.compareTo(“a”);//-32 int类型
6.2 字符串查找和拆分

public static void main(String[] args) {
    String str = "helloworld";
    System.out.println(str.contains("el"));//是否包含某字符
    System.out.println(str.indexOf("ll")); //查找给定字符下标,从0号开始找
    System.out.println(str.indexOf("l",5));//从5号开始找

    String str1 = " hello world hello Java ";
    String[] result = str1.split(" ");//按空格拆分
    System.out.println(Arrays.toString(result));
    for (String s : result){
        System.out.print(s + "- -");
    }
    System.out.println();

    String str3 = "name=zhangsan&age=18";
    String[] result1 = str3.split("&");
    for (int i = 0; i < result1.length; i++){
        String[] temp = result1[i].split("=");
        System.out.println(temp[0] + " = " + temp[1]);
    }

    String str5 = "hello  world 8*(";
    System.out.println(str.toUpperCase());
}

6.3 字符串替换

String str = "helloworld" ;
System.out.println(str.replaceAll("l", "_"));
System.out.println(str.replaceFirst("l", "_"));

只是产生一个新的字符串,并不是修改。
6.4 字符串截取都是String类型,从下标开始/从下标开始–下标前一个

str = "helloworld";
System.out.println(str.substring(5));//world
System.out.println(str.substring(0,5));//hello

6.5 字符串其他操作

String str = " hello world " ;
System.out.println("["+str+"]");//[ hello world ]
System.out.println("["+str.trim()+"]");//[hello world] 删除字符串开头和结尾的空格

String str = " hello$%world 哈哈 " ;
System.out.println(str.toUpperCase());// HELLO$%WORLD 哈哈 --转大写
System.out.println(str.toLowerCase());// hello$%world 哈哈 --转小写
String str1 = " hello$%world 哈哈 " ;
System.out.println(str1.length());//17--字符长度

System.out.println("hello".isEmpty());//f--是否为空字符串 boolean类型.空字符串长度为0
System.out.println("".isEmpty());//t
System.out.println(new String().isEmpty());//t

小实例1:首字母大写

public static void main(String[] args) {
    String str = "yuisama";
    System.out.println(str);
    String str2 = str.substring(0,1).toUpperCase() + str.substring(1);//截取
    System.out.println(str2);
}

小实例2:字符串反转

public class test { //反转
    public static void main(String[] args) {
        String s = "abcdef";
        String ret = reverse(s);
        System.out.println(ret);
    }
    private static String reverse(String str){
        char[] arry = str.toCharArray();
        int i = 0;
        int j = arry.length - 1;
        while (i < j){
            char tp = arry[i];
            arry[i] = arry[j];
            arry[j] = tp;
            i++;
            j--;
        }
        return new String(arry);
        //return String.copyValueOf(value);
    }
}

小实例3:abcdefg ==> defgabc 前3个后移
(1)cbadefg前3个反转
(2)cbagfed后4个反转
(3)defgabc整体反转

import java.util.Scanner;
public class test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int n = scanner.nextInt();
            String str = scanner.next();
            String s = reverseS(str,n);
            System.out.println(s);
        }
    }
    private static String reverseS(String str, int x){
        int s1 = 0;
        int e1 = x - 1;
        int s2 = x;
        int e2 = str.length() - 1;
        str = reverse(str,s1,e1);
        str = reverse(str,s2,e2);
        str = reverse(str,s1,e2);
        return str;
    }
    private static String reverse(String st,int i,int j){
        char[] arr = st.toCharArray();
        while (i < j){
            char q = arr[i];
            arr[i] = arr[j];
            arr[j] = q;
            i++;
            j--;
        }
        return String.copyValueOf(arr);
    }
}

7.StringBuffer和StringBuilder
对于String类型来说,String的常量一旦声明就不可改变,如果改变对象内容,改变的只是其引用的指向。为了方便字符串的修改,提供StringBuffer和StringBuilder。

public static void main(String[] args) {
    StringBuffer stringBuffer = new StringBuffer("abcdef");
    System.out.println(stringBuffer);//abcdef
    String s = stringBuffer.toString();//转为字符串 abcdef
    System.out.println(s);
    stringBuffer.append("world").append(999);//加入新字符
    System.out.println(stringBuffer);//abcdefworld999
}

StringBuffer字符串反转,删除,插入

StringBuffer sb = new StringBuffer("abcdef");
System.out.println(sb.reverse());//fedcba--StringBuffer自带反转
StringBuffer sb1 = new StringBuffer("helloworld");
System.out.println(sb1.delete(5, 10).insert(0, "你好"));//你好hello--删除再插入

StringBuffer采用同步处理,属于线程安全操作;而StringBuilder采用异步处理,属于线程不安全操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值