零基础学习Java 06

目录

String

 String构造方法

字符串查找

字符串截取 

 字符串替换

 字符串拆分

字符串修改 


String

String类在java.lang包下,所以使用的时候不需要导包。
 

 String构造方法

字符串查找

char charAt(int index),输入位置index,找单个字符

    public static void main(String[] args) {
        String s1 = "hello";
        char ch = s1.charAt(1);
        System.out.println(ch);//e
    }

int indexOf(int ch)   ,返回ch字符第一次出现的位置下标,没有就返回-1

        int index = s1.indexOf('l');
        System.out.println(index);//2

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

        int index = s1.indexOf('l',4);
        System.out.println(index);//-1
 
        int index1 = s1.indexOf('l',3);
        System.out.println(index1);//3

int indexOf(String str,int formIndex),从formIndex开始,找Str字符串第一次出现的位置,返回其字符串首字母下标,没有返回-1

    public static void main(String[] args) {
        String s2 = "helloxawllxhxawllxh";
 
        int index2 = s2.indexOf("xawl",6);
        System.out.println(index2);//12
    }

int indexOf(String str,int formIndex),从formIndex开始,找Str字符串第一次出现的位置,返回其字符串首字母下标,没有返回-1

    public static void main(String[] args) {
        String s2 = "helloxawllxhxawllxh";
 
        int index2 = s2.indexOf("xawl",6);
        System.out.println(index2);//12
    }

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

    public static void main(String[] args) {
        String s2 = "helloxawllxhxawllxh";
 
        int index3 = s2.lastIndexOf('a');
        System.out.println(index3);//13
    }

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

    public static void main(String[] args) {
        String s2 = "helloxawllxhxawllxh";
 
        int index3 = s2.lastIndexOf('a',7);
        System.out.println(index3);//6
    }

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

    public static void main(String[] args) {
        String s2 = "helloxawllxhxawllxh";
 
        int index4 = s2.lastIndexOf("xawl");
        System.out.println(index4);//12
    }

int laseIndexOf(String str,int formIndex),从fromIndex开始,从后往前找,返回字符串str第一次出现位置的首字母下标,没有找到返回-1

    public static void main(String[] args) {
        String s2 = "helloxawllxhxawllxh";
 
        int index4 = s2.lastIndexOf("xawl",9);
        System.out.println(index4);//5
    }

字符串截取 

从字符串中截取后面字符串的内容,通过substring,

    public static void main(String[] args) {
        String str = "adsasdasdasdasd";
        String ret = str.substring(4);
        System.out.println(ret);//sdasdasdasd
    }

如果是要截取指定部分内容,可以指定其左右下标范围,但是注意范围是不包括右的[左,右) 

    public static void main(String[] args) {
        String str = "adsasdasdasdasd";
        String ret = str.substring(4,7);//截取[4,7)里面的字符
        System.out.println(ret);//sda
    }

 字符串替换

使用replace将字符串中字符进行替换

    public static void main(String[] args) {
        String str1 = "xawlxawlxawlxawl";
        String ret = str1.replace('a','B');
        System.out.println(ret);//xBwlxBwlxBwlxBwl
        System.out.println(str1);//xawlxawlxawlxawl
    }

使用replace或replaceAll将字符串中字符串进行替换

    public static void main(String[] args) {
        String str1 = "xawlxawlxawlxawl";
        String ret = str1.replace("xa","B");
        String ret1 = str1.replaceAll("xa","B");
        System.out.println(ret);//BwlBwlBwlBwl
        System.out.println(ret1);//BwlBwlBwlBwl
        System.out.println(str1);//xawlxawlxawlxawl
    }

使用replaceFrist将字符串中字符进行替换

    public static void main(String[] args) {
        String str1 = "xawlxawlxawlxawl";
 
        String ret1 = str1.replaceFirst("xa","B");
        System.out.println(ret1);//Bwlxawlxawlxawl
    }

 字符串拆分

可以将一个完整的字符串按照指定的分隔符,分隔为若干个字符串,用spllit

    public static void main(String[] args) {
        String str1 = "Hello this is xawl rjgc professional";
        String[] ret = str1.split(" ");
        for (String s : ret) {
            System.out.println(s);
        }
    }

将字符串以指定的格式,拆分为limit组  

    public static void main(String[] args) {
        String str1 = "Hello this is xawl rjgc professional";
        String[] ret = str1.split(" ",3);
        for (String s : ret) {
            System.out.println(s);
        }
    }

这里还要注意,有些特殊字符(| + * . ,)作为分割符可能无法正确切分, 需要加上转义. 

        String str2 = "192.188.12.1";
        String[] ret1 = str2.split("\\.");
        for (String s1: ret1) {
            System.out.println(s1);
        }

如果是一个字符串中有多个分隔符,那么用 | 作为连字符

        String str3 = "avasda asda&sad";
        String[] ret2 = str3.split(" |&");
        for (String s2: ret2) {
            System.out.println(s2);
        }

字符串修改 

        long start = System.currentTimeMillis();
        String s = "" ;
        for (int i = 0; i < 100000; ++i) {
            s += i;
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值