前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【背诵①】保姆级 | 零基础备赛蓝桥杯Java组| 字符串

【背诵①】保姆级 | 零基础备赛蓝桥杯Java组| 字符串

作者头像
命运之光
发布2024-04-15 08:24:11
600
发布2024-04-15 08:24:11
举报

下面是使用Java字符串方法的一些具体例子:

1、使用 nextLine() 方法获取一行输入:

代码语言:javascript
复制
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
System.out.println("输入的字符串是:" + s);

2、使用 charAt(int index) 方法获取指定索引位置的字符:

代码语言:javascript
复制
String str = "Hello";
char c = str.charAt(2);
System.out.println("索引位置2的字符是:" + c); // 输出 "l"

3、使用 indexOf(int ch) 方法查找字符在字符串中的索引位置:

代码语言:javascript
复制
String text = "Hello World";
int index = text.indexOf('o');
System.out.println("字符 'o' 第一次出现的索引位置是:" + index); // 输出 "4"

4、使用 compareTo(String anotherString) 方法比较字符串的字典顺序:

代码语言:javascript
复制
String str1 = "abc";
String str2 = "def";
int result = str1.compareTo(str2);
System.out.println("比较结果:" + result); // 输出 "-3",因为 "abc" 在字典顺序上小于 "def"

5、使用 concat(String str) 方法连接两个字符串:

代码语言:javascript
复制
String s1 = "Hello";
String s2 = "World";
String result = s1.concat(s2);
System.out.println("连接后的字符串:" + result); // 输出 "HelloWorld"

6、使用 length() 方法获取字符串的长度:

代码语言:javascript
复制
String str = "Hello";
int length = str.length();
System.out.println("字符串的长度是:" + length); // 输出 "5"

7、使用 equals(Object anObject) 方法比较字符串是否相等:

代码语言:javascript
复制
String s1 = "hello";
String s2 = "world";
boolean isEqual1 = s1.equals(s2);
boolean isEqual2 = s1.equals("hello");
System.out.println("s1 equals s2: " + isEqual1); // 输出 "false"
System.out.println("s1 equals \"hello\": " + isEqual2); // 输出 "true"

8、substring(int beginIndex, int endIndex) 方法:截取字符串的子串,从指定的起始索引(包括)到指定的结束索引(不包括)。

代码语言:javascript
复制
String str = "Hello World";
String substr = str.substring(6); // 截取从索引6开始到字符串结束的子串
System.out.println("截取的子串:" + substr); // 输出 "World"

9、toLowerCase()toUpperCase() 方法:将字符串转换为小写或大写。

代码语言:javascript
复制
String str = "Hello";
String lowerCase = str.toLowerCase(); // 转换为小写
String upperCase = str.toUpperCase(); // 转换为大写
System.out.println("转换为小写:" + lowerCase); // 输出 "hello"
System.out.println("转换为大写:" + upperCase); // 输出 "HELLO"

10、trim() 方法:去除字符串前后的空格。

代码语言:javascript
复制
String str = "   Hello   ";
String trimmed = str.trim();
System.out.println("去除空格后的字符串:" + trimmed); // 输出 "Hello"

11、replace(char oldChar, char newChar) 方法:替换字符串中的字符。

代码语言:javascript
复制
String str = "Hello World";
String replaced = str.replace('o', 'x'); // 将字符 'o' 替换为 'x'
System.out.println("替换后的字符串:" + replaced); // 输出 "Hellx Wxrld"

12、startsWith(String prefix)endsWith(String suffix) 方法:检查字符串是否以指定的前缀或后缀开头或结尾。

代码语言:javascript
复制
String str = "Hello World";
boolean startsWithHello = str.startsWith("Hello");
boolean endsWithWorld = str.endsWith("World");
System.out.println("是否以Hello开头:" + startsWithHello); // 输出 "true"
System.out.println("是否以World结尾:" + endsWithWorld); // 输出 "true"
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-04-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档