字符串运算符的分类和示例
时间: 2023-03-29 20:03:40 浏览: 80
字符串运算符可以分为以下几类:
1. 字符串连接运算符:用于将两个字符串连接起来,例如使用“+”运算符将字符串“hello”和“world”连接起来得到“helloworld”。
2. 字符串比较运算符:用于比较两个字符串是否相等,例如使用“==”运算符比较字符串“hello”和“world”,得到结果为false。
3. 字符串截取运算符:用于截取字符串中的一部分,例如使用“substring”方法截取字符串“hello”中的“ell”,得到结果为“ell”。
4. 字符串查找运算符:用于在字符串中查找指定的字符或子串,例如使用“indexOf”方法查找字符串“hello”中是否包含“l”,得到结果为2。
示例:
1. 字符串连接运算符示例:
String str1 = "hello";
String str2 = "world";
String str3 = str1 + str2;
System.out.println(str3); //输出"helloworld"
2. 字符串比较运算符示例:
String str1 = "hello";
String str2 = "world";
if(str1.equals(str2)){
System.out.println("字符串相等");
}else{
System.out.println("字符串不相等");
} //输出"字符串不相等"
3. 字符串截取运算符示例:
String str1 = "hello";
String str2 = str1.substring(1, 4);
System.out.println(str2); //输出"ell"
4. 字符串查找运算符示例:
String str1 = "hello";
int index = str1.indexOf("l");
System.out.println(index); //输出2
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)