Java字符串字符
来源:互联网 发布:淘宝转运香港 编辑:程序博客网 时间:2024/05/17 21:51
字符所在的索引
可以使用charAt()
方法从String
对象中获取指定索引处的字符。索引值是从零开始的。以下代码在“YIIBAI.COM
”字符串中打印索引值和每个索引处对应的字符:
public class Main { public static void main(String[] args) { String str = "YIIBAI.COM"; // Get the length of string YIIBAI.COM int len = str.length(); // Loop through all characters and print their indexes for (int i = 0; i < len; i++) { System.out.println(str.charAt(i) + " has index " + i); } }}
上面的代码生成以下结果。
Y has index 0I has index 1I has index 2B has index 3A has index 4I has index 5. has index 6C has index 7O has index 8M has index 9
测试字符串是否为空
测试String
对象是否为空,空字符串的长度为零。有三种方法可以检查空字符串:
- isEmpty() 方法.
- equals() 方法.
- 获取字符串的长度,并检查它是否为零。
以下代码显示如何使用三种方法:
public class Main { public static void main(String[] args) { String str1 = "Hello"; String str2 = ""; // Using the isEmpty() method boolean empty1 = str1.isEmpty(); // Assigns false to empty1 boolean empty2 = str2.isEmpty(); // Assigns true to empty1 // Using the equals() method boolean empty3 = "".equals(str1); // Assigns false to empty3 boolean empty4 = "".equals(str2); // Assigns true to empty4 // Comparing length of the string with 0 boolean empty5 = str1.length() == 0; // Assigns false to empty5 boolean empty6 = str2.length() == 0; // Assigns true to empty6 }}
更改字符的大小写
要将字符串的内容转换为小写和大写,请分别使用toLowerCase()
和toUpperCase()
方法。
String str1 = new String("Hello"); // str1 contains "Hello" String str2 = str1.toUpperCase(); // str2 contains "HELLO" String str3 = str1.toLowerCase(); // str3 contains "hello"
0 0
- Java 字节 字符 字符串
- Java 字节 字符 字符串
- java 字节,字符,字符串
- java字符串与字符
- Java基础:字符、字符串
- Java字符串字符
- Java字符串、转义字符和字符统计
- java 去掉字符串中的'/'字符
- java基础 ---字符和字符串
- java字符串截取后面字符
- Java字符串切分转义字符
- Java 字符与字符串运算
- Java--字符和字符串运算
- Java字符与字符串(基础)
- java 字符串中插入字符
- Java过滤字符串无效字符
- java转换 HTML字符实体,java特殊字符转义字符串
- JAVA 高效判断字符串的字符个数
- 自然语言处理中的 Attention Model
- Java字符串比较
- 010Editor Cracked分析详文
- navicat mysql自增主键的设置
- [李景山php]每天laravel[005]-入口文件 bootstrap / app.php
- Java字符串字符
- magento的一些不足
- Java字符串搜索
- 六 访问权限控制
- 10.排序及相关操作
- Java字符串转换
- CF中app的stage过程
- 《opencv2》 遍历图像与邻域操作
- linux下shell脚本学习