java笔记-字符串的处理
String str = "Hello, world!";
// 使用 charAt() 方法获取字符串中指定位置的字符
char firstChar = str.charAt(0);
char fifthChar = str.charAt(5);
int index1 = str.indexOf('o'); // 返回值为 4
int index2 = str.indexOf("world"); // 返回值为 7
indexOf(String str, int fromIndex):从指定的索引位置开始,查找指定字符串在字符串中第一次出现的位置。
String sub2 = str.substring(7, 12); // 返回值为 "world"
如果 beginIndex 等于 endIndex,则返回的子字符串为空字符串。
// 使用 charAt() 方法获取字符串中指定位置的字符
char firstChar = str.charAt(0);
char fifthChar = str.charAt(5);
int index1 = str.indexOf('o'); // 返回值为 4
int index2 = str.indexOf("world"); // 返回值为 7
indexOf(String str, int fromIndex):从指定的索引位置开始,查找指定字符串在字符串中第一次出现的位置。
String sub2 = str.substring(7, 12); // 返回值为 "world"
如果 beginIndex 等于 endIndex,则返回的子字符串为空字符串。
全部评论
相关推荐
投递淘天集团等公司10个岗位 >
点赞 评论 收藏
转发
04-12 00:02
华中师范大学 计算机类 点赞 评论 收藏
转发