java.lang.String;实现java示例
时间: 2023-12-19 12:53:31 浏览: 39
下面是一个简单的 Java 示例,演示了如何使用 `String` 类:
```java
public class StringExample {
public static void main(String[] args) {
String str1 = "Hello, world!";
String str2 = new String("Hello, world!");
System.out.println("str1: " + str1);
System.out.println("str2: " + str2);
// 比较两个字符串是否相等
System.out.println("str1 equals str2: " + str1.equals(str2));
System.out.println("str1 == str2: " + (str1 == str2));
// 获取字符串长度
System.out.println("str1 length: " + str1.length());
// 获取字符串的子串
System.out.println("str1 substring(0, 5): " + str1.substring(0, 5));
// 将字符串转换成大写或小写
System.out.println("str1 toUpperCase: " + str1.toUpperCase());
System.out.println("str1 toLowerCase: " + str1.toLowerCase());
}
}
```
输出结果:
```
str1: Hello, world!
str2: Hello, world!
str1 equals str2: true
str1 == str2: false
str1 length: 13
str1 substring(0, 5): Hello
str1 toUpperCase: HELLO, WORLD!
str1 toLowerCase: hello, world!
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)