先进行专栏介绍
本专栏是自己学Java的旅途,纯手敲的代码,自己跟着黑马课程学习的,并加入一些自己的理解,对代码和笔记
进行适当修改。希望能对大家能有所帮助,同时也是请大家对我进行监督,对我写的代码进行建议,互相学习。
String详解
Java中的String是一个不可变的字符序列,它是Java中最常用的数据类型之一。
常用构造方法
String():无参构造方法,创建一个空字符串对象。String(char[] value):通过字符数组创建一个字符串对象。String(char[] value, int offset, int count):通过字符数组的一部分创建一个字符串对象,从指定的偏移量开始,取count个字符。String(byte[] bytes):通过字节数组创建一个字符串对象,使用平台的默认字符集将字节解码为字符。String(byte[] bytes, int offset, int length):通过字节数组的一部分创建一个字符串对象,从指定的偏移量开始,取length个字节,并使用平台的默认字符集将字节解码为字符。String(String original):通过复制原始字符串创建一个新的字符串对象。String(StringBuffer buffer):通过StringBuffer对象创建一个字符串对象。String(StringBuilder builder):通过StringBuilder对象创建一个字符串对象。
代码演示
public class crj{ public static void main ( String [ ] args) { String str1 = new String ( ) ; System . out. println ( "str1: " + str1) ; char [ ] charArray = { 'H' , 'e' , 'l' , 'l' , 'o' } ; String str2 = new String ( charArray) ; System . out. println ( "str2: " + str2) ; String str3 = new String ( charArray, 2 , 3 ) ; System . out. println ( "str3: " + str3) ; byte [ ] byteArray = { 72 , 101 , 108 , 108 , 111 } ; String str4 = new String ( byteArray) ; System . out. println ( "str4: " + str4) ; String str5 = new String ( byteArray, 2 , 3 ) ; System . out. println ( "str5: " + str5) ; String original = "Hello" ; String str6 = new String ( original) ; System . out. println ( "str6: " + str6) ; StringBuffer buffer = new StringBuffer ( "Hello" ) ; String str7 = new String ( buffer) ; System . out. println ( "str7: " + str7) ; StringBuilder builder = new StringBuilder ( "Hello" ) ; String str8 = new String ( builder) ; System . out. println ( "str8: " + str8) ; }
}
常用成员方法
int length():返回字符串的长度。char charAt(int index):返回指定索引位置的字符。int indexOf(String str):返回指定子字符串在字符串中第一次出现的索引位置。int lastIndexOf(String str):返回指定子字符串在字符串中最后一次出现的索引位置。boolean startsWith(String prefix):判断字符串是否以指定的前缀开始。boolean endsWith(String suffix):判断字符串是否以指定的后缀结束。boolean isEmpty():判断字符串是否为空。String substring(int beginIndex):返回从指定索引位置开始到字符串末尾的子字符串。String substring(int beginIndex, int endIndex):返回从指定的开始索引位置到结束索引位置的子字符串。String replace(char oldChar, char newChar):将字符串中的所有旧字符替换为新字符。String replace(CharSequence target, CharSequence replacement):将字符串中的所有目标字符序列替换为指定的替换字符序列。String[] split(String regex):将字符串根据指定的正则表达式分割为字符串数组。String trim():去除字符串两端的空白字符。boolean equals(Object obj):判断字符串是否与指定对象相等。boolean equalsIgnoreCase(String anotherString):忽略大小写判断字符串是否与另一个字符串相等。String toLowerCase():将字符串转换为小写。String toUpperCase():将字符串转换为大写。String concat(String str):将指定字符串连接到原字符串的末尾。boolean contains(CharSequence sequence):判断字符串是否包含指定的字符序列。String format(String format, Object... args):将指定格式的字符串与参数进行格式化。
代码示例
public class StringMethodsDemo { public static void main ( String [ ] args) { String str = "Hello, World!" ; int length = str. length ( ) ; System . out. println ( "Length: " + length) ; char ch = str. charAt ( 7 ) ; System . out. println ( "Character at index 7: " + ch) ; int index = str. indexOf ( "World" ) ; System . out. println ( "Index of 'World': " + index) ; int lastIndex = str. lastIndexOf ( "o" ) ; System . out. println ( "Last index of 'o': " + lastIndex) ; boolean startsWith = str. startsWith ( "Hello" ) ; System . out. println ( "Starts with 'Hello': " + startsWith) ; boolean endsWith = str. endsWith ( "World" ) ; System . out. println ( "Ends with 'World': " + endsWith) ; boolean isEmpty = str. isEmpty ( ) ; System . out. println ( "Is empty: " + isEmpty) ; String substring1 = str. substring ( 7 ) ; System . out. println ( "Substring from index 7: " + substring1) ; String substring2 = str. substring ( 7 , 12 ) ; System . out. println ( "Substring from index 7 to 12: " + substring2) ; String replaced = str. replace ( 'o' , 'O' ) ; System . out. println ( "Replaced string: " + replaced) ; String replaced2 = str. replace ( "World" , "Universe" ) ; System . out. println ( "Replaced string: " + replaced2) ; String [ ] splitArray = str. split ( "," ) ; System . out. println ( "Split array: " + Arrays . toString ( splitArray) ) ; String trimmed = str. trim ( ) ; System . out. println ( "Trimmed string: " + trimmed) ; boolean equals = str. equals ( "Hello, World!" ) ; System . out. println ( "Equals 'Hello, World!': " + equals) ; boolean equalsIgnoreCase = str. equalsIgnoreCase ( "hello, world!" ) ; System . out. println ( "Equals ignore case 'hello, world!': " + equalsIgnoreCase) ; String lowerCase = str. toLowerCase ( ) ; System . out. println ( "Lower case: " + lowerCase) ; String upperCase = str. toUpperCase ( ) ; System . out. println ( "Upper case: " + upperCase) ; String concat = str. concat ( " How are you?" ) ; System . out. println ( "Concatenated string: " + concat) ; boolean contains = str. contains ( "World" ) ; System . out. println ( "Contains 'World': " + contains) ; String formatted = String . format ( "The value of pi is approximately %.2f" , Math . PI ) ; System . out. println ( "Formatted string: " + formatted) ; }
}
总结
String是一个重要的数据类型,它提供了许多方法来处理字符串。由于字符串的不可变性,每次对字符串进行
操作都会创建一个新的字符串对象,因此在频繁操作字符串时需要注意性能问题。