字符串
定义
字符串:由若干个字符构成的字符序列,每一个字符位置是固定的
在java中,提供了一个类来表示字符串:String
Java程序中的所有字符串文字(例如"abc" )都被实现为此类的实例【对象】。
因为String对象是不可变的,它们可以被共享。
思考
思考:
1、同一个内容的字符串,为什么地址值也是一样的? 因为都指向常量池中的同一个字符串地址值。
2、String对象是不可变的,我们却可以修改字符串变量的值? 字符串不可变指的是常量池中的字符串本身不能改变。
3、为什么直接打印字符串对象名,获取的是字符串内容值,而不是地址值?String类中重写了toString()方法
例子
public class StringDemo1 {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
s1 = "hello";
System.out.println(s1);
System.out.println(s2);
}
}
String
String介绍
1.概述:String 类代表字符串
2.特点:
a.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例(对象)实现
凡是带双引号的,都是String的对象
String s = "abc"
"abc"就是对象;String就是对象的数据类型;s就是对象名
b.字符串是常量,它们的值在创建之后不能更改
String s = "hello"
s+="world" -> 会产生新对象
c.String 对象是不可变的,所以可以共享
String s1 = "abc"
String s2 = "abc"
String类中的构造方法:
String类中的构造方法:
public String()
public String(byte[] bytes)
public String(byte[] bytes,int offset,int length)
public String(char[] value)
public String(char[] value,int offset,int count)
public String(String original)
例子
public class StringDemo2 {
public static void main(String[] args) {
String s1 = new String();
System.out.println("s1: "+s1);
System.out.println("-----------------------");
byte[] bytes = {97,65,48,98,99};
String s2 = new String(bytes);
System.out.println("s2: "+s2);
System.out.println("-----------------------");
String s3 = new String(bytes, 1, 3);
System.out.println("s3: "+s3);
System.out.println("-----------------------");
char[] chars = {'我','爱','刘','亦','菲'};
String s4 = new String(chars);
System.out.println("s4: "+s4);
System.out.println("-----------------------");
String s5 = new String(chars,2,3);
System.out.println("s5: "+s5);
System.out.println("-----------------------");
String s6 = new String("hello");
System.out.println("s6: "+s6);
System.out.println("-----------------------");
String s = new String("hello");
String s7 = "hello";
System.out.println(s==s7);
}
}
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)-> 截取字符串,从beginIndex开始到endIndex结束
含头不含尾,返回新串儿
例子:
public class StringDemo4 {
public static void main(String[] args) {
String s1 = "hello";
System.out.println(s1.length());
System.out.println(s1.charAt(1));
System.out.println(s1.indexOf(101));
String bigStr = "dwqsredhelloqjion";
String smallStr = "world";
System.out.println(bigStr.indexOf(smallStr));
String s2= "dwqrewqrffqewrfdwqqrfwqfwq";
System.out.println(s2.indexOf(101,5));
String s3 = "数加科技欢迎大家的到来";
String s4 = s3.substring(4);
System.out.println("s3: "+s3);
System.out.println("s4: "+s4);
String s5 = s3.substring(4, 8);
System.out.println("s5: "+s5);
}
}
练习
遍历字符串
public class Demo05String {
public static void main(String[] args) {
String s = "abcdefg";
for (int i = 0; i < s.length(); i++) {
System.out.println(s.charAt(i));
}
}
}
String类中的转换功能:
byte[] getBytes()-> 将字符串转成byte数组
char[] toCharArray()-> 将字符串转成char数组
static String valueOf(char[] chs)
static String valueOf(int i)
String toLowerCase()
String toUpperCase()
String concat(String str)
例子
public class StringDemo5 {
public static void main(String[] args) {
String s1 = "数加";
byte[] bytes = s1.getBytes();
printIntArray(bytes);
char[] chars = s1.toCharArray();
printIntArray(chars);
String s2 = String.valueOf(chars);
System.out.println(s2);
String s3 = String.valueOf(10);
System.out.println(s3);
String s4 = "HellO";
String s5 = s4.toLowerCase();
System.out.println(s5);
String s6 = s5.toUpperCase();
System.out.println(s6);
String s7 = "shujia";
String s8 = "keji";
System.out.println(s7+s8);
System.out.println(s7.concat(s8));
}
public static void printIntArray(char[] array) {
for (int i = 0; i < array.length; i++) {
if (i == 0) {
System.out.print("[" + array[i] + ",");
} else if (i == array.length - 1) {
System.out.println(array[i] + "]");
} else {
System.out.print(array[i] + ",");
}
}
}
public static void printIntArray(byte[] array) {
for (int i = 0; i < array.length; i++) {
if (i == 0) {
System.out.print("[" + array[i] + ",");
} else if (i == array.length - 1) {
System.out.println(array[i] + "]");
} else {
System.out.print(array[i] + ",");
}
}
}
}
String类中的替换功能
String replace(char old,char new)
String replace(String old,String new)
去除字符串两空格
String trim()
按字典顺序比较两个字符串【重要!!】
int compareTo(String str) 按照字符串中的字符从左向右依次比较=,如果长度和内容都一样,返回0
int compareToIgnoreCase(String str)
例子
public class StringDemo6 {
public static void main(String[] args) {
String s1 = "今天是20205年1月4日,hello今年是蛇年hellodsaqwwdq";
String s2 = s1.replace('h', '_');
System.out.println("s1: "+s1);
System.out.println("s2: "+s2);
System.out.println("--------------------");
String s3 = s1.replace("hello", "flinkspark");
System.out.println("s1: "+s1);
System.out.println("s3: "+s3);
System.out.println("--------------------");
String s4 = " hello world ";
System.out.println(s4.trim());
System.out.println("--------------------");
String s5 = "hello";
String s6 = "world";
String s7 = "hel";
System.out.println(s5.compareTo(s6));
System.out.println(s5.compareTo(s7));
}
}
String练习
把数组中的数据按照指定个格式拼接成一个字符串
举例:int[] arr = {1,2,3}; 输出结果:[1, 2, 3]
public class StringTest3 {
public static void main(String[] args) {
int[] arr = {1,354,56,2,125,12};
System.out.println(arrToStr(arr));
}
public static String arrToStr(int[] array) {
if(array==null){
return null;
}
String res = "[";
for (int i = 0; i < array.length; i++) {
if (i == array.length - 1) {
res = res + array[i] + "]";
break;
}
res = res + array[i] + ",";
}
return res;
}
}
字符串反转
举例:键盘录入”abc” 输出结果:”cba”
方式1:将字符串转字符数组,数组逆序,再转成字符串
方式2:将字符串转字符数组,倒着遍历,拼接成一个字符串
方式3:先将字符串转StringBuffer,调用StringBuffer中的反转方法,再转成字符串
public class StringTest4 {
public static void main(String[] args) {
String s1 = "我爱莫提冯";
System.out.println(fun3(s1));
}
public static String fun3(String s){
StringBuffer sb = new StringBuffer(s);
sb.reverse();
return sb.toString();
}
public static String fun2(String s) {
char[] chars = s.toCharArray();
String res = "";
for (int i = chars.length - 1; i >= 0; i--) {
res+=chars[i];
}
return res;
}
public static String fun1(String s) {
char[] chars = s.toCharArray();
nuXu(chars);
return new String(chars);
}
public static void nuXu(char[] chars) {
for (int front = 0, end = chars.length - 1; front < end; front++, end--) {
char tmp = chars[front];
chars[front] = chars[end];
chars[end] = tmp;
}
}
}
StringBuilder类
StringBuffer:
线程安全,可变的字符序列。 字符串缓冲区就像一个String ,但可以修改。
在任何时间点,它包含一些特定的字符序列,但可以通过某些方法调用来更改序列的长度和内容。
作用:主要是字符串拼接
StringBuilder的特点:
a.底层自带缓冲区,此缓冲区是没有被final修饰的byte数组,默认长度为16
b.如果超出了数组长度,数组会自动扩容
创建一个新长度的新数组,将老数组的元素复制到新数组中,然后将新数组的地址值重新赋值给老数组
c.默认每次扩容老数组的2倍+2
如果一次性添加的数据超出了默认的扩容数组长度(2倍+2),比如存了36个字符,超出了第一次扩容的34,就按照实际数据个数为准,就是以36扩容
构造方法:
public StringBuffer()
public StringBuffer(int capacity)
public StringBuffer(String str)
public class StringBufferDemo1 {
public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer();
System.out.println("sb1: " + sb1);
System.out.println(sb1.capacity());
System.out.println(sb1.length());
System.out.println("----------------");
StringBuffer sb2 = new StringBuffer(100);
System.out.println(sb2.capacity());
System.out.println(sb2.length());
System.out.println("----------------");
StringBuffer sb3 = new StringBuffer("hello");
System.out.println(sb3.capacity());
System.out.println(sb3.length());
System.out.println("----------------");
}
}
StringBuilder的使用功能
添加功能
public StringBuffer append(String str)
public StringBuffer insert(int offset,String str)
删除功能
public StringBuffer deleteCharAt(int index)
public StringBuffer delete(int start,int end)
替换功能
public StringBuffer replace(int start,int end,String str)
反转功能
public StringBuffer reverse()
截取功能
public String substring(int start)
public String substring(int start,int end)
例子:
public class StringBufferDemo2 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append(10);
sb.append(12.34);
sb.append(true);
sb.append("hello");
sb.append('w');
System.out.println("sb: "+sb);
sb.insert(5,"java");
System.out.println("sb: "+sb);
sb.deleteCharAt(7);
System.out.println("sb: "+sb);
sb.delete(7,11);
System.out.println("sb: "+sb);
sb.replace(7,10,"flink");
System.out.println("sb: "+sb);
StringBuffer sb2 = new StringBuffer("hello");
sb2.reverse();
System.out.println("sb2: "+sb2);
StringBuffer sb3 = new StringBuffer("世界很大,我想去看看!");
String s1 = sb3.substring(5);
System.out.println("sb3: "+sb3);
System.out.println("s1: "+s1);
String s2 = sb3.substring(5,8);
System.out.println("sb3: "+sb3);
System.out.println("s1: "+s2);
}
}
StringBuffer作为参数传递
public class StringBufferDemo4 {
public static void main(String[] args) {
StringBuffer sb1 = new StringBuffer("hello");
StringBuffer sb2 = new StringBuffer("world");
System.out.println("sb1: "+sb1+", sb2: "+sb2);
change(sb1,sb2);
System.out.println("sb1: "+sb1+", sb2: "+sb2);
}
public static void change(StringBuffer sb1,StringBuffer sb2){
System.out.println("sb1: "+sb1+", sb2: "+sb2);
sb1 = sb2;
sb2.append(sb1);
System.out.println("sb1: "+sb1+", sb2: "+sb2);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!