package five; public class five { public static void main( String [] args ) { String s1 = " 1234567890QWERTYUIOPqwertyuiop~!@#~$%^^&~我们永远热爱和平 "; System.out.println("s1 = " + s1); int s1_len = s1.length(); System.out.println( s1_len ); String s2 = s1.substring( 10, 20 ); System.out.println( s2 ); char c = s1.charAt( 10 ); System.out.println( c ); String s3 = s1.concat("世界!"); System.out.println( s3 ); String s4 = s1; boolean b1 = s3 == s4; System.out.println( b1 ); int index = s1.indexOf('3'); System.out.println( index ); s1 = s1.trim(); s1.toUpperCase(); System.out.println( s1 ); char cc[] = s1.toCharArray(); System.out.println( java.util.Arrays.toString(cc) ); } }
时间: 2024-01-24 21:04:47 浏览: 177
这是一个Java类,名为five。它包含一个main方法,用于测试一些字符串操作。在main方法中,定义了一个字符串s1,然后对其进行了多种操作:
1. 使用length()方法获取字符串长度,并将其输出。
2. 使用substring()方法从s1中获取子字符串,然后将其输出。
3. 使用charAt()方法获取s1中指定位置的字符,并将其输出。
4. 使用concat()方法将字符串s1和"世界!"拼接起来,并将拼接后的结果输出。
5. 将s1赋值给s4,然后使用"=="运算符比较s3和s4是否相等,并将比较结果输出。
6. 使用indexOf()方法查找字符'3'在s1中第一次出现的位置,并将其输出。
7. 使用trim()方法去除s1首尾的空格,然后使用toUpperCase()方法将其转换为大写字母,并将结果输出。
8. 使用toCharArray()方法将s1转换为字符数组,然后使用Arrays.toString()方法将字符数组转换为字符串,并将其输出。
需要注意的是,虽然使用了trim()和toUpperCase()方法,但是s1本身并没有被修改,因为在Java中String是不可变的。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![text/x-java](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![text/x-java](https://img-home.csdnimg.cn/images/20250102104920.png)
![md](https://img-home.csdnimg.cn/images/20250102104920.png)
![java](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![text/plain; charset=iso-8859-1](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![text/plain](https://img-home.csdnimg.cn/images/20250102104920.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)