目录

1.连接字符串 concat()

2.创建格式化对象 format()

3.获取字符串某一位置字符 charAt()

4.获取字符串的长度 length()

5.查找子串在字符串中的位置 indexOf()

6.截取字符串的子串 substring()

7.字符串比较 compareTo()

8.字符串大小写转换  toLowerCase()【大写换小写】 toUpperCase()【小写换大写】

9.split表达式 split()

10.字符串替换 replaceAll()

11.字符串进行比较的对象 equals()  contains()

12.equals() 和 contains()的区别不同

13.字符串替换 replace()

14.测试字符串是否以指定的后缀结束 endsWith()

15.字符串的指定索引处返回字符的Unicode字符值 codePointAt()

16.字符串是否为空 isEmpty()

17.删除字符串的头尾空白符。trim()


 

 

 

今天突然提到了 String常用的命令方法 恨自己平时没有积累 几个就穷了...String方法好多 后面自己怕忘都小博客记下来!!!

1.连接字符串 concat()

代码非常详细,在这里不做过多解释 一眼明白点个小心心,有点模糊的直接复制运行一遍就懂了!

        String str=new String("abcdefg");
        String str1=new String("hijklmn");
        String concat = str.concat(str1);
        System.out.println("concat = " + concat);

运行结果

2.创建格式化对象 format()

        Date date=new Date();
        System.out.println("date = " + date);
        SimpleDateFormat tt=new SimpleDateFormat("YYYY-MM-dd");
        System.out.println("tt = " + tt);
        String format = tt.format(date);
        System.out.println("format = " + format);
        
        //简化代码
        System.out.println( new SimpleDateFormat("YYYY-MM-dd").format(new Date()));

运行结果:

在网上还看到关于formot这种用法 其具体参数什么的 博主分享是jdk8文档参考而来 感兴趣的可以看一下
但是 还是觉得 formot 上面的好用一点
        String st2 = String.format("%1$tY-%1$tm-%1$te", new Date());
        System.out.println(st2);

运行结果:个人觉得这个有点麻烦,也可能这个格式语句在别的地方大用 知道了解一哈

 

3.获取字符串某一位置字符 charAt()

        String str=new String("abcdefg");        
        char c = str.charAt(0);
        System.out.println("获取字符串某一位置字符 str.charAt(0) 结果是:" + c);

运行结果:

4.获取字符串的长度 length()

        String str=new String("abcdefg");  
        int length = str.length();
        System.out.println("获取字符串的长度 str.length(); 结果是:" + length);

运行结果:

5.查找子串在字符串中的位置 indexOf()

        String str=new String("abcdefg");   
        int a = str.indexOf("a");
        System.out.println("查找子串在字符串中的位置 str.indexOf(\"a\"); 结果是:" + a);

运行结果:

6.截取字符串的子串 substring()

public String substring(int beginIndex, int endIndex),该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符
        String str=new String("abcdefg");          
        String substring = str.substring(5);
        String substring1 = str.substring(3,5);
        String substring2 = str.substring(0,2);
        System.out.println("截取字符串的子串 str.substring(5); 结果是: " + substring);
        System.out.println("截取字符串的子串 str.substring(3,5) 结果是:; " + substring1);
        System.out.println("截取字符串的子串 str.substring(0,2); 结果是:" + substring2);

运行结果:

7.字符串比较 compareTo()

        Integer cc=8;
        System.out.println("cc.compareTo(10) = " + cc.compareTo(10));
        System.out.println("cc.compareTo(5) = " + cc.compareTo(5));
        System.out.println("cc.compareTo(8) = " + cc.compareTo(8));
//        如果指定的数大于参数返回 -1。
//
//        如果指定的数小于参数返回 1。
//
//        如果指定的数与参数相等返回0。

运行结果:

8.字符串大小写转换  toLowerCase()【大写换小写】 toUpperCase()【小写换大写】

    //大转小
        String st=new String("HIJKLMN");
        String s = st.toLowerCase();
        System.out.println("s = " + s);
    //小转大
        String str=new String("abcdefg");   
        String s1 = str.toUpperCase();
        System.out.println("s1 = " + s1);

运行结果:

9.split表达式 split()

