怎么提交公司网站,品牌型网站制作哪,集团网站建设运营公司,国内永久免费的crm系统软件Java 是一种广泛使用的编程语言#xff0c;而字符串处理是 Java 编程中非常重要的一部分。Java 提供了丰富的字符串操作功能#xff0c;通过 String 类和 StringBuilder、StringBuffer 类来处理字符串。
一、Java 字符串的创建
1. 使用字面量
在 Java 中#xff0c;字符串…Java 是一种广泛使用的编程语言而字符串处理是 Java 编程中非常重要的一部分。Java 提供了丰富的字符串操作功能通过 String 类和 StringBuilder、StringBuffer 类来处理字符串。
一、Java 字符串的创建
1. 使用字面量
在 Java 中字符串字面量是最常见的字符串创建方式
String str Hello, World!;2. 使用 new 关键字
使用 new 关键字创建字符串
String str new String(Hello, World!);虽然这两种方式看似相同但它们在内存管理上有所不同。字面量方式会将字符串放在字符串池中而使用 new 关键字则会在堆上创建一个新的字符串对象。
3. 使用字符数组
可以通过字符数组来创建字符串
char[] charArray {H, e, l, l, o};
String str new String(charArray);二、字符串的操作
1. 连接字符串
使用 运算符
最简单的字符串连接方式是使用 运算符
String str1 Hello, ;
String str2 World!;
String str3 str1 str2; // str3 内容为 Hello, World!使用 concat 方法
concat 方法也可以用来连接字符串
String str1 Hello, ;
String str2 World!;
String str3 str1.concat(str2); // str3 内容为 Hello, World!2. 字符串比较
使用 equals 方法
equals 方法用于比较两个字符串的内容是否相同
String str1 Hello;
String str2 Hello;
boolean isEqual str1.equals(str2); // isEqual 为 true使用 运算符 运算符比较的是两个字符串对象的引用是否相同
String str1 Hello;
String str2 new String(Hello);
boolean isSame (str1 str2); // isSame 为 false3. 查找字符串
使用 indexOf 方法
indexOf 方法返回子字符串在字符串中第一次出现的位置
String str Hello, World!;
int index str.indexOf(World); // index 为 7使用 lastIndexOf 方法
lastIndexOf 方法返回子字符串在字符串中最后一次出现的位置
String str Hello, World! Hello!;
int index str.lastIndexOf(Hello); // index 为 144. 字符串替换
使用 replace 方法
replace 方法用于将字符串中的某个子字符串替换为另一个字符串
String str Hello, World!;
String newStr str.replace(World, Java); // newStr 内容为 Hello, Java!使用 replaceAll 方法
replaceAll 方法使用正则表达式替换字符串中的所有匹配项
String str Hello, World! Hello!;
String newStr str.replaceAll(Hello, Hi); // newStr 内容为 Hi, World! Hi!5. 字符串分割
使用 split 方法
split 方法使用正则表达式将字符串分割为子字符串数组
String str apple,banana,orange;
String[] fruits str.split(,); // fruits 为 [apple, banana, orange]6. 字符串子串
使用 substring 方法
substring 方法用于提取字符串中的子字符串
String str Hello, World!;
String subStr str.substring(7); // subStr 内容为 World!
String subStr2 str.substring(7, 12); // subStr2 内容为 World7. 字符串大小写转换
使用 toUpperCase 和 toLowerCase 方法
toUpperCase 方法将字符串转换为大写toLowerCase 方法将字符串转换为小写
String str Hello, World!;
String upperStr str.toUpperCase(); // upperStr 内容为 HELLO, WORLD!
String lowerStr str.toLowerCase(); // lowerStr 内容为 hello, world!8. 去除字符串两端的空格
使用 trim 方法
trim 方法用于去除字符串两端的空格
String str Hello, World! ;
String trimmedStr str.trim(); // trimmedStr 内容为 Hello, World!三、常用的 String 类方法
1. length
length 方法返回字符串的长度
String str Hello, World!;
int length str.length(); // length 为 132. charAt
charAt 方法返回字符串中指定位置的字符
String str Hello, World!;
char ch str.charAt(7); // ch 为 W3. contains
contains 方法检查字符串是否包含指定的子字符串
String str Hello, World!;
boolean contains str.contains(World); // contains 为 true4. startsWith 和 endsWith
startsWith 方法检查字符串是否以指定的子字符串开头endsWith 方法检查字符串是否以指定的子字符串结尾
String str Hello, World!;
boolean starts str.startsWith(Hello); // starts 为 true
boolean ends str.endsWith(World!); // ends 为 true5. isEmpty
isEmpty 方法检查字符串是否为空长度为 0
String str ;
boolean isEmpty str.isEmpty(); // isEmpty 为 true6. intern
intern 方法将字符串放入字符串池中并返回字符串池中的字符串引用
String str1 new String(Hello);
String str2 str1.intern();
String str3 Hello;
boolean isSame (str2 str3); // isSame 为 true7. format
format 方法使用指定的格式字符串和参数返回一个格式化的字符串
String name Alice;
int age 25;
String formattedStr String.format(Name: %s, Age: %d, name, age); // formattedStr 内容为 Name: Alice, Age: 258. valueOf
valueOf 方法将其他类型的数据转换为字符串
int number 123;
String str String.valueOf(number); // str 内容为 123四、StringBuilder 和 StringBuffer
1. StringBuilder
StringBuilder 类用于创建可变的字符串它不是线程安全的但效率较高
StringBuilder sb new StringBuilder(Hello);
sb.append(, World!);
String result sb.toString(); // result 内容为 Hello, World!2. StringBuffer
StringBuffer 类也是用于创建可变的字符串它是线程安全的但效率较低
StringBuffer sb new StringBuffer(Hello);
sb.append(, World!);
String result sb.toString(); // result 内容为 Hello, World!Java 的字符串处理功能非常强大通过 String 类和 StringBuilder、StringBuffer 类提供了丰富的方法来创建、操作和管理字符串。理解和掌握这些方法对于提高 Java 编程能力至关重要。
黑马程序员免费预约咨询