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,则返回的子字符串为空字符串。
全部评论

相关推荐

10-20 11:11
辽宁大学 营销
点赞 评论 收藏
分享
10-13 12:53
已编辑
湖北工业大学 前端工程师
小海c:包装一下,第一个感觉是字节青训营的那个,后面那个是黑马的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务