【String类的常用方法】


字符串构造

public class Test {
    public static void main(String[] args) {

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

        String str2 = new String("hehehe");
        System.out.println(str2);

        char[] array = {'a','b','c'};
        String str3 = new String(array);
        System.out.println(str3);
    }
}
public class Test {
    public static void main(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("world");
        String s3 = s1;
        System.out.println(s3);

        String s4 = "";//存储0
        System.out.println(s4.length());
        System.out.println(s4.isEmpty());

        String s5 = null;//不存任何数
        System.out.println(s5.length());
        System.out.println(s5.isEmpty());
    }
  1. String是引用类型,内部并不存储字符串本身,
    在这里插入图片描述

String对象的比较

1.== //比较地址
2. equals //比较值相不相等
3.compareTo //比较值的大小
4.compareToIgnoreCase//忽略大小写比较

public class Test {
    public static void main(String[] args) {
        String s1 ="Student";
        String s2 ="Student";
        System.out.println(s1==s2);//比较值

        String s3 = new String("JAVA");
        String s4 = new String("JAVA");
        System.out.println(s3 == s4);//比较地址一样不一样
        System.out.println(s3.equals(s4));//比较值

        String s5 ="abc";
        String s6 ="adc";
        //s5>s6 返回正数
        //s5==s6 返回0
        //s5<s6 返回负数
        System.out.println(s5.compareTo(s6));


        //忽略大小写比较
        String s7 ="abc";
        String s8 ="ABC";
        System.out.println(s7.compareToIgnoreCase(s8)); //0

    }
    public static void main3(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("world");
        String s3 = s1;
        System.out.println(s3);//hello

        String s4 = "";//存储0
        System.out.println(s4.length());
        System.out.println(s4.isEmpty());

        String s5 = null;//不存任何数
       // System.out.println(s5.length());
        System.out.println(s5.isEmpty());
    }

    public static void main2(String[] args) {

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

        String str2 = new String("hehehe");
        System.out.println(str2);

        char[] array = {'a','b','c'};
        String str3 = new String(array);
        System.out.println(str3);//abc
    }
  }
}

字符串查找

charAt

找到字符串某个字符的下标的元素

public class Test {

    public static void main(String[] args) {

        //charAt
        String s1 ="English";
        char ch = s1.charAt(3);//3下标的值
        System.out.println(ch);

        //遍历字符串
        for (int i = 0; i <s1.length() ; i++) {
            char sh = s1.charAt(i);
            System.out.print(sh);
        }
    }
}

indexof

int indexOf(int ch)
在字符串中 第一次出现某个字符的下标,没有返回-1

public class Test {
    public static void main(String[] args) {
        String str = "abacabc";
       int index = str.indexOf('a');//字符串中第一次出现字符a的下标
        System.out.println(index);

    }
}

int indexOf(int ch, int fromIndex)
从fromIndex位置开始找字符第一次出现的位置 没有返回-1

public class Test {
    public static void main(String[] args) {
        String str = "abacabc";
        int index = str.indexOf('a',3);
        System.out.println(index);
    }
}

int indexOf(String str)
返回字符串第一次出现的位置,没有返回-1

public class Test {
    public static void main(String[] args) {
        String str = "abacabc";
        int index = str.indexOf("abc");
        System.out.println(index);//4
    }
}

int indexOf(String str, int fromIndex)
从fromIndex位置开始找字符串第一次出现的位置,没有返回-1

public class Test {
    public static void main(String[] args) {
        String str = "acabacb";
        int index = str.indexOf("ab",2);
        System.out.println(index);
    }
}

int lastIndexOf(int ch)
从后往前找,返回字符第一次出现的位置,没有返回-1

public class Test {
    public static void main(String[] args) {
        String str = "acabacb";
        int index = str.lastIndexOf('b');
        System.out.println(index);//6
    }
}

int lastIndexOf(int ch, int fromIndex)
从fromIndex位置开始找,从后往前找字符第一次出现的位置,没有返
回-1

public class Test {
    public static void main(String[] args) {
        String str = "acabacb";
        int index = str.lastIndexOf('b',5);
        System.out.println(index);//3
    }
}

int lastIndexOf(String str)
从后往前找,返回字符串第一次出现的位置,没有返回-1

public class Test {
    public static void main(String[] args) {
        String str = "acabacb";
        int index = str.lastIndexOf("ac");
        System.out.println(index);//4
    }
}

int lastIndexOf(String str, int fromIndex)
从fromIndex位置开始找,从后往前找字符串第一次出现的位置,没有返
回-1

public class Test {
    public static void main(String[] args) {
        String str = "acabacb";
        int index = str.lastIndexOf("ac",3);
        System.out.println(index);//0
    }
}

转化

1. 数值和字符串转化

数字转字符串

