一、String类
- String 类简介
String 类是一个字符串类,字符串是由多个字符组成的一串数据(字符序列),也可以看成是字符数组。
注意:字符串是常量,一旦被赋值,就不能被改变。
String类中常用的方法:
1、String类的判断功能
- String类的判断功能
- boolean equals(Object obj) :比较字符串的内容是否相同,区分大小写
- boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
- boolean contains(String str):判断大字符串中是否包含小字符串
- boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
- boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
- boolean isEmpty():判断字符串是否为空
Demo代码示例:
public class StringDemo {
public static void main(String[] args) {
// 创建字符串对象
String s1 = "helloworld";
String s2 = "helloworld";
String s3 = "HelloWorld";
// boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
System.out.println("equals:" + s1.equals(s2)); //true
System.out.println("equals:" + s1.equals(s3)); //false
System.out.println("==================================================");
// boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
System.out.println("equals:" + s1.equalsIgnoreCase(s2)); //true
System.out.println("equals:" + s1.equalsIgnoreCase(s3)); //true
System.out.println("==================================================");
// boolean contains(String str):判断大字符串中是否包含小字符串
System.out.println("contains:" + s1.contains("hello")); //true
System.out.println("contains:" + s1.contains("hw")); //false
System.out.println("==================================================");
// boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
System.out.println("startsWith:" + s1.startsWith("h")); //true
System.out.println("startsWith:" + s1.startsWith("hello")); //true
System.out.println("startsWith:" + s1.startsWith("world")); //false
System.out.println("==================================================");
// 练习:boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
System.out.println("endsWith:" + s1.endsWith("o")); //false
System.out.println("endsWith:" + s1.endsWith("d")); //true
System.out.println("==================================================");
// boolean isEmpty():判断字符串是否为空。
System.out.println("isEmpty:" + s1.isEmpty()); //false
String s4 = "";
String s5 = null;
System.out.println("isEmpty:" + s4.isEmpty());//true
System.out.println("isEmpty:" + s4.isEmpty(s5));//false
// NullPointerException
// s5对象都不存在,所以不能调用方法,空指针异常
System.out.println("isEmpty:" + s5.isEmpty());
}
}
2、String类的获取功能
- String类的获取功能
- int length():获取字符串的长度。
- char charAt(int index):获取指定索引位置的字符
- int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
- int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
- int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
- int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
- String substring(int start):从指定位置开始截取字符串,默认到末尾。
- String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
Demo代码示例:
public class StringDemo {
public static void main(String[] args) {
// 定义一个字符串对象
String s = "helloworld";
// int length():获取字符串的长度。
System.out.println("s.length:" + s.length());//10
// char charAt(int index):获取指定索引位置的字符
System.out.println("charAt:" + s.charAt(7));//r
// int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
System.out.println("indexOf:" + s.indexOf('l')); //2
// int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
System.out.println("indexOf:" + s.indexOf("owo")); //4
// int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
System.out.println("indexOf:" + s.indexOf('l', 4)); // 8
System.out.println("indexOf:" + s.indexOf('k', 4)); // -1
System.out.println("indexOf:" + s.indexOf('l', 40)); // -1
// int indexOf(String str,intfromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
System.out.println("indexOf:" + s.indexOf('o', 1)); //4
// String substring(int start):从指定位置开始截取字符串,默认到末尾。包含start这个索引
System.out.println("substring:" + s.substring(5));//world
System.out.println("substring:" + s.substring(0));//helloworld
// String substring(int start,int
// end):从指定位置开始到指定位置结束截取字符串。包括start索引但是不包end索引
System.out.println("substring:" + s.substring(3, 8)); //loworl
System.out.println("substring:" + s.substring(0, s.length()));//helloworld
}
}
3、String 的转换功能
- String 的转换功能
- byte[] getBytes():把字符串转换为字节数组
- char[] toCharArray():把字符串转换为字符数组
- static String valueOf(char[] chs):把字符数组转成字符串
- static String valueOf(int i):把int类型的数据转成字符串
- String类的valueOf方法可以把任意类型的数据转成字符串
- String toLowerCase():把字符串转成小写
- String toUpperCase():把字符串转成大写
- String concat(String str):把字符串拼接
Demo 代码示例:
public class StringDemo {
public static void main(String[] args) {
// 定义一个字符串对象
String s = "JavaSE";
// byte[] getBytes():把字符串转换为字节数组。
byte[] bys = s.getBytes();
for (int x = 0; x < bys.length; x++) {
System.out.println(bys[x]);
}
System.out.println("----------------");
// char[] toCharArray():把字符串转换为字符数组。
char[] chs = s.toCharArray();
for (int x = 0; x < chs.length; x++) {
System.out.println(chs[x]);
}
System.out.println("----------------");
// static String valueOf(char[] chs):把字符数组转成字符串。
String ss = String.valueOf(chs);
System.out.println(ss);
System.out.println("----------------");
// static String valueOf(int i):把int类型的数据转成字符串。
int i = 100;
String sss = String.valueOf(i);
System.out.println(sss);
System.out.println("----------------");
// String toLowerCase():把字符串转成小写。
System.out.println("toLowerCase:" + s.toLowerCase());
System.out.println("s:" + s);
// System.out.println("----------------");
// String toUpperCase():把字符串转成大写。
System.out.println("toUpperCase:" + s.toUpperCase());
System.out.println("----------------");
// String concat(String str):把字符串拼接。
String s1 = "hello";
String s2 = "world";
String s3 = s1 + s2;
String s4 = s1.concat(s2);
System.out.println("s3:"+s3);
System.out.println("s4:"+s4);
}
}
运行结果:

4、String 的替换功能
- 替换功能:
- String replace(char old,char new):替换指定的单个字符
- String replace(String old,String new):替换指定的字符串
Demo代码示例:
public class StringDemo {
public static void main(String[] args) {
// 替换功能
String s1 = "helloworld";
String s2 = s1.replace('l', 'k');
String s3 = s1.replace("owo", "ak47");
System.out.println("s1:" + s1);//s1:helloworld
System.out.println("s2:" + s2);//s2:hekkoworkd
System.out.println("s3:" + s3);//s3:hellak47rld
}
}
5、将 String 两侧的空格去除
- 去除字符串两空格
- String trim()
Demo代码示例:
public class StringDemo {
public static void main(String[] args) {
// 去除字符串两空格
String s4 = " hello world ";
String s5 = s4.trim();
System.out.println("s4:" + s4 + "---");//s4: hello world ---
System.out.println("s5:" + s5 + "---");//s5:hello world---
}
}
本文详细介绍了Java String类的基本功能,包括字符串比较(equals vs. equalsIgnoreCase)、长度获取、字符访问、查找子串、转换操作(如toByteArray和toUpperCase)以及替换功能。通过实例演示了如何使用这些方法进行字符串处理。
1536

被折叠的 条评论
为什么被折叠?



