ui作品集 网站怎么做,网站制作加我,网上申请营业执照入口,苏州网站开发公司兴田德润在哪儿String类 字符串相减/替换
使用 String 类的 replace() 方法来实现这个功能: replace(“target”,“replacement”)
如果你想将引号添加到字符串中#xff0c;你可以使用转义字符 \ 来表示双引号。
String str1 abcdefg;
String str2 bcd…String类 字符串相减/替换
使用 String 类的 replace() 方法来实现这个功能: replace(“target”,“replacement”)
如果你想将引号添加到字符串中你可以使用转义字符 \ 来表示双引号。
String str1 abcdefg;
String str2 bcd;
String result str1.replace(str2, );
System.out.println(result); // 输出: aefg字符串连接
使用 String 类的 concat() 方法来实现这个功能: contact(String s)
也可以用StringBuilder类来添加空间效率更快 String s 菜鸟教程;s s.concat(网址:).concat(www.runoob.com);System.out.println(s); //输出 菜鸟教程网址:www.runoob.com比较子/字符串是否相同
equals() 方法
String str1 abc;
String str2 123a;
str1.equals(str2); //输出 false
abc.equals(str2); //输出 trueequalsIgnoreCase() 方法 --不区分大小写
String str1 abc;
String str2 ABC;
System.out.println(str1.equalsIgnoreCase(str2)); // 输出 truecompareTo() 方法
compareTo() 方法用于按字典顺序比较两个字符串的大小该比较是基于字符串各个字符的 Unicode 值。
String str1 A;
String str2 a;
System.out.println(str1.compareTo(str2)); // a - A 输出 32
System.out.println(str2.compareTo(str1)); // A - a 输出 -32
System.out.println(str2.compareTo(a)); // 相同输出 0字符串是否包含
contains() 方法
String str abcdef;
boolean statue srt.contains(bc); // statue true;获取子字符串 substring(int beginIndex) 这个版本的substring方法从指定的索引位置开始提取字符串的一部分一直到字符串的末尾。 substring(int beginIndex, int endIndex) 这个版本的substring方法从指定的起始索引位置开始提取字符串的一部分直到结束索引之前的位置。 public class SubstringExample {public static void main(String[] args) {String s Hello, world!;// 第1个substring方法// 使用单个索引提取从索引2开始到末尾的子字符串String substring1 s.substring(2);System.out.println(Substring 1: substring1); // 输出llo, world!// 第2个substring方法// 使用两个索引提取从索引7到索引12之前的子字符串即 7 - 11 String substring2 s.substring(7, 12);System.out.println(Substring 2: substring2); // 输出world// 提取索引4之前的子字符串索引4不包括在内String substring3 s.substring(0, 4);System.out.println(Substring 3: substring3); // 输出Hell}
}查找 字符/串 位置 int indexOf(String str) 这个版本的 indexOf 方法用于查找指定子字符串在字符串中第一次出现的索引位置。 int indexOf(String str,int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引如果此字符串中没有这样的字符则返回 -1。 public class IndexOfExample {public static void main(String[] args) {String text Hello, world! This is a sample text.;// 第1个indexOf方法// 查找字符 , 第一次出现的索引int commaIndex text.indexOf(,);System.out.println( commaIndex); // 输出 5// 查找子字符串 world 第一次出现的索引int worldIndex text.indexOf(world);System.out.println( worldIndex); // 输出 7// 查找不存在的字符或子字符串int notFoundIndex text.indexOf(123);System.out.println( notFoundIndex); // 输出-1// 第二个indexOf方法String t text.indexOf(o,6);System.out.println( t); // 输出 8}
}