public class Test {
    public static void main(String[] args) {
        //数字转字符串
        String s1 =  String.valueOf(1919);//整型
        String s2 =  String.valueOf(19.9);//浮点型
        System.out.println(s1);//变成字符串了
        System.out.println(s2);
    }

字符串转数字

public class Test {
    public static void main(String[] args) {
       int date = Integer.parseInt("198");//整型转字符串
      double date2 = Double.parseDouble("15.55");//浮点型转字符串
        System.out.println(date);
        System.out.println(date2);
    }
}

2.大小写转换 toUpperCase toLowerCase

public class Test {
    public static void main(String[] args) {
        String s1 = "hello";
        System.out.println(s1.toUpperCase());//HELLO
        
        String s2 = "WORLD";
        System.out.println(s2.toLowerCase());//world
    }

3.字符串转数组 toCharArray

public class Test {
    public static void main(String[] args) {
        String s1 = "hello";
        char[] array = s1.toCharArray();
        System.out.println(Arrays.toString(array));//Arrays.toString( )是打印数组的
    }
}

4.数组转字符串

public class Test {
    public static void main(String[] args) {
        char[] array ={'h','e','l','l','o'};
        String s1 = new String(array);
        System.out.println(s1);
    }
}

5.格式化 format

public class Test {
    public static void main(String[] args) {
        String s =String.format("%d-%d-%d",2023,11,06);
        System.out.println(s);//2023-11-06
    }

字符串替换

String replaceAll(String regex, String replacement)

替换所有的指定内容

public class Test {
    public static void main(String[] args) {
        String str = "abbaacca";
        System.out.println(str.replaceAll("b","q"));//aqqaacca
        System.out.println(str);//abbaacca
    }
}

String replaceFirst(String regex, String replacement)

替换首个内容

public class Test {
    public static void main(String[] args) {
        String str = "abbaacca";
        String s1 = str.replaceFirst("b","88");
        System.out.println(s1);

    }

注意事项: 由于字符串是不可变对象, 替换不修改当前字符串, 而是产生一个新的字符串

字符串拆分

以空格拆分

public class Test {
    public static void main(String[] args) {
        String str = "hello beautiful world";
        String [] s1 = str.split(" ");
        
        for (int i = 0; i < s1.length; i++) {
            System.out.println(s1[i]);
        }
    }

以空格分开,分成两组

public class Test {
    public static void main(String[] args) {
        String str = "hello beautiful world";
        String [] s1 = str.split(" ",2);

        for (int i = 0; i < s1.length; i++) {
            System.out.println(s1[i]);
        }
    }

特殊字符拆分

特殊字符作为分割符可能无法正确切分, 需要加上转义 \ \

public class Test {
// 拆分IP地址
    public static void main(String[] args) {
        String str = "198.153.1.2";
        String [] s1 = str.split("\\.");

        for (int i = 0; i < s1.length; i++) {
            System.out.println(s1[i]);
        }
    }

多个分隔符拆分

如果一个字符串中有多个分隔符,可以用"|"作为连字符

public class Test {
    public static void main(String[] args) {
        String str2 = "wo=he=zhangsan&zai&xuexi ";
        String [] array =str2.split("=|&");
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i]);
        }
    }
 }

字符串的截取

从一个完整的字符串之中截取出部分内容。

String substring(int beginIndex, int endIndex)

截取部分内容

public class Test {
    public static void main(String[] args) {
        String str3 = "abcaac";
        String s4 = str3.substring(0,3);
        System.out.println(s4);//abc
    }
}

String substring(int beginIndex)

从指定索引截取到结尾

public class Test {
    public static void main(String[] args) {
        String str3 = "abcaac";
        String s4 = str3.substring(3);
        System.out.println(s4);//aac
    }

其他操作

String trim()

去掉字符串中的左右空格,保留中间空格

public class Test {
    public static void main(String[] args) {
        String str4 = " abc aac ";
        String s5 = str4.trim();
        System.out.println(s5);//abc aac 
    }

String类 练习

给定一个字符串 s ,找到它的第一个不重复的字符,并返回它的索引 。如果不存在,则返回 -1 。

public class Test {
    public int firstUniqChar(String s) {
        int[] count = new int[26]; //创建一个数组
        //每遍历一个字符,就把字符对应的下标++一次
        for(int i = 0;i < s.length();i++){
            char ch = s.charAt(i);
            count[ch-'a']++;
//如果是a-a就是97-97=0,a就能在0下标位置,b-a就是97-97,b就在1下标位置,
//这个方法适用于找字符。
        }
        for(int i = 0;i < s.length();i++) {
        //再遍历一次,如果有i的下标==1,则返回i
            char ch = s.charAt(i);
            if(count[ch-'a'] == 1){
                return i;
            }
        }
        return -1;
    }

计算字符串最后一个单词的长度,单词以空格隔开
运用String类中多个库方法计算

public class Test {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();//输入
       while(in.hasNextLine()){
           int index = str.lastIndexOf(' ');//从后往前找第一个空格
           String s4 = str.substring(index+1);//截取空格后到末尾的字符串
           System.out.println(s4.length());//求字符的长度
       }
    }
  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值