Review13_String类中的常用方法

前言:String类中方法很多,在这里将主要常用的总结了一下,主要方法有:

charAt();
compareTo();
contains();
endsWith();
startWith();
equals();
equalsIgnoreCase();
getBytes();
isEmpty();
indexOf();
length();
lastIndexOf();
replace();
replaceAll();
split();
subString();//左闭右开
toCharArray();
toLowerCase();
toUpperCase();
trim();
valueOf();
1、charAt方法
    public char charAt(int index)
    返回指定索引处的char值。
    public static void main(String[] args) {
        String s = "qwert";
        char c = s.charAt(2);//返回s的第三个字符:e
        System.out.println(c);//e

    }
2、compareTo()方法以及compareToIgnoreCase()方法
    compareTo():
        public int compareTo(String anotherString)
        //按字典顺序比较两个字符串字母,从前到后,相同返回0,不同则返回位置差值

    compareToIgnoreCase():
        同compareTo(),但忽略大小写。
    public static void main(String[] args) {
        String s = "abd";
        int a1 = s.compareTo("abc");//1
        int a2 = s.compareTo("abd");//0
        int a3 = s.compareTo("abe");//-1
    }
3、contains();
    public boolean contains(CharSequence s)
    //在字符串中查找指定的字符序列,找到返回true,没找到返回false
    public static void main(String[] args) {
        String s = "abcdef";
        boolean b1 = s.contains("bcd");
        System.out.println(b1);//true

        boolean b2 = s.contains("bcf");
        System.out.println(b2);//false
    }

4、endsWith();与 startWith();
    endsWith();
        public boolean endsWith(String suffix)
        //测试此字符串是否以指定的后缀结尾。
    startWith();
        public boolean startsWith(String prefix)
        //测试此字符串是否以指定的前缀开头
    public static void main(String[] args) {
        String s = "abcdef";
        System.out.println(s.endsWith("def"));//true
        System.out.println(s.endsWith("deg"));//false
        System.out.println(s.startsWith("abc"));//true
        System.out.println(s.startsWith("abd"));//false
    }
5、equals();与equalsIgnoreCase();
    equals();
        public boolean equals(Object anObject)
        //将此字符串与指定的对象进行比较。 当且仅当参数不是null且是String对象表示与此对象相同的字符序列时,结果为true 。

    equalsIgnoreCase();
        同equals();但忽略大小写
    public static void main(String[] args) {
        String s = "abc";
        System.out.println(s.equals("abc"));//true
        System.out.println(s.equals("abe"));//false
        System.out.println(s.equalsIgnoreCase("abC"));//true
    }
6、getBytes();
    public byte[] getBytes()
    //将字符串编码为字节序列,存储到一个数组中
    public static void main(String[] args) {
        String s = "abc";
        byte[] b = s.getBytes();
        byte[] b2 = s.getBytes(StandardCharsets.UTF_8);
        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);//97,98,99
        }
        for (int i = 0; i < b.length; i++) {
            System.out.println(b2[i]);//97,98,99
        }
    }
7、isEmpty();以及length();
   public boolean isEmpty()
   //返回 true ,当且仅当, length()是 0。

   public int length()
   //返回此字符串的长度
    public static void main(String[] args) {
        String s = "";
        String s2 = "abc";
        s.length();
        s2.length();
        System.out.println(s.isEmpty());//true
        System.out.println(s2.isEmpty());//false
        System.out.println(s.length());//0
        System.out.println(s2.length());//3
    }
8、indexOf();以及lastIndexOf();
    public int indexOf(String str, int fromIndex)
    //从指定的索引处开始,返回指定子字符串第一次出现的字符串中的索引。

    public int lastIndexOf(String str, int fromIndex)
    返回指定子字符串最后一次出现的字符串中的索引,从指定索引开始向后搜索。
    public static void main(String[] args) {
        String s = "sheisagoodteacher";
        int a1 = s.indexOf("is");
        System.out.println(a1);//3
        int a2 = s.indexOf("is",2);
        System.out.println(a2);//3,绝对index

        int a3 = s.lastIndexOf("e");
        System.out.println(a3);//15
    }
9、replace();以及replaceAll();
    public String replace(char oldChar, char newChar)
    //替换所有字符串:用newChar替换oldChar。注意是char
    //单个字符替换单个,字符串替换字符串均可

    replaceAll();
    //正则表达式相关,后期了解。
    public static void main(String[] args) {
        String oldStr = "mesquite in your cellar";
        String newStr = oldStr.replace('e','o');
        System.out.println(newStr);//mosquito in your collar
        String newStr2 =oldStr.replace("es","a");
        System.out.println(newStr2);//maquite in your cellar
    }
10、split();
    public String[] split(String regex, int limit)
    //将此字符串拆分为给定的正则表达式的匹配项,返回到一个数组中
    public static void main(String[] args) {
        String s = "1+2+3+4+5+6+7";
        String[] split = s.split("\\+",7);//不写limit默认最大拆分
        for (int i = 0; i < split.length; i++) {
            System.out.println(split[i]);
            /*  1
                2
                3
                4
                5
                6
                7
             */
        }
    }
11、subString();
    public String substring(int beginIndex, int endIndex)
    //返回一个字符串,该字符串是此字符串的子字符串。 子字符串从指定的beginIndex开始,并扩展到索引endIndex - 1处的字符。 因此子串的长度是endIndex-beginIndex 。
    //beginIndex - 起始索引,包括在内。
    //endIndex - 结束索引,不包括。
    public static void main(String[] args) {
        String s = "abcdef";
        String subStr = s.substring(2,4);
        System.out.println(subStr);//cd,左闭右开
    }
12、toCharArray();
    public char[] toCharArray()
    //将此字符串转换为新的字符数组。
    public static void main(String[] args) {
        String s = "abcdefg";
        char[] c = s.toCharArray();
        for (int i = 0; i < c.length; i++) {
            System.out.println(c[i]);
        }
        //{a,b,c,d,e,f,g}
    }
13、toLowerCase();以及 toUpperCase();
    toLowerCase();
    //转换为小写

    toLowerCase();
    //转换为大写
    public static void main(String[] args) {
        String s = "aBcDeFGh";
        String toLow = s.toLowerCase(Locale.ROOT);
        System.out.println(toLow);//abcdefgh

        String toUpp = s.toUpperCase(Locale.ROOT);
        System.out.println(toUpp);//ABCDEFGH
    }
14、trim();
    public String trim()
    //删除了所有前导和尾随空格,不能删除中间的

    trim()删半角,strip()可以去除字符串前后的全角和半角空白字符
    public static void main(String[] args) {
        String s = "    a sdf    df    er  ";
        System.out.println(s);//    a sdf    df    er
        System.out.println(s.trim());//a sdf    df    er,不能删除中间的
    }
15、valueOf();
    public static String valueOf()
    //String类中唯一的静态方法,将“非字符串”转换为“字符串”
    //唯一静态方法
    public static void main(String[] args) {
        String s = valueOf(123);
        System.out.println(s+1);//1231
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值