java String类

String类

1.String类的特点

  1. 字符串的本质是数组

  2. String 类代表字符串。Java 程序中的所有字符串字面值(如 “abc” )都作为此类的实例实现。

  3. 字符串常量存在于方法区常量池

  4. 它们的值在创建之后不能更改

  5. 字符串缓冲区支持可变的字符串 StringBuffer StringBuilder

  6. 字符串的拼接在编译时期其实已经做了优化,使用的是StringBuilder

    		String str1 = "aaa";
    		String str2 = "bbb";
    		String str3 = "aaabbb";
    	 	String str4 = str1 + str2;
    		System.out.println(str3 == str4); //false
    
            String hello = "hello";
            String hel = "hel";
            String lo = "lo";
            System.out.println(hello == "hel" + "lo"); // true
            System.out.println(hello == "hel" + lo); // false
    

    因为 String 对象是不可变的,所以可以共享

  7. String s = “Hello”;和String s3 = new String(“Hello”);的区别

    1. 前者在类加载的时候在方法区创建字符串对象,后者在new的时候在堆区中创建对象,在类加载的时候在方法区中创建对象

    2. String s = new String("abc");
       		String s1 = "abc";
       		String s2 = new String("abc");
       		
       		System.out.println(s == s1); // false
       		System.out.println(s == s2); // false
       		System.out.println(s1 == s2); // false
      
  8. intern();能够将指向堆区的指针指向方法区,并且返回方法区字符串的地址(即消除对象指向堆区,改为直接指向方法区)

    		String s = "Hello";
    		String s2 = new String("Hello");
    		String s3 = new String("Hello");
    		System.out.println(s == s2.intern()); // true
    		System.out.println(s2.intern() == s3.intern()); // true
    		
    		System.out.println(s.equals(s2)); // true
    		System.out.println(s2.equals(s3)); // true
    

2.字符串的构造方法

  1. public String() 创建一个空字符串对象

  2. public String(String original) 创建一个内容为original的字符串对象

  3. public String(char[] value) 将字符数组转换成字符串

  4. public String(char[] value, int index, int count) 将字符数组的一部分转换成字符串

  5. public String(byte[] bytes) 将字节数组转换成字符串

  6. public String(byte[] bytes, int offset, int length) 将字节数组一部分转换为字符串

3.字符串成员方法

3.1获取相关方法

说明
char charAt(int index)获取index索引相对应的字符
int indexOf(int ch)获取某个字符ch在字符串中的索引
int indexOf(String str)获取字符串str在源字符串中出现的索引
int indexOf(int ch,int fromIndex)从fromIndex开始从左往右找,第一次出现字符ch的索引
int indexOf(String str,int fromIndex)从fromIndex开始从左往右找,第一次出现字符串str的索引
int lastIndexOf(int ch)从右往左找,第一次出现字符串ch对应的索引
int lastIndexOf(int ch,int fromIndex)从fromIndex开始从右往左找,第一次出现字符ch的索引
int lastIndexOf(String str,int fromIndex)从fromIndex开始从右往左找,第一次出现字符串str的索引
String substring(int start)从start截取字符串到字符串末尾
String substring(int start,int end)从start索引出开始截取到end索引
int length()获取字符串的长度

3.2判断相关方法

说明
boolean isEmpty()判断字符串是否为空
boolean equals(Object obj)将此字符串的内容与指定的对象比较,区分大小写
boolean equalsIgnoreCase(String str)将此 String 与另一个 String 比较,忽略大小写
boolean contains(String str)判断字符串中是否包含方法传入的字符串
1boolean startsWith(String str)判断字符串是否以某个指定的字符串开头
boolean endsWith(String str)判断字符串是否以某个指定的字符串结尾
        public static void main(String[] args) {
                String s = "";
                String s2 = new String();
                String s3 = null;
                System.out.println(s.isEmpty()); // true
                System.out.println(s2.isEmpty()); // true
            //这里需要和数组区分开,在数组创建新的对象,动态初始化时,
            //堆区会自动给相应类型进行默认赋值
            //而这里的 String s2 = new String();是类,
            //类的创建也会在堆区生成相应空间,但会调用无参构造,
            //无参构造的方法体是空字符,不是为空,所以值为空字符,不会出现空指针异常
                // System.out.println(s3.isEmpty()); // 运行报错
        }

3.3转换相关方法

说明
byte[] getBytes()将字符串转化为字节数组
char[] toCharArray()将字符串转化为字符数组
static String valueOf(char[] chs)返回 char 数组参数的字符串表示形式
static String valueOf(int i)返回 int 参数的字符串表示形式
String toLowerCase()将此 String 中的所有字符都转换为小写
String toUpperCase()将此 String 中的所有字符都转换为大写
String concat(String str)将指定字符串连接到此字符串的结尾

3.4其他方法

说明
String replace(char old,char new)替换功能
String replace(String old,String new)替换功能
String trim()去除字符串两空格
int compareTo(String str)按字典顺序比较两个字符串
int compareToIgnoreCase(String str)按字典顺序比较两个字符串,忽略大小写
public String[] split(String regex)分隔字符串成字符数组
static String format(Locale l, String format, Object… args)使用指定的区域设置,格式字符串和参数返回格式化的字符串
static String format(String format, Object… args)使用指定的格式字符串和参数返回格式化的字符串
static String join(CharSequence delimiter, CharSequence[] elements)使用指定连接符连接成字符串

4.使用总结

  1. //把字符串中的字符进行排序
    public class Practice {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
            System.out.println("请输入一个字符串");
            String str = input.next();
    		byte[] bys = str.getBytes();
            
    		Arrays.sort(bys);
            
            //使用Sring构造方法把字符数组转字符串
    		str = new String(bys);
    		System.out.println(str);
            
            //使用String成员方法把字符串,因为valueOf没有bytep[]类型的,
            //所以用的是Object参数类型的,toString没有重写,所以输出的是地址
            System.out.println(String.valueOf(bys));
    	}
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值