本篇博客主要讲解Java基础语法中的 认识 String 类 ,了解 String 类的基本用法,知道字符串的三种常见构造方法。 会使用String对象的比较,知道==、equals、compareTo、compareToIgnoreCase比较的用法和区别。等String类常见用法。 熟练掌握 String 类的常见操作 、认识字符串常量池、 认识 StringBuffer 和 StringBuilder、最后是String类编程题的练习。
大家好,本人是普通一本的在校大学生一枚,目前在学习java。之前也学了一段时间,但是没有发布博客。本人现在已经大二结束了,开学就大三了,时间过的真的很快。我会利用好这个暑假,来复习之前学过的内容,并整理好之前写过的博客进行发布。如果博客中有错误或者没有读懂的地方。热烈欢迎大家在评论区进行讨论!!! 喜欢我文章的兄弟姐妹们可以点赞,收藏和评论我的文章。喜欢我的兄弟姐妹们以及也想复习一遍java知识的兄弟姐妹们可以关注我呦,我会持续更新滴,并且追求完整。 望支持!!!!!!一起加油呀!!!!
语言只是工具,不能决定你好不好找工作,决定你好不好找工作的是你的能力!!!!!
学历本科及以上就够用了!!!!!!!!!!!!!!!!!!!!!!!!!!!!
话不多说,直接上干货
我们都知道String是字符串类型,是引用类型。在java中String也是一个类。
// 使用常量串构造
String s1 = "hello bit";
System.out.println(s1); // 直接newString对象
String s2 = new String("hello bit");
System.out.println(s1); // 使用字符数组进行构造
char[] array = {'h','e','l','l','o','b','i','t'};
String s3 = new String(array);
System.out.println(s1);1. String是引用类型,内部并不存储字符串本身,在String类的实现源码中,String类实例变量如下:

public static void main(String[] args) {
// s1和s2引用的是不同对象 s1和s3引用的是同一对象
String s1 = new String("hello");
String s2 = new String("world");
String s3 = s1;
System.out.println(s1.length()); // 获取字符串长度---输出5
System.out.println(s1.isEmpty()); // 如果字符串长度为0,返回true,否则返回false
}代码图解:

