Java常用类学习:String类

 

Java常用类学习:String类

  • Java String类:

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

       

  • 创建字符串:

    String name="haha";

    //1,在代码中遇到字符串常量时(这里指的是haha),编译器会使用该值创建一个String对象;
    //2,和其他对象一样,可以使用关键字和构造方法来创建String对象;

    //3,用构造方法创建字符串
    String name1=new String("haha");

    //4,String创建的字符串,存储在公共池中,而new创建的字符串对象在堆中;

    //例子:
    String s1="hello";
    String s2="hello";
    String s3=s2;
    String s4=new String("hello");
    String s5=new String("hello");

     

  • String类有11种构造方法,这些方法提供不同的参数来初始化字符串;

  • String类是不可改变的,所以一旦创建了String对象,那它的值就无法改变了;

  • 如果要对字符串频繁修改,需要使用StringBuffer和StringBuilde类;

    String s="Google";
    System.out.println("s:"+s);

    s="Runoob";
    System.out.println("s:"+s);

    /**
    笔记:
    1,从结果上看,是改变了;但为什么说String对象是不可变的呢?
    2,原因在于实例中的s只是一个String对象的引用,并不是对象本身;
    3,当执行s="baidu";创建了一个新的对象"baidu",而原来的"Google"还存在内存中;

    */

     

  • 字符串长度:

    • 用于获取有关对象的信息的方法称为访问器方法

    • String类的一个访问器方法是length()方法,它返回字符串对象包含的字符数;

      public class StringDemo01 {
         public static void main(String[] args) {

             String s="Google";
             int i=s.length();
             System.out.println(i);//6

        }
      }

       

  • 字符串连接:

    • String类提供了连接2个字符串的方法;

    • 切记:用新的字符串去接收;

      public class StringDemo01 {
         public static void main(String[] args) {

             String str="Google";
             String str1=".com";
             String str3=str.concat(str1);//切记,用新的字符串去接收
             System.out.println(str3);//Google.com

        }
      }

       

  • 代码案例:字符串常用方法

    • 注意1:isEmpty() :判断的是:value.length == 0;

    • 注意2:String[] strArr=s12.split(".");//切记:特殊符号需要转义


    /**
    * 字符串常用方法
    */
    public class StringDemo01 {
       public static void main(String[] args) {
           String str="Google";

           //int codePointAt(int index):返回指定索引处的字符(Unicode值)
           int i1=str.codePointAt(0);
           System.out.println(i1);//71

           //int compareTo(String str):按字典顺序比较2个字符串
           String str1="Google";
           int i2=str.compareTo(str1);
           System.out.println(i2);//全匹配:0;否则不为0

           //boolean endWith(String str):判断此字符串是否以指定的后缀结尾
           Boolean b=str.endsWith("e");
           System.out.println(b);//true

           //boolean startWith(String str):判断此字符串是否以指定的前缀开始
           Boolean b1=str.startsWith("G");
           System.out.println(b1);

           //boolean equals(String str):将此字符串与指定对象进行比较
           boolean b2=str.equals(str1);
           System.out.println(b2);//true

           //boolean equalsIgnorecase(String s):忽略大小写比较字符串
           boolean b3=str.equalsIgnoreCase("GoogLE");
           System.out.println(b3);//true

           //static String format(String format,Object obj):格式化字符串
           //String str3= String.format(str, )


           //int hashCode(String str):返回该字符串的hashCode值
           int i3=str.hashCode();
           System.out.println(i3);//2138589785

           //char charAt(int index):返回char指定索引处的值

           char c=str.charAt(0);
           System.out.println(c);//G

           //int indexOf(char c):
           //System.out.println(str.toString());
           int i4=str.indexOf('G');
           System.out.println(i4);//0

           //int length(String str):
           int i5=str.length();
           System.out.println(i5);//6

           //String replace(char c1,char c2):替换:用新的字符替换旧的字符
           String s11="Google";
           s11=s11.replace('G','g');
           System.out.println(s11);//google

           //String[] split(String regex):
           String s12="www.baidu.com";
           String[] strArr=s12.split("\\.");//切记:特殊符号需要转义
           for (String x:strArr) {
               System.out.println(x+",");
          }

           //String substring(int begindex,int endindex):截取字符串
           String str13="Google";
           str13=str13.substring(0,2);
           System.out.println(str13);//Go


           // boolean isEmpty():判断字符串是否为空;//判断的是值的长度
           String str14="";
           System.out.println(str14);
           if(str14.isEmpty()){//判断的是:value.length == 0;
               System.out.println("为空");
          }else{
               System.out.println("不为空");
          }




      }
    }
  • 代码案例2:String类中getBytes()方法:返回一个byte[]数组;

    public class Demo01 {
       public static void main(String[] args) {

           //String类方法:getBytes(): 返回一个byte[]数组;

           String s="Hello";
           byte[] bytes=s.getBytes();
           System.out.println(bytes);

      }
    }
posted @   gzs1024  阅读(84)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示