网站设计确认,用html设计一个网页代码,做企业网站需要建多大的画布,网站前端开发培训这里写目录标题 先进行专栏介绍String详解常用构造方法代码演示常用成员方法代码示例总结 先进行专栏介绍
本专栏是自己学Java的旅途#xff0c;纯手敲的代码#xff0c;自己跟着黑马课程学习的#xff0c;并加入一些自己的理解#xff0c;对代码和笔记
进行适当修改。希望… 这里写目录标题 先进行专栏介绍String详解常用构造方法代码演示常用成员方法代码示例总结 先进行专栏介绍
本专栏是自己学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); // 输出str1:// 通过字符数组创建字符串对象char[] charArray {H, e, l, l, o};String str2 new String(charArray);System.out.println(str2: str2); // 输出str2: Hello// 通过字符数组的一部分创建字符串对象String str3 new String(charArray, 2, 3);System.out.println(str3: str3); // 输出str3: llo// 通过字节数组创建字符串对象byte[] byteArray {72, 101, 108, 108, 111};String str4 new String(byteArray);System.out.println(str4: str4); // 输出str4: Hello// 通过字节数组的一部分创建字符串对象String str5 new String(byteArray, 2, 3);System.out.println(str5: str5); // 输出str5: llo// 通过复制原始字符串创建新的字符串对象String original Hello;String str6 new String(original);System.out.println(str6: str6); // 输出str6: Hello// 通过StringBuffer对象创建字符串对象StringBuffer buffer new StringBuffer(Hello);String str7 new String(buffer);System.out.println(str7: str7); // 输出str7: Hello// 通过StringBuilder对象创建字符串对象StringBuilder builder new StringBuilder(Hello);String str8 new String(builder);System.out.println(str8: str8); // 输出str8: Hello}
}常用成员方法
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!;// length()int length str.length();System.out.println(Length: length); // 输出Length: 13// charAt(int index)char ch str.charAt(7);System.out.println(Character at index 7: ch);// 输出Character at index 7: W// indexOf(String str)int index str.indexOf(World);System.out.println(Index of World: index); // 输出Index of World: 7// lastIndexOf(String str)int lastIndex str.lastIndexOf(o);System.out.println(Last index of o: lastIndex); // 输出Last index of o: 8// startsWith(String prefix)boolean startsWith str.startsWith(Hello);System.out.println(Starts with Hello: startsWith); // 输出Starts with Hello: true// endsWith(String suffix)boolean endsWith str.endsWith(World);System.out.println(Ends with World: endsWith); // 输出Ends with World: false// isEmpty()boolean isEmpty str.isEmpty();System.out.println(Is empty: isEmpty); // 输出Is empty: false// substring(int beginIndex)String substring1 str.substring(7);System.out.println(Substring from index 7: substring1);// 输出Substring from index 7: World!// substring(int beginIndex, int endIndex)String substring2 str.substring(7, 12);System.out.println(Substring from index 7 to 12: substring2); // 输出Substring from index 7 to 12: World// replace(char oldChar, char newChar)String replaced str.replace(o, O);System.out.println(Replaced string: replaced); // 输出Replaced string: HellO, WOrld!// replace(CharSequence target, CharSequence replacement)String replaced2 str.replace(World, Universe);System.out.println(Replaced string: replaced2); // 输出Replaced string: Hello, Universe!// split(String regex)String[] splitArray str.split(,);System.out.println(Split array: Arrays.toString(splitArray));// 输出Split array: [Hello, World!]// trim()String trimmed str.trim();System.out.println(Trimmed string: trimmed); // 输出Trimmed string: Hello, World!// equals(Object obj)boolean equals str.equals(Hello, World!);System.out.println(Equals Hello, World!: equals); // 输出Equals Hello, World!: true// equalsIgnoreCase(String anotherString)boolean equalsIgnoreCase str.equalsIgnoreCase(hello, world!);System.out.println(Equals ignore case hello, world!: equalsIgnoreCase); // 输出Equals ignore case hello, world!: true// toLowerCase()String lowerCase str.toLowerCase();System.out.println(Lower case: lowerCase); // 输出Lower case: hello, world!// toUpperCase()String upperCase str.toUpperCase();System.out.println(Upper case: upperCase); // 输出Upper case: HELLO, WORLD!// concat(String str)String concat str.concat( How are you?);System.out.println(Concatenated string: concat); // 输出Concatenated string: Hello, World! How are you?// contains(CharSequence sequence)boolean contains str.contains(World);System.out.println(Contains World: contains); // 输出Contains World: true// format(String format, Object... args)String formatted String.format(The value of pi is approximately %.2f, Math.PI);System.out.println(Formatted string: formatted); // 输出Formatted string: The value of pi is approximately 3.14}
} 总结
String是一个重要的数据类型它提供了许多方法来处理字符串。由于字符串的不可变性每次对字符串进行
操作都会创建一个新的字符串对象因此在频繁操作字符串时需要注意性能问题。