s1和s2引用的是不同对象 s1和s3引用的是同一对象
2. 在Java中“”引起来的也是String类型对象。
// 打印"hello"字符串(String对象)的长度
System.out.println("hello".length());字符串的比较是常见操作之一,比如:字符串排序。Java中总共提供了4种方式:
1. ==比较是否引用同一个对象
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = 10;
// 对于基本类型变量,==比较两个变量中存储的值是否相同
System.out.println(a == b); // false
System.out.println(a == c); // true
// 对于引用类型变量,==比较两个引用变量引用的是否为同一个对象
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = new String("world");
String s4 = s1;
System.out.println(s1 == s2); // false
System.out.println(s2 == s3); // false
System.out.println(s1 == s4); // true
}2. boolean equals(Object anObject) 方法:
按照字典序比较
字典序:字符大小的顺序
String类重写了父类Object中equals方法,Object中equals默认按照==比较,
String重写equals方法后,按照 如下规则进行比较,
比如: s1.equals(s2)
public boolean equals(Object anObject) {
// 1. 先检测this 和 anObject 是否为同一个对象比较,如果是返回true
if (this == anObject) {
return true;
}
// 2. 检测anObject是否为String类型的对象,如果是继续比较,否则返回false
if (anObject instanceof String) {
// 将anObject向下转型为String类型对象
String anotherString = (String)anObject;
int n = value.length;
// 3. this和anObject两个字符串的长度是否相同,是继续比较,否则返回false
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
// 4. 按照字典序,从前往后逐个字符进行比较
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}public static void main(String[] args) {
String s1 = new String("hello");
String s2 = new String("hello");
String s3 = new String("Hello");
// s1、s2、s3引用的是三个不同对象,因此==比较结果全部为false
System.out.println(s1 == s2); // false
System.out.println(s1 == s3); // false
// equals比较:String对象中的逐个字符
// 虽然s1与s2引用的不是同一个对象,但是两个对象中放置的内容相同,因此输出true
// s1与s3引用的不是同一个对象,而且两个对象中内容也不同,因此输出false
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // false
}equals比较:String对象中的逐个字符
3. int compareTo(String s) 方法:
按照字典序进行比较
与equals不同的是
equals返回的是boolean类型,而compareTo返回的是int类型。具体比较方式:
1. 先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值
2. 如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值
public static void main(String[] args) {
String s1 = new String("abc");
String s2 = new String("ac");
String s3 = new String("abc");
String s4 = new String("abcdef");
System.out.println(s1.compareTo(s2)); // 不同输出字符差值-1
System.out.println(s1.compareTo(s3)); // 相同输出 0
System.out.println(s1.compareTo(s4)); // 前k个字符完全相同,输出长度差值 -3
}4. int compareToIgnoreCase(String str) 方法:
与compareTo方式相同,但是忽略大小写比较
public static void main(String[] args) {
String s1 = new String("abc");
String s2 = new String("ac");
String s3 = new String("ABc");
String s4 = new String("abcdef");
System.out.println(s1.compareToIgnoreCase(s2)); // 不同输出字符差值-1
System.out.println(s1.compareToIgnoreCase(s3)); // 相同输出 0
System.out.println(s1.compareToIgnoreCase(s4)); // 前k个字符完全相同,输出长度差值 -3
}String类提供的常用查找的方法:

public static void main(String[] args) {
String s = "aaabbbcccaaabbbccc";
System.out.println(s.charAt(3)); // 'b'
System.out.println(s.indexOf('c')); // 6
System.out.println(s.indexOf('c', 10)); // 15
System.out.println(s.indexOf("bbb")); // 3
System.out.println(s.indexOf("bbb", 10)); // 12
System.out.println(s.lastIndexOf('c')); // 17
System.out.println(s.lastIndexOf('c', 10)); // 8
System.out.println(s.lastIndexOf("bbb")); // 12
System.out.println(s.lastIndexOf("bbb", 10)); // 3
}注意:上述方法都是实例方法。
数字转字符串:
String.valueOf(1234);
字符串转数字:
Integer.parseInt("1234");
public static void main(String[] args) {
// 数字转字符串
String s1 = String.valueOf(1234);
String s2 = String.valueOf(12.34);
String s3 = String.valueOf(true);
String s4 = String.valueOf(new Student("Hanmeimei", 18));
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println("=================================");
// 字符串转数字
// 注意:Integer、Double等是Java中的包装类型,这个后面会讲到
int data1 = Integer.parseInt("1234");
double data2 = Double.parseDouble("12.34");
System.out.println(data1);
System.out.println(data2);
}运行结果

s1.toUpperCase();
s2.toLowerCase();
public static void main(String[] args) {
String s1 = "hello";
String s2 = "HELLO";
System.out.println(s1.toUpperCase()); // 小写转大写
System.out.println(s2.toLowerCase()); // 大写转小写
}运行结果

s.toCharArray();
public static void main(String[] args) {
String s = "hello"; // 字符串转数组
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i]);
}
System.out.println(); // 数组转字符串
String s2 = new String(ch);
System.out.println(s2);
}
String.format();
public static void main(String[] args) {
String s = String.format("%d-%d-%d", 2019, 9,14);
System.out.println(s);
}2019-9-14
使用一个指定的新的字符串替换掉已有的字符串数据,可用的方法如下:
代码示例: 字符串的替换处理
String str = "helloworld" ;
运行结果

注意事项: 由于字符串是不可变对象, 替换不修改当前字符串, 而是产生一个新的字符串.
可以将一个完整的字符串按照指定的分隔符划分为若干个子字符串。

