Java进阶之String类

 Java进阶之String类

Java进阶之String类

在Java中,字符串属于对象,Java提供了String类来创建、操作字符串

  • 创建字符串
  • String string = "Hello String!"
  • String类是不可以改变的,一旦创建,值就不可以改变,如果要对字符串进行修改,可以选择用StringBuffer和StringBuider

字符串常用方法

  • charAt(): 返回指定位置的字符
  • compareTo(): 比较两个字符串,返回int类型值
  • concat(): 拼接
  • contains(): 判断字符串是否包含特定字符
  • equals(): 比较两个字符串,返回布尔值
  • equalsIgnoreCase(): 比较两个字符串,不区分大小写
  • startsWith() endWith(): 判断开头结尾的格式
  • hashCode(): 返回字符串的哈希值
  • indexOf(): 返回第一次出现的字符,可以设置开始查找的位置
  • lastIndexOf(): 返回最后一出现的字符
  • isEempty(): 判断字符串是否为NULL
  • substring(): 根据下标指定位置输出,可以设定结束的位置
  • toCharArray(): 转换成字符数组
  • trim(): 去掉两端空格
public class StringTest {
    public static void main(String[] args) {
        String str1 = "bilibili";
        //返回指定位置的字符
        char c = str1.charAt(1);
        System.out.println(c);

        //比较两个字符串
        String strCompareTo1 = "abc";
        String strCompareTo2 = "bcd";
        System.out.println(strCompareTo1.compareTo(strCompareTo2));

        //拼接
        String strConcat1 = "a";
        String strConcat2 = strConcat1.concat("b");
        System.out.println(strConcat2);

        //检查字符串中是否包含特定字符
        String strContains1 = "哔哩哔哩干杯";
        System.out.println(strContains1.contains("干"));



        //比较两个字符串
        String strEquals1 = "abc";
        String strEquals2 = "abc";
        System.out.println(strEquals1.equals(strEquals2));

        //比较两个字符串,不区分大小写
        String strEqualsIgnoreCase1 = "abc";
        String strEqualsIgnoreCase2 = "ABC";
        System.out.println(strEqualsIgnoreCase1.equalsIgnoreCase(strEqualsIgnoreCase2));

        //判断结尾格式
        String strEndsWith = "qwer@163";
        System.out.println(strEndsWith.endsWith("@163"));

        //判断开头格式
        String strStartWith = "www.xumouren";
        System.out.println(strStartWith.startsWith("www"));
        

        //返回字符串哈西码
        String strHashCode = "abc";
        System.out.println(strHashCode.hashCode());


        //返回指定字符的第一次出现的位置
        String strIndexOf1 = "abcdeabcde";
        System.out.println(strIndexOf1.indexOf("b"));

        //返回第一次出现字符,从指定位置开始寻找
        String strIndexOf2 = "abcdeabcde";
        System.out.println(strIndexOf2.indexOf("d",4));

        //lastIndexOdf,为最后一次出现,用法同indexOf
        //返回指定字符最后一次出现的位置
        String strLastIndexf = "abcdeabcde";
        System.out.println(strLastIndexf.lastIndexOf("d"));

        //检查字符串是否为空
        String strIsEmpty = "";
        System.out.println(strIsEmpty.isEmpty());//长度为0,返回true

        //获取长度
        String strLength = "abcdefg";
        System.out.println(strLength.length());


        //替换
        String strReplace = "abcdvfg";
        System.out.println(strReplace.replace("v","e"));

        //分割
        String strSplit = "abcde";
        String [] strSplitArray1 = strSplit.split("c");
        String [] strSplitArray2 = strSplit.split("b");
        for(String s : strSplitArray1){
            System.out.print("	"+s);
        }
        System.out.println("");
        for(String s : strSplitArray2){
            System.out.print("	"+s);
        }
        System.out.println("");

        System.out.println("--------");

        //指定位置输出
        String strSubString = "abcdefg";
        System.out.println(strSubString.substring(1,3));
        System.out.println(strSubString.substring(2));

        //将字符串转换为字符数组
        String strArrayChar = "abcdefg";
        char [] arrayChar = strArrayChar.toCharArray();

        //去掉空格
        String strTrim = " qqq ";
        System.out.println(strTrim.trim());

        //valueOf

        //int转String
        int q = 1234;
        Integer integer = q;
        String str = integer.toString();
        //String类型转int
        int i = Integer.parseInt(str);
        System.out.println(i);


    }


}
如有问题,请发送邮件至buxiaqingcheng@163.com或者buxiaqingcheng@dingtalk.com
原文地址:https://www.cnblogs.com/zhenzhunaichabujiatang/p/13409008.html