Java基础:String类型下的方法

1.字符串和字符数组转换:toCharArray

    //将字符串转换为字符数组:toCharArray()

        String str = "hello";
        char[] c = str.toCharArray();

       //将字符数组转换为字符串

                  String s=new String(c);//取出完整的str:hello

                  String s=new String(c,0,2); //从0开始计数,0表示初始位置,2表示个数,则s的结果是he

                        String s=new String(c,1,3);//初始位置1,个数3,则s结果是ell


2.取出字符串指定的字符charAt

          char ch=s.charAt(1);//从0开始计算,1则表示e


3.字符串与字节数组转换byte[]

           String str = "hello";
        byte[] b = str.getBytes();//字节数组要输出需要转换输出
        System.out.println(new String(b));//同理能得hello


4.字符串长度length

        String str="world";

        str.length();//长度为5


5.字符串是否存在 indexof,lastIndexOf(最后出现的位子)

               String str = "hello3e";

         System.out.println(str.indexOf("a"));//a没有,所以返回-1,-1则表示没找到

        System.out.println(str.indexOf("e"));//位子从0开始排,第一个e返回则为1
        System.out.println(str.indexOf("e", 2));//加个数字2,则为第二个e,则返回6

            System.out.println(str.lastIndexOf('l'));//最后个l出现位子 为3


6.去空格trim,去掉字符串左右的空格(不含字符串中间的空格)

         String str="        hi       ";

        System.out.println(str.trim());//输出结果为hi,没有空格


7.字符串截取substring

        String str = "hello3e";
        System.out.println(str.substring(2));//得到'llo3e'
        System.out.println(str.substring(1, 2));//得到'e'
        System.out.println(str.subSequence(1, 2));//得到'e'

substring和subSequence的返回类型不一样,substring返回的是String,subSequence返回的是实现了CharSequence接口的类,也就是说使用subSequence得到的结果,只能使用CharSequence接口中的方法。不过在String类中已经重写了subSequence,调用subSequence方法,可以直接下转为String对象。


8.拆分字符串split

        String str = "hello3e";
        String[] s = str.split("l");
        System.out.println(s.length);//s.length=3
        for (int i = 0; i < s.length; i++) {
            System.out.println(s[i]);
        }

     //此处s[0]为"he",s[1]为"",s[2]为“o3e”



9.字符串拼接
    字符串可以split拆分,同样可以用concat拼接

 String str = "hello";
        String str2 = "HELlo";
        str.concat(str2);
        System.out.println(str.concat(str2));//返回结果:helloHELlo

    把2个字符串拼接起来


10.大小转换toUpperCase,toLowerCase

        String str = "hello3e";
        String s = str.toUpperCase();
        System.out.println(s);//转为大写:HELLO3E

      String str = "HEllo3e";
        String s = str.toLowerCase();
        System.out.println(s);转为大写:hello3e



11.判断是否以某字符串开头,结尾startsWith,endsWith,返回的是boolean型

        String str = "HEllo3e";
        System.out.println(str.startsWith("H"));//true
        System.out.println(str.startsWith("h"));//false
        System.out.println(str.endsWith("E"));//false 

        System.out.println(str.endsWith("e"));//true


12.比较,不区分大小写equals,       equalsIgnoreCse不区分大小写

   str.equals(str2)返回的是boolean型

        String str = "HEllo3e";
        String s1 = "HEllo3e";
        String s2 = "hello3e";
        String s3 = "HELLO3E";
        System.out.println(str.equals(s1));//true
        System.out.println(str.equals(s2));//false
        System.out.println(str.equals(s3));//false
        System.out.println(str.equalsIgnoreCase(s1));//true
        System.out.println(str.equalsIgnoreCase(s2));//true
        System.out.println(str.equalsIgnoreCase(s3));//true

13.字符串代表的char值按指定的顺序相同的顺序contentEquals
    String str1 = "amrood admin", str2 = "345";
    CharSequence cs = "amrood admin"; 
    System.out.println("Method returns: " + str1.contentEquals("amrood admin"));//true
    System.out.println("Method returns: " + str1.contentEquals(cs));//true
    System.out.println("Method returns: " + str2.contentEquals("12345"));//false
contentEquals和equals的区别:
   contentEquals就是用来比较内容相同就行了
   ,CharSequence,StringBuffer 都可以进行比较,只要内部char 相同排序相同就算一样。

   equels方法是 如果这2个同一个字符串引用那么为true,
   如果不是同一个引用那么如果二个对象都是String类型则会判断内部char是否相同并且排序一样,
   如果一样就是true,除了这2中就为false。



