本视频是解读性视频,所以希望您已经看过了本知识点的内容,并且编写了相应的代码之后,带着疑问来观看,这样收获才多。 不建议一开始就观看视频
![]() 6分23秒 本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)
示例 2 : 获取对应的字符数组 示例 3 : 截取子字符串 示例 4 : 分隔 示例 5 : 去掉首尾空格 示例 6 : 大小写 示例 7 : 定位 示例 8 : 替换 示例 9 : 练习-每个单词的首字母都转换为大写 示例 10 : 答案-每个单词的首字母都转换为大写 示例 11 : 练习-英文绕口令 示例 12 : 答案-英文绕口令 示例 13 : 练习-间隔大写小写模式 示例 14 : 答案-间隔大写小写模式 示例 15 : 练习-最后一个字母变大写 示例 16 : 答案-最后一个字母变大写 示例 17 : 练习-把最后一个two单词首字母大写 示例 18 : 答案-把最后一个two单词首字母大写
charAt(int index)获取指定位置的字符
package character;
public class TestString {
public static void main(String[] args) {
String sentence = "盖伦,在进行了连续8次击杀后,获得了 超神 的称号";
char c = sentence.charAt(0);
System.out.println(c);
}
}
package character; public class TestString { public static void main(String[] args) { String sentence = "盖伦,在进行了连续8次击杀后,获得了 超神 的称号"; char c = sentence.charAt(0); System.out.println(c); } }
toCharArray()
获取对应的字符数组 package character;
public class TestString {
public static void main(String[] args) {
String sentence = "盖伦,在进行了连续8次击杀后,获得了超神 的称号";
char[] cs = sentence.toCharArray(); //获取对应的字符数组
System.out.println(sentence.length() == cs.length);
}
}
package character; public class TestString { public static void main(String[] args) { String sentence = "盖伦,在进行了连续8次击杀后,获得了超神 的称号"; char[] cs = sentence.toCharArray(); //获取对应的字符数组 System.out.println(sentence.length() == cs.length); } }
subString
截取子字符串 package character;
public class TestString {
public static void main(String[] args) {
String sentence = "盖伦,在进行了连续8次击杀后,获得了 超神 的称号";
//截取从第3个开始的字符串 (基0)
String subString1 = sentence.substring(3);
System.out.println(subString1);
//截取从第3个开始的字符串 (基0)
//到5-1的位置的字符串
//左闭右开
String subString2 = sentence.substring(3,5);
System.out.println(subString2);
}
}
split
根据分隔符进行分隔 package character;
public class TestString {
public static void main(String[] args) {
String sentence = "盖伦,在进行了连续8次击杀后,获得了 超神 的称号";
//根据,进行分割,得到3个子字符串
String subSentences[] = sentence.split(",");
for (String sub : subSentences) {
System.out.println(sub);
}
}
}
package character; public class TestString { public static void main(String[] args) { String sentence = "盖伦,在进行了连续8次击杀后,获得了 超神 的称号"; //根据,进行分割,得到3个子字符串 String subSentences[] = sentence.split(","); for (String sub : subSentences) { System.out.println(sub); } } }
trim
去掉首尾空格 package character;
public class TestString {
public static void main(String[] args) {
String sentence = " 盖伦,在进行了连续8次击杀后,获得了 超神 的称号 ";
System.out.println(sentence);
//去掉首尾空格
System.out.println(sentence.trim());
}
}
package character; public class TestString { public static void main(String[] args) { String sentence = " 盖伦,在进行了连续8次击杀后,获得了 超神 的称号 "; System.out.println(sentence); //去掉首尾空格 System.out.println(sentence.trim()); } }
toLowerCase 全部变成小写
toUpperCase 全部变成大写 package character;
public class TestString {
public static void main(String[] args) {
String sentence = "Garen";
//全部变成小写
System.out.println(sentence.toLowerCase());
//全部变成大写
System.out.println(sentence.toUpperCase());
}
}
package character; public class TestString { public static void main(String[] args) { String sentence = "Garen"; //全部变成小写 System.out.println(sentence.toLowerCase()); //全部变成大写 System.out.println(sentence.toUpperCase()); } }
indexOf 判断字符或者子字符串出现的位置
contains 是否包含子字符串 package character;
public class TestString {
public static void main(String[] args) {
String sentence = "盖伦,在进行了连续8次击杀后,获得了超神 的称号";
System.out.println(sentence.indexOf('8')); //字符第一次出现的位置
System.out.println(sentence.indexOf("超神")); //字符串第一次出现的位置
System.out.println(sentence.lastIndexOf("了")); //字符串最后出现的位置
System.out.println(sentence.indexOf(',',5)); //从位置5开始,出现的第一次,的位置
System.out.println(sentence.contains("击杀")); //是否包含字符串"击杀"
}
}
package character; public class TestString { public static void main(String[] args) { String sentence = "盖伦,在进行了连续8次击杀后,获得了超神 的称号"; System.out.println(sentence.indexOf('8')); //字符第一次出现的位置 System.out.println(sentence.indexOf("超神")); //字符串第一次出现的位置 System.out.println(sentence.lastIndexOf("了")); //字符串最后出现的位置 System.out.println(sentence.indexOf(',',5)); //从位置5开始,出现的第一次,的位置 System.out.println(sentence.contains("击杀")); //是否包含字符串"击杀" } }
replaceAll 替换所有的
replaceFirst 只替换第一个 package character;
public class TestString {
public static void main(String[] args) {
String sentence = "盖伦,在进行了连续8次击杀后,获得了超神 的称号";
String temp = sentence.replaceAll("击杀", "被击杀"); //替换所有的
temp = temp.replaceAll("超神", "超鬼");
System.out.println(temp);
temp = sentence.replaceFirst(",","");//只替换第一个
System.out.println(temp);
}
}
package character; public class TestString { public static void main(String[] args) { String sentence = "盖伦,在进行了连续8次击杀后,获得了超神 的称号"; String temp = sentence.replaceAll("击杀", "被击杀"); //替换所有的 temp = temp.replaceAll("超神", "超鬼"); System.out.println(temp); temp = sentence.replaceFirst(",","");//只替换第一个 System.out.println(temp); } }
给出一句英文句子: "let there be light"
得到一个新的字符串,每个单词的首字母都转换为大写
英文绕口令
peter piper picked a peck of pickled peppers 统计这段绕口令有多少个以p开头的单词
把 lengendary 改成间隔大写小写模式,即 LeNgEnDaRy
把 lengendary 最后一个字母变大写
Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak
把最后一个two单词首字母大写
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
![]()
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|