length() 返回该字符串的长度
equals() 将此字符串与指定对象进行比较。
isEmpty() 判断字符串内容是否为空
charAt(指定索引值) 返回指定索引处的char值。
contains() 当且仅当此字符串包含指定的char值序列时返回true。
indexOf(字符) 返回该字符串中指定子字符串第一次出现的索引。
replace(“被替换的字符”,“替换的内容”) 替换指定字符串为指定内容
substring(int) 截取指定索引位置字符到末尾
substring(int,int) 截取范围索引字符,包含开始索引不包含结束索引
toCharArray() 将此字符串转换为新的字符数组。
split() 根据传入的符号进行分割字符串.
public static void main(String[] args) {String str = "今天天气真不错!";//length() 返回该字符串的长度int length = str.length();System.out.println("字符串长度:"+length); //8
}
public static void main(String[] args) {String str = "今天天气真不错!";//equals() 将此字符串与指定对象进行比较。// 当且仅当参数不为空且为String对象,表示与此对象相同的字符序列时,结果为真boolean f = str.equals("哈哈");boolean ff = str.equals("今天天气真不错!");System.out.println("字符串内容不相等:"+ f); //falseSystem.out.println("字符串内容相等:"+ ff); //true
}
public static void main(String[] args) {String str = "今天天气真不错!";//isEmpty() 判断字符串内容是否为空boolean empty = str.isEmpty();System.out.println("字符串不为空:"+empty); //falseString s = "";boolean empty1 = s.isEmpty();System.out.println("字符串为空:"+empty1); // true
}
public static void main(String[] args) {String str = "今天天气真不错!";//charAt(指定索引值) 返回指定索引处的char值。char c = str.charAt(3);System.out.println("索引为3的字符:"+c); //气
}
public static void main(String[] args) {String str = "今天天气真不错!";//contains() 当且仅当此字符串包含指定的char值序列时返回true。boolean contains = str.contains("不错");boolean contains1 = str.contains("哈哈");System.out.println("字符串中是否包含传入的字符:"+contains); //trueSystem.out.println("字符串中是否包含传入的字符:"+contains1); //false
}
public static void main(String[] args) {String str = "今天天气真不错!";//indexOf(字符) 返回该字符串中指定子字符串第一次出现的索引。//如果不存在,则返回-1。int indexOf = str.indexOf("天"); //1int indexOf2 = str.indexOf("哈"); //-1System.out.println("返回在字符串中指定字符第一次出现的索引位置并返回:"+indexOf);System.out.println("字符串中不存在传入字符时返回:"+indexOf2);
}
public static void main(String[] args) {String str = "今天天气真不错!";//replace(“被替换的字符”,“替换的内容”) 替换指定字符串为指定内容String replace = str.replace("天", "哈");System.out.println("替换指定字符后:"+replace); // 今哈哈气真不错!
}
public static void main(String[] args) {String str = "今天天气真不错!";//substring(int) 截取指定索引位置字符到末尾//substring(int,int) 截取范围索引字符,包含开始索引不包含结束索引String substring = str.substring(3);String substring1 = str.substring(1, 6);System.out.println("截取指定索引位置字符到末尾:"+substring); //气真不错!System.out.println("截取范围索引字符,包含开始索引不包含结束索引:"+substring1); // 天天气真不}
public static void main(String[] args) {String str = "今天天气真不错!";// toCharArray() 将此字符串转换为新的字符数组。char[] chars = str.toCharArray();System.out.println("将字符串转换为字符数组并遍历:");for (int i = 0; i < chars.length; i++) {if (i == chars.length-1){System.out.println(chars[i]);}else {System.out.print(chars[i]+"/");}}// 今/天/天/气/真/不/错/!}
public static void main(String[] args) {//split() 根据传入的符号进行分割字符串.// 围绕给定正则表达式的匹配来拆分此字符串。 String sp = "ha和a后a安全";String[] split = sp.split("a");System.out.println("根据传入的符号进行分割字符串并遍历:");for (int i = 0; i < split.length; i++) {if (i == split.length-1){System.out.println(split[i]);}else {System.out.print(split[i]+"/");}}// h/和/后/安全}
上一篇:常用设计模式的正确应用