public class StringTest {
public static void main(String[] args) {
// 创建字符串的几种方式
// 第一种
String s1 = "Hello world";
// new对象
String s2 = new String("Hello world");
// 返回常量池引用
String s3 = s2.intern();
}
}
indexOf(String str) 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
indexOf(String str, int fromIndex) 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。如果此字符串中没有这样的字符,则返回 -1。
substring() 方法返回字符串的子字符串。
public String substring(int beginIndex)
public String substring(int beginIndex, int endIndex)
beginIndex -- 起始索引(包括), 索引从 0 开始。
endIndex -- 结束索引(不包括)。
charAt() 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1。
public class Test {
public static void main(String args[]) {
String s = "www.runoob.com";
char result = s.charAt(6);
System.out.println(result);
}
}
index -- 字符的索引。
描述:trim方法只会将字符串两端的空格格式化掉,并不会处理格式化字符串中间的空格
System.out.println(" 我 是 trim ".trim());
输出:
我 是 trim
描述:String.join()方法是JDK1.8之后新增的一个静态方法。
String result = String.join("-","a", "b", "c", "d");
输出结果:a-b-c-d
List names=new ArrayList<String>();
names.add("1");
names.add("2");
names.add("3");
System.out.println(String.join("-", names));
输出结果:1-2-3
String[] arrStr=new String[]{"a","b","c"};
System.out.println(String.join("-", arrStr));
输出结果:a-b-c
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有