类String 常用方法

字符串当中的常用方法之比较相关的方法

  • public boolean equals (object obj):将此字符串与指定的对象进行比较(只有参数是字符串并且内容相同才会返回true)
  • public boolean  equalsIgnoreCase(String anotherString):将此字符串与指定的对象进行比较,忽略大小写。

注意点:

  • 任何对象都可以用object接收
  • equals方法具有对称性,也就是a.equals(b) 等价于b.equals(a)
  • 如果比较双方一个常量一个变量,推荐常量字符串写在前面
  • 只有英文字母区分大小,其他都不区分大小写

代码举例:

public class StringTest1 {


    public static void main(String[] args) {
        String a1 = "abc";
        String a2 = "abc";
        char[] charArray = {'a', 'b', 'c'};
        String a3 = new String(charArray);
        String a4 = "Abc";
        // 比较字符串对象,(字符串对象且内容完全一样)
        System.out.println(a1.equals(a2));
        System.out.println(a1.equals(a3));
        System.out.println(a3.equals(a2));
        //区分大小写
        System.out.println(a1.equals(a4));
        // 不区分大小写
        System.out.println(a1.equalsIgnoreCase(a4));

    }
}

执行结果:

字符串当中的常用方法之获取相关的方法

  • public int length():返回此字符串的长度
  • public String concat(String str):将指定的字符串连接到该字符串的末尾
  • public char charAt(int index):返回指定索引出的char值
  • public int indexOf(String str):返回指定参数字符串第一次出现在该字符串内的索引
  • public String substring(int beginIndex):返回一个子字符串,从beginIndex开始截取字符串到字符串结尾
  • public String substring (intbeginIndex, int endIndex):返回一个子字符串,从beginIndex到endIndex截取字符串。(包含beginIndex,不包含 endIndex)

代码举例:

public class StringTest1 {
    public static void main(String[] args) {
        String s = "hello world";
        //获取字符串的长度
        System.out.println(s.length());
        // 合并成为新的字符串
        System.out.println(s.concat("java"));
        //返回索引位置为5的字符char
        System.out.println(s.charAt(6));
        //返回参数字符串,第一次在字符串中出现的索引,没有就返回-1
        System.out.println(s.indexOf("l"));
        System.out.println(s.indexOf("A"));
        //返回从索引位置3到末尾的子字符串
        System.out.println(s.substring(3));
        //返回从索引位置3到索引值位置6的子字符串
        System.out.println(s.substring(3, 7));
    }
}

执行结果:

字符串当中的常用方法之转换相关的方法

  •  public char[ ] toCharArray():将此字符串转换为新的字符数组
  • public byte[ ] getBytes():使用平台的默认字符集将该String编码转换为新的字节数组
  • public String replace (CharSequence target,   CharSequence  replacement):将于target匹配的字符串,使用replacement字符串替换

代码举例:

public class StringTest1 {


    public static void main(String[] args) {
        String s1 = "acg,afg";
        //转换为字符数组
        char[] c = s1.toCharArray();
        //遍历数组
        for (int i = 0; i < c.length; i++) {
            System.out.println(c[i] + " ");
        }
        // 转换为字节数组
        byte[] b = s1.getBytes();
        // 把所有的ad,替换成为你好
        String s2 = s1.replace("af", "你好");
        System.out.println(s2);
    }
}

执行结果

字符串当中的常用方法之分割功能相关的方法

  • public  String[ ] split (String regex):将此字符串按照正则表达式的规则拆分成为字符串数组
public class StringTest1 {
    public static void main(String[] args) {
        String s1 = "1,2,3,4,5";
        // 分割字符串
        String[] s2 = s1.split(",");
        //遍历字符串数组
        for (int i = 0; i < s2.length; i++) {
            System.out.print(s2[i]);
        }
    }
}

执行结果

需求

代码实现:

import java.util.Scanner;

public class StringTest1 {
    public static void main(String[] args) {
        // 从键盘输入对象
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您的字符串");
        String s = sc.next();
        // 定义变量存储对应的总数
        int conutUpper = 0;
        int countlower = 0;
        int conutNumber = 0;
        int countOther = 0;
        // 字符串转换为数组
        char[] c = s.toCharArray();
        //遍历数组
        for (int i = 0; i < c.length; i++) {
            if ('A' <= c[i] && c[i] <= 'Z') {
                conutUpper++;
            } else if ('a' <= c[i] && c[i] <= 'z') {
                countlower++;
            } else if ('0' <= c[i] && c[i] <= '9') {
                conutNumber++;
            } else {
                countOther++;

            }
        }
        System.out.println("大写字母总数是" + conutUpper);
        System.out.println("小写字母总数是" + countlower);
        System.out.println("数字总数是" + conutNumber);
        System.out.println("其他总数是" + countOther);

    }
}

执行结果

猜你喜欢

转载自www.cnblogs.com/wurengen/p/10760235.html