14.查看是否包含,字符串中是否包含指定字符串/字符,返回的是boolean型

          String str = "hello";
        String str2 = "lo";
        System.out.println(str.contains(str2));//hello中确实包含了lo,返回true


15.字符串替换replaceAll

        String str = "HEllo3e";
        String s1 = str.replace("l", "x");//将l转换成x
        String s2 = str.replaceAll("l", "x");//将l转换成x
        String s3 = str.replaceFirst("l", "x");//将第一个l转换成x
        System.out.println(s1);//HExxo3e
        System.out.println(s2);//HExxo3e
        System.out.println(s3);//HExlo3e

     此处,replace 和replaceAll有本质区别,replace可以是字符和字符串的替换,无异常

replaceAll用的是正则表达式的方法,第一个参数是替换规则。

此处如果这样写会有异常

String str = "String.abc.com";
        System.out.println(str.replaceAll(".", "/"));//输出结果为//


16.查询String类下,指定索引所对应的Unicode码,索引从0开始

     codePointAt:指定索引所对应的Unicode码

        String str = "HEllo3e";
        System.out.println(str.codePointAt(0));//H的Unicode码:72
        System.out.println(str.codePointAt(1));//E的Unicode码:69
        System.out.println(str.codePointAt(2));//l的Unicode码:108

    codePointBefore:指定索引之前所对应的Unicode码

        String str = "HEllo3e";
        System.out.println(str.codePointBefore(1));//H的Unicode码:72

     codePointCount:准确计算unicode字符的数量,而不是char的数量。
        String str = "HEllo3e";
        str.codePointCount(1, 3);
        System.out.println(str.codePointCount(1, 3));//结果为"2",2个unicode字符
        String greeting = "hello好";
        int cpCount = greeting.codePointCount(0, greeting.length());
        System.out.println(cpCount);//结果为"6",6个unicode字符

17.Comparable接口下的compareTo,compareToIgnoreCase
        String str = "2";
        String str2 = "1";       
        System.out.println(str.compareTo(str2));//1
        System.out.println(str2.compareTo(str));//-1
多用于数字的比较,相同返回0,大了返回1,小了返回-1
        String str = "hello";
        String str2 = "hello";       
        System.out.println(str.compareTo(str2));//返回0

        String str = "hello";
        String str2 = "HELlo";
        System.out.println(str.compareToIgnoreCase(str2));//忽略大小写,返回0

18.hashCode值。如果不改写,默认返回hashCode值        
        String str = "hello";
        str.hashCode();
        System.out.println(str.hashCode());//99162322
  另外,对象的hashCode值显示很难看懂,最好定义下toString方法

19.intern
   String s1 = new String("hello");
        String s2 = s1.intern();
        System.out.println(s2);//hello
        System.out.println(s1 == s2);//true
intern 这个方法返回的是 返回字符串对象的规范化表示形式,当调用 intern 方法时,如果池已经
包含一个等于此 String 对象的字符串(该对象由 equals(Object) 方法确定),则返回池中的字符串


20.getClass()。返回类
String s1 = new String("hello");
        s1.getClass();
        System.out.println(s1.getClass()); //class java.lang.String
返回的是String类,并没有什么卵用的方法

21.isEmpty(),判断是否为空字符串
  String s1 = new String("hello");
        s1.isEmpty();
        System.out.println(s1.isEmpty());//因为字符串s1不为空,则返回false

22.matches,正则表达式用此方法
 String s1 = new String("hellolllo");
        String regex = "[a-z]{1,10}";
        System.out.println(s1.matches(regex));//true

23.notify(),notifyAll(),wait()
synchronized同步锁时使用,在此不做深究


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,String类型是不支持直接减法操作的。String类型是用来表示文本的,它是不可变的,也就是说一旦创建就不能修改。如果需要对字符串进行减法操作,可以使用字符串的replace()方法或者正则表达式来实现。replace()方法可以用来替换字符串中的某些字符或者子串,而正则表达式可以用来匹配和替换字符串中符合某种规则的部分。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Java实现String类型时间加减](https://blog.csdn.net/T_yuqing/article/details/104989597)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [java String实现加,减,乘,除运算。](https://blog.csdn.net/lkclkc88/article/details/7481460)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Java自学视频教程-JavaSE基础-常用API-05、String案例:验证码、登录、隐私号码.mp4](https://download.csdn.net/download/weixin_54787054/88233250)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值