java例题学习:字符串的遍历
来源:互联网 发布:流程优化的好处 编辑:程序博客网 时间:2024/06/18 08:00
package cn.itcast_04;
/*
* 需求:遍历获取字符串中的每一个字符
*
* 分析:
* A:如何能够拿到每一个字符呢?
* char charAt(int index)
* B:我怎么知道字符到底有多少个呢?
* int length()
*/
public class StringTest {
public static void main(String[] args) {
// 定义字符串
String s = "helloworld";
// 原始版本
// System.out.println(s.charAt(0));
// System.out.println(s.charAt(1));
// System.out.println(s.charAt(2));
// System.out.println(s.charAt(3));
// System.out.println(s.charAt(4));
// System.out.println(s.charAt(5));
// System.out.println(s.charAt(6));
// System.out.println(s.charAt(7));
// System.out.println(s.charAt(8));
// System.out.println(s.charAt(9));
// 只需要我们从0取到9
// for (int x = 0; x < 10; x++) {
// System.out.println(s.charAt(x));
// }
// 如果长度特别长,我不可能去数,所以我们要用长度功能
for (int x = 0; x < s.length(); x++) {
// char ch = s.charAt(x);
// System.out.println(ch);
// 仅仅是输出,我就直接输出了
System.out.println(s.charAt(x));
}
}
}
/*
* 需求:遍历获取字符串中的每一个字符
*
* 分析:
* A:如何能够拿到每一个字符呢?
* char charAt(int index)
* B:我怎么知道字符到底有多少个呢?
* int length()
*/
public class StringTest {
public static void main(String[] args) {
// 定义字符串
String s = "helloworld";
// 原始版本
// System.out.println(s.charAt(0));
// System.out.println(s.charAt(1));
// System.out.println(s.charAt(2));
// System.out.println(s.charAt(3));
// System.out.println(s.charAt(4));
// System.out.println(s.charAt(5));
// System.out.println(s.charAt(6));
// System.out.println(s.charAt(7));
// System.out.println(s.charAt(8));
// System.out.println(s.charAt(9));
// 只需要我们从0取到9
// for (int x = 0; x < 10; x++) {
// System.out.println(s.charAt(x));
// }
// 如果长度特别长,我不可能去数,所以我们要用长度功能
for (int x = 0; x < s.length(); x++) {
// char ch = s.charAt(x);
// System.out.println(ch);
// 仅仅是输出,我就直接输出了
System.out.println(s.charAt(x));
}
}
}
0 0
- java例题学习:字符串的遍历
- 学习java的经典例题
- java学习例题:对象的使用
- java例题学习:HashSet
- java例题学习:接口小例题
- java例题学习:字符流例题
- 广度优先遍历的几个例题
- 适合新手学习的Java语言 例题 【3】
- java例题学习:简单的继承代码小片段
- 【C++学习】-字符串的遍历---ShinePans
- java例题学习:面向对象之长方形例题学习
- JAVA学习--集合的遍历
- 例题6-7 树的层次遍历 BFS遍历
- vue的学习记录-例题
- java 字符串遍历
- JAVA遍历json字符串
- Java 正确遍历字符串
- java字符串的学习
- 工作四年,三年的Android码农,感觉自己还是很菜!
- Shell函数学习
- android EventBus
- Git使用学习一
- 软连接与硬链接的区别
- java例题学习:字符串的遍历
- mysql的distinct用法-mysql中如何筛选出非重复的数据
- 王朝第十一周 第几天
- 360兼容模式,搜狗等奇葩浏览器下无法正常渲染的问题
- FORM表单防止页面重复提交
- 《Android第一行代码》,陌生单词及释意
- 软件测试工程师的6+1种技能
- 如何在QT环境中使用openCV
- react ntive snippet的妙用