一.内存分配
这样的话,如果我们改了 a 的值,系统会重新给 a 分配一个内存,这样 a 和 b,c指的就不是一个地方
如果使用new
分配内存
这样他们指的就不是同一个地方了
代码String test1 = you + hi;
相当于new String("你好")
,这样两个地址就不同了
二.常用方法
1. int length()
获取长度
2.boolean startsWith(String s), boolean endsWith(String s)
判断是否是以s开头或者结尾的
3.int compareTo(String s),int compareToIgnoreCase(String s)
compareTo
是根据字典序判断this
和s
的关系
如果this > s
,返回正值
如果this < s
,返回负值
如果this = s
,返回0值
compareToIgnoreCase
是忽略大小写比值
4.boolean equalsIgnoreCase(String s)
忽略大小写看是否相等
5.boolean contains(String s)
判断对象是否包含s
如tom = ”student
,tom.contains("stu")
是true
6.String substring(int startpoint)
①只有一个参数时,截取从这个位置到最后一位的字符串
String hello = "你好java";
hello = hello.substring(2); // 返回 java
②有两个参数时,截取的是从 start 到 end-1
String hello = "你好java,c++";
hello = hello.substring(2,6); // 返回 java
7.String trim()
去掉开头和结尾的空格,但是没有办法去掉中间的空格
String hello = " 你好jav a, c++ ";
hello = hello.trim();
8.char charAt(index)
返回第 index 的字符
9.void getChars(int start, int end, char c[], int offset)
将string
中从start
到end
的字符串,变成字符数组,存放在c
中,并从c
的offset
位置开始存放这些字符
三.正则表达式
正则表达式都要加 [ ], 如 [ \\D ]
public class test
{
public static void main(String[] args)
{
String rex = "[a-z[A-Z]]+";
String s = "dsadsd";
if(s.matches(rex))
System.out.println("匹配");
else
System.out.println("不匹配");
}
}
public class test
{
public static void main(String[] args)
{
String rex = "123{1}";
String s = "123123";
if(s.matches(rex))
System.out.println("匹配");
else
System.out.println("不匹配");
}
}
public class test
{
public static void main(String[] args)
{
String rex = "hello[2468]?"; // 表示 hello2,hello4,hello6,hello8
String s = "hello"; // 匹配的是 0 次
if(s.matches(rex))
System.out.println("匹配");
else
System.out.println("不匹配");
}
}
四.字符串的替换
这个替换替换的是序列
使用string
类的replace(old,new)
函数
public class test
{
public static void main(String[] args)
{
String str = "123 hello 456 fuck 789";
String s = str.replace("[a-zA-Z]+", "你好");
System.out.println(s);
}
}
这个replace
只能替换第一个序列
替换所有可以用replaceAll
import com.sun.jdi.event.StepEvent;
public class test
{
public static void main(String[] args)
{
String str = "12123 hello 456465 fuck 54"; // 表示 hello2,hello4,hello6,hello8
String s = str.replaceAll("[a-zA-Z]+", "你好");
System.out.println(s);
}
}
五.分割字符
使用string
类的split(string regex)
方法
用regex
来分割这个字符串
也就是将regex
从字符串中截取掉,剩下的分配到字符数组中
public class test
{
public static void main(String[] args)
{
String str = "1949年10月1日是中华人民共和国成立的日子";
String reg = "[\\D]+";
String digWord[] = str.split(reg);
for(int i = 0; i < digWord.length; i ++)
System.out.println(digWord[i]);
}
}
注:如果string
的前缀和regex
匹配,第一个会是“”
import com.sun.jdi.event.StepEvent;
public class test
{
public static void main(String[] args)
{
String str = "公元1949年10月1日是中华人民共和国成立的日子";
String reg = "[\\D]+";
String digWord[] = str.split(reg);
for(int i = 0; i < digWord.length; i ++)
System.out.println(digWord[i]);
}
}
这个可以理解为 “公元” 将 “” 和1949分割开
注:
如果reg = "#/"
,则是按照"#/"
分割
public class test
{
public static void main(String[] args)
{
String shoppingReceipt = "you***#//###are*#*#/welcom";
String reg = "#/";
String str[] = shoppingReceipt.split(reg);
for(int i = 0;i < str.length;i++)
System.out.println(str[i]);
}
}
但如果reg = "[#/]+"
,则按照#/
的任意排序来分割
public class test
{
public static void main(String[] args)
{
String shoppingReceipt = "you#//##//#are##/welcom";
String reg = "[#/]+";
String str[] = shoppingReceipt.split(reg);
for(int i = 0;i < str.length;i++)
System.out.println(str[i]);
}
}