代码示例:
实现字符串的拆分处理
String str = "hello world hello bit" ;
String[] result = str.split(" ") ; // 按照空格拆分
for(String s: result) {
System.out.println(s);
}
代码示例:
字符串的部分拆分
String str = "hello world hello bit" ;
String[] result = str.split(" ",2) ;
for(String s: result) {
System.out.println(s);
}
拆分是特别常用的操作. 一定要重点掌握.
另外有些特殊字符作为分割符可能无法正确切分, 需要加上转义.
代码示例:
拆分IP地址
String str = "192.168.1.1" ;
String[] result = str.split("\\.") ;
for(String s: result) {
System.out.println(s);
}

代码示例:
多次拆分
String str = "name=zhangsan&age=18" ;
String[] result = str.split("&") ;
for (int i = 0; i < result.length; i++) {
String[] temp = result[i].split("=") ;
System.out.println(temp[0]+" = "+temp[1]);
}
从一个完整的字符串之中截取出部分内容。可用方法如下:

代码示例:
String str = "helloworld" ;
System.out.println(str.substring(5));
System.out.println(str.substring(0, 5));
注意事项:
1. 索引从0开始
2. 注意前闭后开区间的写法, substring(0, 5) 表示包含0号下标的字符, 不包含5号下标

代码示例:
观察trim()方法的使用
String str = " hello world " ;
System.out.println("["+str+"]");
System.out.println("["+str.trim()+"]");
trim 会去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等).
代码示例:
大小写转换
String str = " hello%$$%@#$%world 哈哈哈 " ;
System.out.println(str.toUpperCase());
System.out.println(str.toLowerCase());
这两个函数只转换字母。
String是一种不可变对象. 字符串中的内容是不可改变。
1. String类在设计时就是不可改变的,String类实现描述中已经说明了

被源码中final修饰

2. 所有涉及到可能修改字符串内容的操作都是创建一个新对象,改变的是新对象
比如replace方法:

【纠正】
网上有些人说:字符串不可变是因为其内部保存字符的数组被final修饰了,因此不能改变。这种说法是错误的,不是因为String类自身,或者其内部value被final修饰而不能被修改。
final修饰类表明该类不想被继承,
final修饰引用类型表明该引用变量不能引用其他对象,但是其引用对象中的内容是可以修改的。
public static void main(String[] args) {
final int array[] = {1, 2, 3, 4, 5};
array[0] = 100;
System.out.println(Arrays.toString(array));
}
我们可以看到是可以被修改的。

只是不能引用其他对象
注意:尽量避免直接对String类型对象进行修改,因为String类是不能修改的,
所有的修改都会创建新对象,效率非常低下。
public static void main(String[] args) {
String s = "hello";
s += " world";
System.out.println(s); // 输出:hello world
}但是这种方式不推荐使用,因为其效率非常低,中间创建了好多临时对象。

在对String类进行修改时,效率是非常慢的,因此:尽量避免对String的直接修改,
如果要修改建议尽量 使用StringBuffer或者StringBuilder。
由于String的不可更改特性,为了方便字符串的修改,Java中又提供StringBuilder和StringBuffer类。这两个类大 部分功能是相同的,这里介绍 StringBuilder常用的一些方法
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder("hello");
StringBuilder sb2 = sb1;
// 追加:即尾插-->字符、字符串、整形数字
sb1.append(' ');// hello
sb1.append("world"); // hello world
sb1.append(123); // hello world123
System.out.println(sb1); // hello world123
System.out.println(sb1 == sb2); // true
System.out.println(sb1.charAt(0)); // 获取0号位上的字符 h
System.out.println(sb1.length()); // 获取字符串的有效长度14
System.out.println(sb1.capacity()); // 获取底层数组的总大小
sb1.setCharAt(0, 'H'); // 设置任意位置的字符 Hello world123
sb1.insert(0, "Hello world!!!"); // Hello world!!!Hello world123
System.out.println(sb1);
System.out.println(sb1.indexOf("Hello")); // 获取Hello第一次出现的位置
System.out.println(sb1.lastIndexOf("hello")); // 获取hello最后一次出现的位置
sb1.deleteCharAt(0); // 删除首字符
sb1.delete(0, 5); // 删除[0, 5)范围内的字符
String str = sb1.substring(0, 5); // 截取[0, 5)区间中的字符以String的方式返回
System.out.println(str);
sb1.reverse(); // 字符串逆转
str = sb1.toString(); // 将StringBuffer以String的方式返回
System.out.println(str);
}String和StringBuilder最大的区别在于String的内容无法修改,而StringBuilder的内容可 以修改。频繁修改字符串的情况考虑使用StringBuilder。
注意:
String和StringBuilder类不能直接转换。
如果要想互相转换,可以采用如下原则:
1.String、StringBuff和StringBulider之间的区别
2. 以下总共创建了多少个String对象【前提不考虑常量池之前是否存在】
String str = new String("ab");String 对象存储在常量池中。String 对象在堆中。总共:2 个对象
String str = new String("a") + new String("b");这行代码的解析较为复杂,因为涉及到字符串连接操作。
String 对象存储在常量池中。String 对象存储在常量池中。String 对象在堆中。String 对象在堆中。String 对象在堆中,结果为 "ab"。总共:5 个对象

