Java中String类中常见方法以及类与类之间的转换

String类中的方法描述

  • public String() 空参数构造方法
  • public String(byte[] bytes) 把字节数组转换成字符串对象
  • public String(byte[] bytes,int index,int length) 把字节数组一部分转换成字符串对象
  • public String(char[] value) 把字符数组转换成字符串对象
  • public String(char[] value,int index,int count) 把字符数组一部分转换成字符串对象
  • public String(String original) 把字符串常量封装成字符串对象
  • public boolean equals(Object obj)判断两个字符串内容是否一致
  • public boolean equalsIgnoreCase(String str)判断两个字符串内容是否一致,忽略大小
  • public boolean contains(String str) 判断是否包含指定的字符串
  • public boolean startsWith(String str) 判断是否以指定的字符串开头
  • public boolean endsWith(String str) 判断是否以指定的字符串结尾
  • public boolean isEmpty()判断字符串内容是否为空串
  • public int length() 获取字符串长度
  • public char charAt(int index) 获取指定位置上对应的字符
  • public int indexOf(int ch) 获取指定字符第一次出现的位置
  • public int indexOf(String str) 获取指定字符串第一次出现的位置
  • public int indexOf(int ch,int fromIndex) 从指定位置开始,获取指定字符第一次出现的位置
  • public int indexOf(String str,int fromIndex) 从指定位置开始,获取指定字符串第一次出现的位置
  • public String substring(int start) 从指定位置开始,截取字符串到末尾,返回新字符串
  • public String substring(int start,int end) 从指定位置开始,到指定位置结束,截取字符串,返回新字符串
  • public byte[] getBytes() 将字符串转换成字节数组
  • public char[] toCharArray()将字符串转换成字符数组
  • public static String valueOf(char[] chs) 将字符数组转换成字符串
  • public static String valueOf(int i) 将基本数据类型转换成字符串
  • public String toLowerCase() 转换成小写字母
  • public String toUpperCase() 转换成大写字母
  • public String concat(String str) 与指定的字符串向连接,返回新字符串 –
  • public String replace(char old,char new) 将字符串中指定的旧字符,用指定的新字符替换
  • public String replace(String old,String new) 将字符串中指定的旧字符串,用指定的新字符串替换
  • public String trim() 去除字符串两端空格
  • public int compareTo(String str) 与指定的字符串,按照自然顺序比较
  • public int compareToIgnoreCase(String str) 与指定的字符串,按照自然顺序比较,忽略大小写
  • public int capacity() 获得此字符串缓冲对象的当前容量
  • public int length() 获得此字符串缓冲对象的长度,即包含的字符数
  • public StringBuffer append(String str) 将给定的字符串追加到字符串缓冲对象中
  • public StringBuffer insert(int offset,String str) 将给定的字符串插入到字符串缓冲对象中的指定位置
  • public StringBuffer deleteCharAt(int index) 删除掉指定索引处的字
  • public StringBuffer delete(int start,int end) 删除掉字符串缓冲对象部分字符
  • public StringBuffer replace(int start,int end,String str) 用新字符串替换掉指定的子字符串
  • public StringBuffer reverse() 将一个字符串中字符的序列反转
  • public String substring(int start) 获得一个子串,从指定位置开始,到字符串缓冲对象结束处,并将其存储在一个新字符串对象中
  • public String substring(int start,int end) 获得一个指定子串,并将其存储在一个新字符串对象中

==与equals()方法的区别

(1) == 号可以比较基本数据类型,也可以比较引用数据类型
(2)equals方法只能比较引用数据类型,默认比较的是地址值,如果我们想要建立自己的比较方式,我们就需要复写equals方法

下面这条语句一共创建了多少个对象:String s = “a”+“b”+”c”; 分别都是什么?

5个对象,分别是 “a” , “b” , “c” , “ab” , “abc”,因为字符串的特点是一旦被创建就不能被改变,所有在使用常量进行相加的时候,都是在创建新的字符串对象,最后在把字符串"abc"这个常量值赋值给引用变量s

如何实现StringBuffer和String的相互转换?

StringBuffer 转换到 String的方式:
1.通过String类的构造方法
2.通过StringBuffer类中的toString()方法
3.通过StringBuffer类中的substring()方法 (注:不常用)

String 转换到 StringBuffer的方式:

1.通过StringBuffer类的构造方法
2.通过StringBuffer类的append()、insert()方法

如何实现String和int数据的相互转换

String 转换到 int的方式:

1.String – Integer – int通过Integer类的intValue()方法
2.通过Integer类的parseInt(String s)方法

Int 转换到 String的方式:

1.Int – Integer – String Integer的toSting()
2.通过String类的valueOf()方法
3.通过Integer类的toSting(int i)方法
4.通过与字符串""相连接

如何实现 基本数据类型 与 基本数据包装类 之间的相互转换

基本数据类型 转换到 包装类的方式:

1.通过包装类的构造方法
2.通过包装类的静态方法valueOf()

包装类 转换到 基本数据类型的方式:

1.通过包装类的方法xxxValue()

请说明String与StringBuffer二者之间的区别?

Java平台提供了两个类:String和StringBuffer,他们可以存储和操作字符串,即包含多个字符的字符数据。String类表示内容不可以改变的字符串;而StringBuffer类表示内容可以被修改的字符串。

请说明StringBuffer与StringBilder二者之间的区别?

StringBuffer和StringBuilder类都表示内容可以被修改的字符串,StringBilder是线程不安全的,运行效率高,如果一个字符串变量是在方法里面定义,这种情况只可能有一个线程访问它,不存在不安全的因素了,则用StringBuilder。如果要在类里面定义成员变量,并且这个类的实例对象会在多线程环境下使用,那么最好用StringBuffer。

猜你喜欢

转载自blog.csdn.net/qq_43141904/article/details/83385603