Java中关于String(字符串)一系列的用法

注:下标从0开始

String toUpperCase():字符串全部转成大写

 代码如下:

;public class Test {
    public static void main(String[] args) {
        String dd="uppercase";
        String large=dd.toUpperCase();
        System.out.println(large);
    }
}

 效果:
在这里插入图片描述

String toLowerCase():字符串全部转成小写

 代码如下:

public class Test {
    public static void main(String[] args) {
        String dd="UPPERCASE";
        String little=dd.toLowerCase();
        System.out.println(little);
    }
}

 效果:在这里插入图片描述

charAt() :用于返回指定索引处的字符。索引范围为从 0 到 length() – 1public class Test {

 代码如下:

  public static void main(String[] args) {
        String dd="uppercase";
        char lie=dd.charAt(5);
        System.out.println(lie);
    }
}

 效果:在这里插入图片描述

.equals():是区分大小写,只能是相同的数字字母比较,返回值是true或false

 例:

        ccdPwd.equals(Pwd)

equalsIgnoreCase():是来比较字母的长度和字符是否相同,且不区分大小写,返回值是true或false

 例:

       ccdPwd.equalsIgnoreCase(Pwd)

indexOf() :可返回某个指定的字符串值在字符串中首次出现的位置,对字符串大小写敏感,没有则返回-1

 例:

public class Test {
    public static void main(String[] args) {
    String str="521654364572";
    //求4首次出现的下标位置
    int index=str.indexOf("4");
    System.out.println("4下标为:"+index);
    //求7首次出现下标位置,且从下标的5位开始
    int index1= str.indexOf("7",5);
    System.out.println("7下标为:"+index1);
}

 效果:在这里插入图片描述

lastIndexOf():可返回某个指定的字符串值在字符串中最后出现的位置,对字符串大小写敏感,没有则返回-1

 代码如下:

public class Test {
    public static void main(String[] args) {
    String str="52167543645723";
    //求4最后一次出现的下标位置
    int index=str.lastIndexOf("4");
    System.out.println("4下标为:"+index);
    //求7最后一次出现下标位置,且从后面下标的第10位开始,从右往左
    int index1=str.lastIndexOf("7",10);
    System.out.println("7下标为:"+index1);
}
}

 效果:
在这里插入图片描述

substring():提取字符串中介于两个指定下标之间的字符

  • 1.substring(int beginIndex)

   返回值:从起始位置(beginIndex)至字符串末尾的字符串

   str.substring(2)

  • 2.substring(int beginIndex, int endIndex)

   返回值:从起始位置(beginIndex)到目标位置(endIndex)之间的字符串,但不包含目标位置(endIndex)的字符

   str.substring(2,4);
 
 代码如下:

 public class Test {
   public static void main(String[] args) {
   String str="52167543645723";
   //从起始位置(包括本身)至字符串末尾的字符串,也就是从下标为2的那个字符串到末尾之间的字符串
   String method1=str.substring(2);
   System.out.println("字符串为:"+method1);
   //从起始位置(包括本身)到目标位置之间的字符串,但不包含目标位置的字符,也就是从下标为2的那个字
   //符串到下标为10的那个字符串之间的字符串
    String method2=str.substring(2,10);
    System.out.println("字符串为:"+method2);
}
}

 效果:
在这里插入图片描述

length():返回此对象表示的字符序列的长度

 代码如下:

public class Test {
    public static void main(String[] args) {
    String str="String length";
    int index=str.length();
    System.out.println("字符串的长度为:"+index);
}
}

 效果:
在这里插入图片描述

trim():去除字符串首尾字符的空格,但不会去除字符与字符之间的空格

 代码:

public class Test {
    public static void main(String[] args) {
    String str="   string     ";
    //去除之前
    System.out.println("去除之前:"+str);
    //去除之后
    String userStr=str.trim();
    System.out.println("去除之后:"+userStr);
}
}

 效果:
在这里插入图片描述

split() 方法:把一个 字符串 分割成 字符串数组 ,不包括分割对象本身

 代码:
public class Pslit {

    public static void main(String[] args) {
        String str="我爱中华,中华爱我,我爱民族,民族爱我";
        String str1[]=str.split(",");
        for (int i = 0; i <str1.length; i++) {
            System.out.println(str1[i]+"\n");
        }
    }
}
               

 效果:
在这里插入图片描述

toString():返回此对象本身(它已经是一个字符串)

 代码:

     public static void main(String args[]) {
            String Str = new String("wo,shi,shei");
            System.out.print("返回值 :" );
            System.out.println( Str.toString() );
        }
    }

 效果:在这里插入图片描述

insert():表示在字符串中插入字符串

 例:StringBuffer insert(int index,String str)
    int index:从此位置开始插入,为下标位置(数字)
    String str:插入的字符串
 代码如下:

public class Pslit {
        public static void main(String args[]) {
            //插入前
            StringBuffer str=new StringBuffer("我爱中华");
            System.out.println("插入前:"+str);
            //插入后
            str.insert(4,",中华爱我");
            System.out.println("插入后:"+str.toString());
        }
    }

StringBuffer:是一个可变对象,当对他进行修改的时候不会像String那样重新建立对象
 效果:
在这里插入图片描述

append():其实就是往字符串数组里添加字符串

 代码:

public class Pslit {
    public static void main(String[] args) {
        StringBuffer str = new StringBuffer("Hello");
        str.append("World");
        System.out.println(str);
    }
    }

 效果:
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值