class Solution {
public String addStrings(String num1, String num2) {
int i = num1.length() - 1, j = num2.length() - 1, add = 0;
StringBuffer ans = new StringBuffer();
while (i >= 0 || j >= 0 || add != 0) {
int x = i >= 0 ? num1.charAt(i) - '0' : 0;
int y = j >= 0 ? num2.charAt(j) - '0' : 0;
int result = x + y + add;
ans.append(result % 10);
add = result / 10;
i--;
j--;
}
// 计算完以后的答案需要翻转过来
ans.reverse();
return ans.toString();
}
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/add-strings/solutions/357938/zi-fu-chuan-xiang-jia-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。StringBuffer 转换为字符串并返回。class Solution {
public int firstUniqChar(String s) {
Map<Character, Integer> frequency = new HashMap<Character, Integer>();
for (int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
frequency.put(ch, frequency.getOrDefault(ch, 0) + 1);
}
for (int i = 0; i < s.length(); ++i) {
if (frequency.get(s.charAt(i)) == 1) {
return i;
}
}
return -1;
}
}HashMap 变量 frequency 用于存储每个字符的出现次数。自己用String写的
class Solution {
public int firstUniqChar(String s) {
int[] count = new int[256];
// 统计每个字符出现的次数
for (int i = 0; i < s.length(); ++i) {
count[s.charAt(i)]++;
}
// 找第一个只出现一次的字符
for (int i = 0; i < s.length(); ++i) {
if (1 == count[s.charAt(i)]) {
return i;
}
}
return -1;
}
} public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 获取一行单词
String s = sc.nextLine();
// 1. 找到最后一个空格
// 2. 获取最后一个单词:从最后一个空格+1位置开始,一直截取到末尾
// 3. 打印最后一个单词长度
int len = s.substring(s.lastIndexOf(' ') + 1, s.length()).length();
System.out.println(len);
}class Solution {
public boolean isPalindrome(String s) {
StringBuffer sgood = new StringBuffer();
int length = s.length();
for (int i = 0; i < length; i++) {
char ch = s.charAt(i);
if (Character.isLetterOrDigit(ch)) {
sgood.append(Character.toLowerCase(ch));
}
}
StringBuffer sgood_rev = new StringBuffer(sgood).reverse();
return sgood.toString().equals(sgood_rev.toString());
}
} public static boolean isValidChar(char ch) {
if ((ch >= 'a' && ch <= 'z') ||
(ch >= '0' && ch <= '9')) {
return true;
}
return false;
}
public boolean isPalindrome(String s) {
s = s.toLowerCase();
int left = 0, right = s.length() - 1;
while (left < right) {
// 1. 从左侧找到一个有效的字符
while (left < right && !isValidChar(s.charAt(left))) {
left++;
}
// 2. 从右侧找一个有效的字符
while (left < right && !isValidChar(s.charAt(right))) {
right--;
}
if (s.charAt(left) != s.charAt(right)) {
return false;
} else {
left++;
right--;
}
}
return true;
}