常见对象String:
字符串是由多个字符组成的一串数据(字符序列),字符串可以看成是字符数组。
构造方法
class Test {
public static void main(String[] args) {
//1. String():无参构造
String s = new String();
s = "test";
System.out.println(s);
//2. String(byte[] bys):传一个字节数组作为参数
byte[] bys = { 10, 11, 12, 13, 14};
String s1 = new String(bys);
System.out.println(s1);
//3. String(byte[] bys,int index,int length):把字节数组的一部分转换成一个字符串
String s2 = new String(bys, 1, 2);
System.out.println(s2);
//4. String(char[] chs):传一个字符数组作为参数
char[] chs = { 'a', 'b', 'c', 'd', 'e' };
String s3 = new String(chs);
System.out.println(s3);
//5. String(char[] chs,int index,int length):把字符数组的一部分转换成一个字符串
String s4 = new String(chs, 1, 2);
System.out.println(s4);
//6. String(String str):把一个字符串传递过来作为参数
String s5 = new String(s);
System.out.println(s5);
//7. 直接把字符串常量赋值给字符串引用对象(最常用)
String str = "hello";
System.out.println(str);
}
}
String类的判断功能
boolean equals(Object obj)
:比较字符串的内容是否相同,区分大小写。
boolean equalsIgnoreCase(String string)
:比较字符串的内容是否相同,忽略大小写。
boolean contains(String string)
:判断大字符串中是否包含小字符串。
boolean startsWith(String string)
:判断字符串是否以某个指定的字符串开头。
boolean endsWith(String string)
:判断字符串是否以某个指定的字符串结尾。
boolean isEmpty()
:判断字符串是否为空。
String类的常见面试题
String str = new String("hello");
创建了几个对象?
两个,一个"hello"
字符串对象,在方法区的常量池;一个str
对象,在堆内存。
class Test {
public static void main(String[] args) {
// 1.判断定义为 String 类型的 s1 和 s2 是否相等
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);// true
System.out.println(s1.equals(s2));// true
// 2.判断定义为 String 类型的 s1 和 s2 是否相等
String s3 = new String("abc");
String s4 = "abc";
System.out.println(s3 == s4);// false
System.out.println(s3.equals(s4));// true
// 3.判断定义为 String 类型的 s1 和 s2 是否相等
String s5 = "a" + "b" + "c";
String s6 = "abc";
System.out.println(s5 == s6);// true
System.out.println(s5.equals(s6));// true
// 4.判断定义为 String 类型的 s1 和 s2 是否相等
String s7 = "ab";
String s8 = "abc";
String s9 = s7 + "c";
System.out.println(s9 == s8);// false
System.out.println(s9.equals(s8));// true
}
}
String类的获取功能
int length()
:获取字符串长度。
char charAt(int index)
:获取指定索引位置的字符。
int indexOf(int char)
:返回指定字符在此字符中第一次出现处的索引。
int indexOf(String str)
:返回指定字符串在此字符串中第一次出现处的索引。
int indexOf(int ch,int fromIndex)
:返回指定字符在此字符串中从指定位置后第一次出现处的索引。
int indexOf(String str,int fromIndex)
:返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
int lastIndexOf()
:用法和 indexOf 一样。
String substring(int start)
:从指定位置开始截取字符串,默认到末尾。
String substring(int start,int end)
:从指定位置开始到指定位置结束截取字符串。
字符串的遍历
class TestStr {
public static void main(String[] args) {
String str = "abcdef";
for (int i = 0; i < str.length(); i++) {
System.out.print(str.charAt(i) + ", ");
}
}
}
String类的转换功能
byte[] getBytes()
:把字符串转换为字节数组。
char[] toCharArray()
:把字符串转换为字符数组。
static String valueOf(char[] chs)
:把字符数组转成字符串。
static String valueOf(int i)
:把 int 类型的数据转成字符串。
注意:String 类的 valueOf 方法可以把任意类型的数据转成字符串。
打印字符串中字母的出现次数
/**
* 取出一个字符串中字母出现的次数。 如: 字符串: "abcdekka27qoasd123q" ,
* 输出格式为a(3)b(1)c(1)k(2)...
*/
public class Test {
public static void main(String[] args) {
// 定义一个字符串类型变量, 存放欲统计的字符串
String s = "abcdekka27qoasd123q";
count(s);
}
public static void count(String s) {
char[] c = s.toCharArray(); // 将字符串转换为字符数组, 以便操作
// 创建一个 int数组, 长度等于字符串( 字符数组) 的长度, 用于存放字符出现的次数
int[] count = new int[c.length];
/*
* 循环统计字符出现的次数, 将次数存入 count 数组 i 代表 char 数组中正在统计的字符的角标,
* j 代表被比较字符的角标
*/
for (int i = 0; i < c.length; i++) {
if (!Character.isLetter(c[i]))
// 如果当前字符不是字母, 则跳过, 其所对应的 count 为 0
continue;
for (int j = 0; j < i; j++) { //将当前字符和他前面每一个字符进行比较
if (c[i] == c[j]){
// 如果两个字符相等, 则代表是重复元素,
// 角标小的字符(前面的元素)对应的 count 中相同角标位置处数值加 1,
count[j]++; // 角标大的元素对应 count 中数值设为-1, 并进行下个字符的判断;
count[i] = -1;
continue;
}
}
count[i]++; // 如果当前字符与前面每一个字符都不同, 则代表它是第一次出现, 计数加 1
}
// 进行循环输出
for (int i = 0; i < c.length; i++) {
// 如果字符c[i]对应角标为 0, 则表示它不是字母或者不是第一次出现的字母, 不做输出
if (count[i] == 0)
continue;
System.out.print(c[i] + "(" + count[i] + ")"); // 按要求格式输出
}
}
}
String类的其他功能
String
的替换功能及案例演示String replace(char old,char new)
String replace(String old,String new)
String
的去除字符串两空格及案例演示String trim()
int compareTo(String str)
int compareToIgnoreCase(String str)
String toLowerCase()
:把字符串转成小写。String toUpperCase()
:把字符串转成大写。String concat(String str)
:把字符串拼接。
将所有已输入的字符串按字典顺序倒序打印
/**
* 编写程序, 循环接收用户从键盘输入多个字符串, 直到输入“ end” 时循环结束,
* 并将所有已输入的字符串按字典顺序倒序打印。
*/
public class TestStr {
private static StringBuilder stringBuilder = new StringBuilder();
public static void main(String[] args) {
// 循环接收键盘输入的字符串
while (true) {
// 接收键盘输入的字符串
String string = new Scanner(System.in).nextLine();
// 判断输入的是否是: end
if (string.equals("end")) {
// 是, 退出 JVM
stringBuilder.reverse();
System.out.println(stringBuilder);
System.exit(0);
}
stringBuilder.append(string);
}
}
}
希望能帮助到有需要的人。