String
类包括用于检查序列的各个字符的方法,用于比较字符串,搜索字符串,提取子字符串以及创建将所有字符翻译为大写或小写的字符串的副本。
内存案例
String 创建的字符串存储在公共池中,而 new 创建的字符串对象在堆上
public static void main(String[] args) {
String a1 = "XXXXX"; // String 直接创建
String a2 = "XXXXX"; // String 直接创建
String a3 = s1; // 相同引用
System.out.println(a1==a2);//true
String a4 = new String("XXXXX"); // String 对象创建
String a5 = new String("XXXXX"); // String 对象创建
System.out.println(a4==a5);//false
}
String 类是不可改变的
String 是不可改变的,一旦创建了 String 对象,那它的值就无法改变了
String被final关键字修饰,意味着String类不能被继承, 并且它的成员方法都默认为final方法;字符串一旦创建就不能再修改
连接字符串(concat和+)
public static void main(String[] args){
String a1 = "卢大宝".concat("XXX");
String a2 = "卢大宝" + "XXX";
System.out.println(a1);//卢大宝XXX
System.out.println(a2);//卢大宝XXX
}
+ 可以连接 null,null视为一个字符串连接,concat连接中出现null,会造成空指针错误
+号编译后就是使用了StringBuiler来拼接,所以使用+的语句就会创建一个StringBuilder,多条+语句在不同循环层中会创建多个StringBuilder对象,建议手动使用StringBuilder
格式化字符串
转 换 符 | 说 明 | 示 例 |
%s | 字符串类型 | "mingrisoft" |
%c | 字符类型 | 'm' |
%b | 布尔类型 | TRUE |
%d | 整数类型(十进制) | 99 |
%x | 整数类型(十六进制) | FF |
%o | 整数类型(八进制) | 77 |
%f | 浮点类型 | 99.99 |
%a | 十六进制浮点类型 | FF.35AE |
%e | 指数类型 | 9.38E+05 |
%g | 通用浮点类型(f和e类型中较短的) | 空 |
%h | 散列码 | 空 |
%% | 百分比类型 | % |
%n | 换行符 | 空 |
%tx | 日期与时间类型(x代表不同的日期与时间转换符 | 空 |
public static void main(String[] args){
String str=null;
str=String.format("Hi,%s", "王力");
System.out.println(str); //Hi,王力
str=String.format("Hi,%s:%s.%s", "王南","王力","王张");
System.out.println(str); //Hi,王南:王力.王张
System.out.printf("字母a的大写是:%c %n", 'A'); //字母a的大写是:A
System.out.printf("3>7的结果是:%b %n", 3>7); //3>7的结果是:false
System.out.printf("100的一半是:%d %n", 100/2); //100的一半是:50
System.out.printf("100的16进制数是:%x %n", 100); //100的16进制数是:64
System.out.printf("100的8进制数是:%o %n", 100); //100的8进制数是:144
System.out.printf("50元的书打8.5折扣是:%f 元%n", 50*0.85); //50元的书打8.5折扣是:42.500000 元
System.out.printf("上面价格的16进制数是:%a %n", 50*0.85); //上面价格的16进制数是:0x1.54p5
System.out.printf("上面价格的指数表示:%e %n", 50*0.85); //上面价格的指数表示:4.250000e+01
System.out.printf("上面价格的指数和浮点数结果的长度较短的是:%g %n", 50*0.85); //上面价格的指数和浮点数结果的长度较短的是:42.5000
System.out.printf("上面的折扣是%d%% %n", 85); //上面的折扣是85%
System.out.printf("字母A的散列码是:%h %n", 'A'); //字母A的散列码是:41
}
构造方法
public static void main(String[] args) throws UnsupportedEncodingException {
// String()
// 初始化新创建的 String对象,使其表示空字符序列。
String a1 = new String();
System.out.println(a1);//null
// String(byte[] bytes)
// 通过使用平台的默认字符集解码指定的字节数组来构造新的 String 。
byte[] chars= {'a','b'};
String a2 = new String(chars);//ab
// String(byte[] bytes, Charset charset)
// 构造一个新的String由指定用指定的字节的数组解码charset 。
byte[] charss= {'a','b'};
String a3 = new String(charss, "UTF-8");
// String(byte[] bytes, int offset, int length)
// 通过使用平台的默认字符集解码指定的字节子阵列来构造新的 String(offset要解码的第一个byte的索引,length要解码的byte数组的长度)
byte[] chars4= {'f','r', 'a','b', 'c', 'd'};
String a4 = new String(chars4, 3, 3);//bcd
// String(byte[] bytes, int offset, int length, Charset charset)
// 构造一个新的String通过使用指定的指定字节子阵列解码charset 。
byte[] chars5= {'a','b', 'c', 'd'};
Charset ch5 = Charset.forName("UTF-8");
String a5 = new String(chars5, 0, 3, ch5);//abc
// String(byte[] bytes, int offset, int length, String charsetName)
// 构造一个新的 String通过使用指定的字符集解码指定的字节子阵列。
byte[] chars6= {'a','b', 'c', 'd'};
String a6 = new String(chars6, 0, 3, "UTF-8");//abc
// String(byte[] bytes, String charsetName)
// 构造一个新的String由指定用指定的字节的数组解码charset 。
byte[] chars7 = {'a','b', 'c', 'd'};
String a7 = new String(chars7,"UTF-8");//abcd
// String(char[] value)
// 分配一个新的 String ,以便它表示当前包含在字符数组参数中的字符序列。
char[] bat1 = {'a','b', 'c', 'd'};
String a8 = new String(bat1);//abcd
// String(char[] value, int offset, int count)
// 分配一个新的 String ,其中包含字符数组参数的子阵列中的字符。(offset要解码的第一个byte的索引,count要解码的char的个数)
char[] bat9 = {'e', 'r','a','b', 'c', 'd'};
String a9 = new String(bat9,3,2);//bcd
// String(int[] codePoints, int offset, int count)
// 分配一个新的 String ,其中包含 Unicode code point数组参数的子阵列中的 字符 。
int[] int10 = {'a','b', 'c', 'd', 'e'};
String a10 = new String(int10,1,3);//bcd
// String(String original)
// 初始化新创建的String对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本
String a11 = new String("asd");//asd
// String(StringBuffer buffer)
// 分配一个新的字符串,其中包含当前包含在字符串缓冲区参数中的字符序列。
StringBuffer bf = new StringBuffer();
bf.append("asd");
String a12 = new String(bf);//asd
// String(StringBuilder builder)
// 分配一个新的字符串,其中包含当前包含在字符串构建器参数中的字符序列。
StringBuilder bu = new StringBuilder();
bu.append("asd");
String a13 = new String(bu);//asd
}
案例
public static void main(String[] args) {
// charAt(int index)
// 返回 char指定索引处的值
String a = "abcd";
char a1 = a.charAt(1);
System.out.println(a1);//b
// codePointAt(int index)
// 返回指定索引处的字符(Unicode代码点)
IntStream a2 = a.chars();
int [] a22 = a2.toArray();//{a,b,c,d}
// compareTo(String anotherString)
// 按字典顺序比较两个字符串
String a3 = "abcd";
String a33 = "abcd";
int a333 = a3.compareTo(a33);//0
// compareToIgnoreCase(String str)
// 按字典顺序比较两个字符串,忽略病例差异(忽略大小写)
String a4 = "abcd";
String a44 = "aBcd";
int a444 = a3.compareToIgnoreCase(a44);//0
// concat(String str)
// 将指定的字符串连接到该字符串的末尾。
String a5 = "asd";
System.out.println(a5.concat("!!!"));//asd!!!
// contains(CharSequence s)
// 当且仅当此字符串包含指定的char值序列时才返回true
String a6 = "asd";
System.out.println(a6.contains("s"));//true
// contentEquals(CharSequence cs)
// 将此字符串与指定的CharSequence进行 CharSequence
String a7 = "asd";
System.out.println(a7.contentEquals("asd"));//true
// contentEquals(StringBuffer sb)
// 将此字符串与指定的StringBuffer进行 StringBuffer
String a8 = "asd";
StringBuilder a888 = new StringBuilder();
a888.append("ssd");
System.out.println(a8.contentEquals(a888));//true
// endsWith(String suffix)
// 测试此字符串是否以指定的后缀结尾
String a9 = "asd";
System.out.println(a9.endsWith("d"));//true
// equals(Object anObject)
// 将此字符串与指定对象进行比较
String b1 = "asd";
System.out.println(b1.equals("asd"));//true
// equalsIgnoreCase(String anotherString)
// 将此 String与其他 String比较,忽略案例注意事项(忽略大小写)
String b2 = "asd";
System.out.println(b2.equalsIgnoreCase("asd"));//true
// format(Locale l, String format, Object... args)
// 使用指定的区域设置,格式字符串和参数返回格式化的字符串
System.out.println(String.format("字符串:%s整数",2));//字符串:2整数
// format(String format, Object... args)
// 使用指定的格式字符串和参数返回格式化的字符串
System.out.println(String.format(Locale.CHINA,"字符串:%s整数",2));//字符串:2整数
// getBytes()
// 使用平台的默认字符集将此 String编码为字节序列,将结果存储到新的字节数组中
String b3 = "asd";
byte [] b33 = b3.getBytes();//{a,s,d}
// hashCode()
// 返回此字符串的哈希码
String b4 = "卢大宝";
System.out.println(b4.hashCode());//21244472
// indexOf(int ch)
// 返回指定字符第一次出现的字符串内的索引
String b5 = "asdqweqws";
System.out.println(b5.indexOf("s"));//1
// indexOf(int ch, int fromIndex)
// 返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索
String b6 = "asdqweqws";
System.out.println(b6.indexOf("s",2));//8
// indexOf(String str)
// 返回指定子字符串第一次出现的字符串内的索引
String b7 = "wedqweqws";
System.out.println(b7.indexOf("we"));//0
// indexOf(String str, int fromIndex)
// 返回指定子串的第一次出现的字符串中的索引,从指定的索引开始
String b8 = "wedqweqws";
System.out.println(b8.indexOf("we",1));//4
// isEmpty()
// 返回 true如果,且仅当 length()为 0
String b9 = "";
System.out.println(b9.isEmpty());//true
// String join(CharSequence delimiter, CharSequence... elements)
// 返回一个新的字符串,由 CharSequence elements的副本组成,并附有指定的delimiter的 delimiter
StringBuilder c11 = new StringBuilder();
StringBuilder c111 = new StringBuilder();
c11.append("-");
c111.append("123");
String c1 = String.join(c11, c111,c111);//123-123
System.out.println(c1);
String[] arrStr=new String[]{"a","b","c"};
System.out.println(String.join("-", arrStr));
// lastIndexOf(int ch)
// 返回指定字符的最后一次出现的字符串中的索引
String c2 = "asdasd";
System.out.println(c2.lastIndexOf("sd"));//4
// lastIndexOf(int ch, int fromIndex)
// 返回指定字符的最后一次出现的字符串中的索引,从指定的索引开始向后搜索
String c3 = "asdasd";
System.out.println(c3.lastIndexOf("sd",4));//4
// length()
// 返回此字符串的长度
String c4 = "asd";
System.out.println(c4.length());//3
// replace(char oldChar, char newChar)
// 返回从替换所有出现的导致一个字符串 oldChar在此字符串 newChar 。
String c7 = "asdasd";
System.out.println(c7.replace("da","X"));//asXsd
// replaceAll(String regex, String replacement)
// 用给定的替换替换与给定的 regular expression匹配的此字符串的每个子字符串
String c8 = "asd5a4s3d";
System.out.println(c8.replaceAll("\\d","-"));//asd-a-s-d
// split(String regex)
// 将此字符串分割为给定的 regular expression的匹配
String c9 = "a,s,d5a4s,3d";
System.out.println(c9.split(","));//{a,s,d5a4s,3d}
// startsWith(String prefix)
// 测试此字符串是否以指定的前缀开头
String d1 = "asdqwe";
System.out.println(d1.startsWith("as"));//true
// startsWith(String prefix, int toffset)
// 测试在指定索引处开始的此字符串的子字符串是否以指定的前缀开头
String d2 = "asdqwe";
System.out.println(d2.startsWith("as",1));//fasle
// subSequence(int beginIndex, int endIndex)
// 返回一个字符序列,该序列是该序列的子序列
StringBuilder d33 = new StringBuilder();
d33.append("asdqwe");
System.out.println(d33.subSequence(1,2));//s(截取的字符串中不包括结束索引对应的字符)
// substring(int beginIndex)
// 返回一个字符串,该字符串是此字符串的子字符串
String d4 = "asdqwe";
System.out.println(d4.substring(1));//sdqwe
// substring(int beginIndex, int endIndex)
// 返回一个字符串,该字符串是此字符串的子字符串
String d5 = "asdqwe";
System.out.println(d5.substring(1,2));//s(截取的字符串中不包括结束索引对应的字符)
// toCharArray()
// 将此字符串转换为新的字符数组。
String d66 = "asd";
char [] d6 = d66.toCharArray();//{a,s,d}
// toLowerCase()
// 将所有在此字符 String使用默认语言环境的规则,以小写。
String d7 = "ASD";
System.out.println(d7.toLowerCase());//asd
// toString()
// 此对象(已经是字符串!)本身已被返回。
String d8 = "asd".toString();//asd
// toUpperCase()
// 将所有在此字符 String使用默认语言环境的规则大写
String d9 = "asd";
System.out.println(d9.toUpperCase());//ASD
// trim()
// 返回一个字符串,其值为此字符串,并删除任何前导和尾随空格
String e1 = " asd ";
System.out.println(e1.trim());//asd
// valueOf(int i)
// 返回 int参数的字符串 int形式
int e2 = 123;
System.out.println(Integer.valueOf(e2));//123
}
char | charAt(int index) 返回 |
int | codePointAt(int index) 返回指定索引处的字符(Unicode代码点) |
int | codePointBefore(int index) 返回指定索引之前的字符(Unicode代码点) |
int | codePointCount(int beginIndex, int endIndex) 返回此 |
int | compareTo(String anotherString) 按字典顺序比较两个字符串 |
int | compareToIgnoreCase(String str) 按字典顺序比较两个字符串,忽略病例差异 |
String | concat(String str) 将指定的字符串连接到该字符串的末尾。 |
boolean | contains(CharSequence s) 当且仅当此字符串包含指定的char值序列时才返回true |
boolean | contentEquals(CharSequence cs) 将此字符串与指定的CharSequence进行 |
boolean | contentEquals(StringBuffer sb) 将此字符串与指定的StringBuffer进行 |
static String | copyValueOf(char[] data) 相当于 |
static String | copyValueOf(char[] data, int offset, int count) |
boolean | endsWith(String suffix) 测试此字符串是否以指定的后缀结尾 |
boolean | equals(Object anObject) 将此字符串与指定对象进行比较 |
boolean | equalsIgnoreCase(String anotherString) 将此 |
static String | format(Locale l, String format, Object... args) 使用指定的区域设置,格式字符串和参数返回格式化的字符串 |
static String | format(String format, Object... args) 使用指定的格式字符串和参数返回格式化的字符串 |
byte[] | getBytes() 使用平台的默认字符集将此 |
byte[] | getBytes(Charset charset) 使用给定的charset将该 |
void | getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin) 已弃用 此方法无法将字符正确转换为字节。 从JDK 1.1开始,首选的方法是通过 |
byte[] | getBytes(String charsetName) 使用命名的字符集将此 |
void | getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 将此字符串中的字符复制到目标字符数组中 |
int | hashCode() 返回此字符串的哈希码 |
int | indexOf(int ch) 返回指定字符第一次出现的字符串内的索引 |
int | indexOf(int ch, int fromIndex) 返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索 |
int | indexOf(String str) 返回指定子字符串第一次出现的字符串内的索引 |
int | indexOf(String str, int fromIndex) 返回指定子串的第一次出现的字符串中的索引,从指定的索引开始 |
String | intern() 返回字符串对象的规范表示 |
boolean | isEmpty() 返回 |
static String | join(CharSequence delimiter, CharSequence... elements) 返回一个新的字符串,由 |
static String | join(CharSequence delimiter, Iterable<? extends CharSequence> elements) 返回一个新 |
int | lastIndexOf(int ch) 返回指定字符的最后一次出现的字符串中的索引 |
int | lastIndexOf(int ch, int fromIndex) 返回指定字符的最后一次出现的字符串中的索引,从指定的索引开始向后搜索 |
int | lastIndexOf(String str) 返回指定子字符串最后一次出现的字符串中的索引 |
int | lastIndexOf(String str, int fromIndex) 返回指定子字符串的最后一次出现的字符串中的索引,从指定索引开始向后搜索 |
int | length() 返回此字符串的长度 |
boolean | matches(String regex) 告诉这个字符串是否匹配给定的 regular expression |
int | offsetByCodePoints(int index, int codePointOffset) 返回此 |
boolean | regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) 测试两个字符串区域是否相等 |
boolean | regionMatches(int toffset, String other, int ooffset, int len) 测试两个字符串区域是否相等 |
String | replace(char oldChar, char newChar) 返回从替换所有出现的导致一个字符串 |
String | replace(CharSequence target, CharSequence replacement) 将与字面目标序列匹配的字符串的每个子字符串替换为指定的字面替换序列 |
String | replaceAll(String regex, String replacement) 用给定的替换替换与给定的 regular expression匹配的此字符串的每个子字符串 |
String | replaceFirst(String regex, String replacement) 用给定的替换替换与给定的 regular expression匹配的此字符串的第一个子字符串 |
String[] | split(String regex) 将此字符串分割为给定的 regular expression的匹配 |
String[] | split(String regex, int limit) 将这个字符串拆分为给定的 regular expression的匹配 |
boolean | startsWith(String prefix) 测试此字符串是否以指定的前缀开头 |
boolean | startsWith(String prefix, int toffset) 测试在指定索引处开始的此字符串的子字符串是否以指定的前缀开头 |
CharSequence | subSequence(int beginIndex, int endIndex) 返回一个字符序列,该序列是该序列的子序列 |
String | substring(int beginIndex) 返回一个字符串,该字符串是此字符串的子字符串 |
String | substring(int beginIndex, int endIndex) 返回一个字符串,该字符串是此字符串的子字符串 |
char[] | toCharArray() 将此字符串转换为新的字符数组。 |
String | toLowerCase() 将所有在此字符 |
String | toLowerCase(Locale locale) 将所有在此字符 |
String | toString() 此对象(已经是字符串!)本身已被返回。 |
String | toUpperCase() 将所有在此字符 |
String | toUpperCase(Locale locale) 将所有在此字符 |
String | trim() 返回一个字符串,其值为此字符串,并删除任何前导和尾随空格 |
static String | valueOf(boolean b) 返回 |
static String | valueOf(char c) 返回 |
static String | valueOf(char[] data) 返回 |
static String | valueOf(char[] data, int offset, int count) 返回 |
static String | valueOf(double d) 返回 |
static String | valueOf(float f) 返回 |
static String | valueOf(int i) 返回 |
static String | valueOf(long l) 返回 |
static String | valueOf(Object obj) 返回 |