本视频是解读性视频,所以希望您已经看过了本知识点的内容,并且编写了相应的代码之后,带着疑问来观看,这样收获才多。 不建议一开始就观看视频
![]() 6分23秒 本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器。 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)。 chrome 的 视频下载插件会影响播放,如 IDM 等,请关闭或者切换其他浏览器
示例 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"
得到一个新的字符串,每个单词的首字母都转换为大写
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
本视频是解读性视频,所以希望您已经看过了本答案的内容,带着疑问来观看,这样收获才多。 不建议一开始就观看视频
![]() 1分31秒 本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器。 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)。 chrome 的 视频下载插件会影响播放,如 IDM 等,请关闭或者切换其他浏览器 package character;
public class TestString {
public static void main(String[] args) {
// 给出一句英文句子: "let there be light"
// 得到一个新的字符串,每个单词的首字母都转换为大写
String s = "let there be light";
System.out.println(s);
String[] words = s.split(" ");
for (int i = 0; i < words.length; i++) {
String word = words[i];
char upperCaseFirstWord =Character.toUpperCase(word.charAt(0));
String rest = word.substring(1);
String capitalizedWord = upperCaseFirstWord + rest;
words[i] = capitalizedWord;
}
String result = "";
for (String word : words) {
result+= word + " ";
}
result= result.trim();
System.out.println(result);
}
}
英文绕口令
peter piper picked a peck of pickled peppers 统计这段绕口令有多少个以p开头的单词
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
本视频是解读性视频,所以希望您已经看过了本答案的内容,带着疑问来观看,这样收获才多。 不建议一开始就观看视频
![]() 1分49秒 本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器。 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)。 chrome 的 视频下载插件会影响播放,如 IDM 等,请关闭或者切换其他浏览器 package character;
public class TestString {
public static void main(String[] args) {
// peter piper picked a peck of pickled peppers
// 统计这段绕口令有多少个以p开头的单词
String s = "peter piper picked a peck of pickled peppers";
System.out.println(s);
String[] words = s.split(" ");
int count= 0;
for (int i = 0; i < words.length; i++) {
String word = words[i];
char firstWord =word.charAt(0);
if(firstWord=='p')
count++;
}
System.out.printf("总共有%d个p开头的单词",count);
}
}
把 lengendary 改成间隔大写小写模式,即 LeNgEnDaRy
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
本视频是解读性视频,所以希望您已经看过了本答案的内容,带着疑问来观看,这样收获才多。 不建议一开始就观看视频
![]() 1分30秒 本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器。 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)。 chrome 的 视频下载插件会影响播放,如 IDM 等,请关闭或者切换其他浏览器 package character;
public class TestString {
public static void main(String[] args) {
// 把 lengendary 改成间隔大写小写模式,即 LeNgEnDaRy
String s = "lengendary";
char[] cs =s.toCharArray();
int count= 0;
for (int i = 0; i < cs.length; i++) {
if(0==i%2)
cs[i] = Character.toUpperCase(cs[i]);
}
String result = new String(cs);
System.out.printf(result);
}
}
package character; public class TestString { public static void main(String[] args) { // 把 lengendary 改成间隔大写小写模式,即 LeNgEnDaRy String s = "lengendary"; char[] cs =s.toCharArray(); int count= 0; for (int i = 0; i < cs.length; i++) { if(0==i%2) cs[i] = Character.toUpperCase(cs[i]); } String result = new String(cs); System.out.printf(result); } }
把 lengendary 最后一个字母变大写
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
本视频是解读性视频,所以希望您已经看过了本答案的内容,带着疑问来观看,这样收获才多。 不建议一开始就观看视频
![]() 56秒 本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器。 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)。 chrome 的 视频下载插件会影响播放,如 IDM 等,请关闭或者切换其他浏览器 package character;
public class TestString {
public static void main(String[] args) {
// 把 lengendary 最后一个字母变大写
String s = "lengendary";
char[] cs = s.toCharArray();
cs[cs.length - 1] = Character.toUpperCase(cs[cs.length-1]);
String result = new String(cs);
System.out.printf(result);
}
}
package character; public class TestString { public static void main(String[] args) { // 把 lengendary 最后一个字母变大写 String s = "lengendary"; char[] cs = s.toCharArray(); cs[cs.length - 1] = Character.toUpperCase(cs[cs.length-1]); String result = new String(cs); System.out.printf(result); } }
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单词首字母大写
在查看答案前,尽量先自己完成,碰到问题再来查看答案,收获会更多
本视频是解读性视频,所以希望您已经看过了本答案的内容,带着疑问来观看,这样收获才多。 不建议一开始就观看视频
![]() 1分43秒 本视频采用html5方式播放,如无法正常播放,请将浏览器升级至最新版本,推荐火狐,chrome,360浏览器。 如果装有迅雷,播放视频呈现直接下载状态,请调整 迅雷系统设置-基本设置-启动-监视全部浏览器 (去掉这个选项)。 chrome 的 视频下载插件会影响播放,如 IDM 等,请关闭或者切换其他浏览器 package character;
public class TestString {
public static void main(String[] args) {
// 把最后一个two单词首字母大写
String s = "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";
int index = s.lastIndexOf(" two ");
char[] cs = s.toCharArray();
cs[index +1] = Character.toUpperCase(cs[index+1]);
String result = new String(cs);
System.out.printf(result);
}
}
package character; public class TestString { public static void main(String[] args) { // 把最后一个two单词首字母大写 String s = "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"; int index = s.lastIndexOf(" two "); char[] cs = s.toCharArray(); cs[index +1] = Character.toUpperCase(cs[index+1]); String result = new String(cs); System.out.printf(result); } }
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
![]()
问答区域
2024-08-30
我的答案,欢迎批评指正
回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
2024-08-21
最后two大写
回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
2024-07-03
一种更快速、更简洁的方法求解绕口令问题
2023-04-14
间隔大写小写模式
2022-10-23
不一样的答案
提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 147 条以前的提问,请 点击查看
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|