|NO.Z.00020|——————————|BigDataEnd|——|Java&核心类库.V05|——|Java.v05|string类.v05|字符获取使用|
一、string类型和数组之间的转换:常用的成员方法(练熟、记住)
方法声明 | 功能介绍 |
char charAt(int index) | 方法charAt用于返回字符串指定位置的字符。 |
int length() | 返回字符串字符序列的长度 |
boolean isEmpty() | 判断字符串是否为空 |
package com.yanqi.task12;
public class StringCharTest {
public static void main(String[] args) {
// 1.构造String类型的对象并打印
String str1 = new String("hello");
System.out.println("str1 = " + str1); // hello
// 2.获取字符串的长度和每个字符并打印
System.out.println("字符串的长度是:" + str1.length()); // 5
System.out.println("下标为0的字符是:" + str1.charAt(0)); // h
System.out.println("下标为1的字符是:" + str1.charAt(1)); // e
System.out.println("下标为2的字符是:" + str1.charAt(2)); // l
System.out.println("下标为3的字符是:" + str1.charAt(3)); // l
System.out.println("下标为4的字符是:" + str1.charAt(4)); // o
//System.out.println("下标为5的字符是:" + str1.charAt(5)); // StringIndexOutOfBoundsException 字符串下标越界异常
System.out.println("----------------------------------------------");
// 3.使用for循环获取所有字符
for (int i = 0; i < str1.length(); i++) {
System.out.println("下标为" + i + "的字符是:" + str1.charAt(i)); // h e l l o
}
System.out.println("----------------------------------------------");
// 4.判断字符串是否为空
System.out.println(0 == str1.length()? "字符串为空": "字符串不为空"); // 不为空
System.out.println(str1.isEmpty()? "字符串为空": "字符串不为空"); // 不为空
System.out.println("----------------------------------------------");
// 5.笔试考点
// 使用两种方式实现字符串"12345"转换为整数12345并打印
String str2 = new String("12345");
// 方式一:调用Integer类中的parseInt()方法即可
int ia = Integer.parseInt(str2);
System.out.println("转换出来的整数是:" + ia); // 12345
// 方式二:利用ASCII来实现类型转换并打印
// '1' - '0' => 1 '2' - '0' => 2 ...
int ib = 0;
for (int i = 0; i < str2.length(); i++) {
ib = ib*10 + (str2.charAt(i) - '0'); // 1 12 ...
}
System.out.println("转换出来的结果是:" + ib); // 12345
System.out.println("----------------------------------------------");
// 如何实现整数到字符串的转换
//String str3 = String.valueOf(ib);
String str3 = "" + ib; // 推荐使用
System.out.println("str3 = " + str3); // 12345
}
}
三、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=51963:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task12.StringCharTest
str1 = hello
字符串的长度是:5
下标为0的字符是:h
下标为1的字符是:e
下标为2的字符是:l
下标为3的字符是:l
下标为4的字符是:o
----------------------------------------------
下标为0的字符是:h
下标为1的字符是:e
下标为2的字符是:l
下标为3的字符是:l
下标为4的字符是:o
----------------------------------------------
字符串不为空
字符串不为空
----------------------------------------------
转换出来的整数是:12345
转换出来的结果是:12345
----------------------------------------------
str3 = 12345
Process finished with exit code 0
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通