官方回答:split表达式,其实就是一个正则表达式。* ^ | 等符号在正则表达式中属于一种有特殊含义的字符, 如果使用此种字符作为分隔符,必须使用转义符即\\加以转义。、

通俗点说就是把特殊格式的字符串 通过split去掉奇奇怪怪的格式 使字符串看起来更加顺眼一点。

        String dizhi="地球-中国-河南-郑州";
        String dizhi2="地球:巴基斯坦:河南:郑州";
        String[] split = dizhi.split("-");
        String[] split2 = dizhi2.split(":");
        System.out.println(split[0]+split[1]+split[2]+split[3]);
        System.out.println(split2[0]+split2[1]+split2[2]+split2[3]);

运行结果:

 突然想到 特定情况 我们需要使用UUID的时候 随机生成ID会有--符号 不好看 spilt也可以解决这个问题 但是不是最便捷的

10.字符串替换 replaceAll()

        UUID uuid = UUID.randomUUID();
        System.out.println("uuid = " + uuid);
        String[] split1 = uuid.toString().split("-");
        System.out.println("split1[0]+split1[1]+split1[2]+split1[3]+split1[4] = " +                        split1[0]+split1[1]+split1[2]+split1[3]+split1[4]);
//还可以使用replaceAll方法替换字符串 regex参数是需要替换的字符 replacement 替换之后显示的字符
        String s2 = uuid.toString().replaceAll("-", "");
        System.out.println("s2 " + s2);
       

运行结果:

11.字符串进行比较的对象 equals()  contains()

        String str=new String("abcdefg");  
        String str1=new String("hijklmn");         
        if(str.equals("1")){
            System.out.println("输出错误");
        }else{
            System.out.println("输出正确");
        }
       
        boolean equals = str.equals(str1);
        System.out.println("equals = " + equals);




        boolean a1 = str.contains("a");
        System.out.println("a1 = " + a1);

运行结果:

12.equals() 和 contains()的区别不同

//        如果此字符串包含,contains的意思就是 我str字符串内若包含()内的char值 就返回true 反之false
//        那么到这个地方 心细的同学可能会发现 contains和equals不是一样啊 都是判断()内值是否存在 返回Boolean类型
//        其实并不是这样的 只是一个烟雾弹 细探根源
//        第一不同:
//        public boolean equals(Object anObject)
//        public boolean contains(CharSequence s)
//        其次:
//        equals只能判断两个变量的值是否相等。
//        contains常用与集合中判断某个对象是否含有这个元素
//
//       最后:
//        就明白了,equals是需要两个对象完全相同才会返回true,而contains是要循环遍历容器里的所有内容后判断是否包含对象。

13.字符串替换 replace()

        String str=new String("abcdefg");  
        String replace = str.replace("a", "AAA");
        System.out.println("replace = " + replace);

运行结果:

14.测试字符串是否以指定的后缀结束 endsWith()

//        endsWith() 方法用于测试字符串是否以指定的后缀结束。
//        如果是 显示为true 反之false
        String str=new String("abcdefg");  
        boolean d = str.endsWith("d");
        System.out.println("d = " + d);

运行结果:

15.字符串的指定索引处返回字符的Unicode字符值 codePointAt()

//        codePointAt()方法在字符串的指定索引处返回字符的Unicode字符值。第一个字符的索引为0,第二个字符的索引为1,依此类推。
//        更多Unicode字符值 如下图
        String str=new String("abcdefg");    
        int i = str.codePointAt(0);
        int i1 = str.codePointAt(1);
        int i2 = str.codePointAt(2);
        int i3 = str.codePointAt(3);
        System.out.println("i = " + i);
        System.out.println("i1 = " + i1);
        System.out.println("i2 = " + i2);
        System.out.println("i3 = " + i3);

运行结果:

16.字符串是否为空 isEmpty()

判断字符串是否为空 如果为空 返回true 如果不为空 返回 fales
        String str=new String("abcdefg");  
        boolean empty = str.isEmpty();
        System.out.println("empty = " + empty);

运行结果:

17.删除字符串的头尾空白符。trim()

trim() 方法用于删除字符串的头尾空白符。
        String sin=new String("   www.baidu.com    ");
        String trim = sin.trim();
        System.out.println("trim = " + trim);

 

运行结果:

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