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

相关推荐

吴offer选手:HR:我KPI到手了就行,合不合适关我什么事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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