String类
概述
String
类代表字符串。 String 是一种不可变字符串,一旦创建了一个对象并赋值后,再对其进行二次赋值则会指向一个新的对象,而原有的对象地址则不会改变。
- 例如
String s1 = "hello";
//赋的新值则会指向一个新的对象地址
s1 = "world";
API方法示例
charAt
@Test
public void charAt() {
//charAt: 根据索引值返回指定位置的字符
String Char = "charAt";
//0号索引值是c字符
char ch = Char.charAt(0);
System.out.println("ch = " + ch);
}
codePointAt
@Test
public void codePointAt() {
//codePointAt: 根据索引值返回某个字符的哈希值
String code = "codePointAt";
//指定0号索引值
System.out.println(code.codePointAt(0));
}
codePointBefore
@Test
public void codePointBefore() {
//codePointBefore: 获取指定索引值前一位索引值的哈希值
String code = "codePointBefore";
//此时1号元素为0号元素字符c
System.out.println(code.codePointBefore(1));
}
codePointCount
@Test
public void codePointCount() {
//codePointCount: 获取指定范围的字符长度
String code = "codePointCount";
//获取0-14之间的索引值
System.out.println(code.codePointCount(0, 14));
}
compareTo
@Test
public void compareTo() {
//compareTo: 按照字母的顺序比较字符串
String compare = "z";
//用Z比较A字母之间的范围
System.out.println(compare.compareTo("A"));
// A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
}
compareToIgnoreCase
@Test
public void compareToIgnoreCase() {
//compareToIgnoreCase: 按照字母的顺序比较字符串,忽略大小写
String compareToIgnoreCase = "Z";
System.out.println(compareToIgnoreCase.compareToIgnoreCase("A"));
}
concat
@Test
public void concat() {
//concat: 将指定字符串拼接到尾部
String concat = "Concat";
//输出结果为Concats
System.out.println(concat.concat("s"));
}
contains
@Test
public void contains() {
//contains: 判断是否包含指定的字符,包含为true,否则false
String contains = "contains";
System.out.println(contains.contains("con"));
}
contentEquals
@Test
public void contentEquals() {
//contentEquals(CharSequence cs): 比较字符串是否相等,相等为true,否则false
String contentEquals = "contentEquals";
System.out.println(contentEquals.contentEquals("contentEquals"));
}
ContentEquals
@Test
public void ContentEquals() {
//contentEquals(StringBuffer sb): 将此字符串与指定的 StringBuffer 比较。
String contentEquals = "contentEquals";
StringBuffer buffer = new StringBuffer().append("contentEquals");
System.out.println(contentEquals.contentEquals(buffer));
}
CopyValueOf
@Test
public void CopyValueOf() {
//copyValueOf(char[] data): 根据char类型数组获取字符值
String CopyValueOf = copyValueOf(new char[]{'1'});
System.out.println(CopyValueOf);
}
copyValueOf(char[] data, int offset, int count)
@Test
public void CopyValueof() {
//copyValueOf(char[] data, int offset, int count): 根据char型数组获取值,并指定索引区间
String CopyValueOf = copyValueOf(new char[]{'a'}, 0, 1);
//获取0-1索引区间的值,也就是a字符
System.out.println("CopyValueOf = " + CopyValueOf);
}
endsWith
@Test
public void endsWith() {
//endsWith: 判断字符串是否是指定的值结尾
String endsWith = "endsWith";
System.out.println(endsWith.endsWith("h"));
}
equals
@Test
public void equals() {
String equals1 = "equals1";
String equals2 = "equals2";
//比较equals1和equals2对象是否相等
System.out.println(equals1.equals(equals2));
}
equalsIgnoreCase
@Test
public void equalsIgnoreCase() {
//equalsIgnoreCase: 和其它字符串比较是否相等并忽略大小写,相等为true,否则false
String equalsIgnoreCase = "equalsIgnoreCase";
System.out.println(equalsIgnoreCase.equalsIgnoreCase("EQUALSIGNORECASE"));
}
format(Locale l, String format, Object… args)
@Test
public void format() {
//format(Locale l, String format, Object... args): 使用指定的区域设置,格式字符串和参数返回格式化的字符串
String format = String.format(Locale.CHINESE, "china", "good");
System.out.println("format = " + format);
System.out.println(Locale.CHINA);
}
format(String format, Object… args)
@Test
public void Format() {
//format(String format, Object... args): 使用指定的区域设置并格式化字符串
String format = String.format(Locale.US, "US");
System.out.println("format = " + format);
}
getBytes
@Test
public void getBytes() {
//getBytes: 使用默认字符集将字符串编码为字节序列,将结果存储到新的字节数组中。
String getBytes = "getBytes";
byte[] bytes = getBytes.getBytes();
System.out.println("bytes = " + bytes);
}
getBytes(Charset charset)
@Test
public void GetBytes() {
//getBytes(Charset charset): 使用给定的charset将该String编码为字节序列,将结果存储到新的字节数组中。
String getBytes = "getBytes method";
System.out.println(getBytes.getBytes(Charset.defaultCharset()));
}
indexOf
@Test
public void indexOf() {
//indexOf(int ch): 返回指定字符的索引值
String indexOf = "indexOf";
//返回字符O的索引值
System.out.println(indexOf.indexOf('O'));
}
indexOf(int ch, int fromIndex)
@Test
public void IndexOf() {
//indexOf(int ch, int fromIndex): 返回指定字符串的索引值,以指定的索引开始搜索。
String indexOf = "indexOf";
//获取字符O的索引值,从1号索引开始查询
System.out.println(indexOf.indexOf('O', 1));
}
indexOf(String str)
@Test
public void Indexof() {
//indexOf(String str): 根据指定字符串内容获取索引值
String indexOf = "indexOf";
//获取字符i的索引值
System.out.println(indexOf.indexOf("i"));
}
intern
@Test
public void intern() {
//intern:返回字符串对象的规范表示。
String intern = "null";
System.out.println(intern.intern());
}
isEmpty
@Test
public void isEmpty() {
//isEmpty: 判断是否为空,空则返回true,否则false
String isEmpty = "";
System.out.println(isEmpty.isEmpty());
}
join
@Test
public void join() {
//join(CharSequence delimiter, CharSequence... elements): 添加新字符串,由delimiter分隔符和element字符串内容组成
String join = String.join("-", "join", "method");
System.out.println(join);
}
lastIndexOf(int ch)
@Test
public void lastIndexOf() {
//lastIndexOf(int ch): 返回指定字符的最后一次出现的位置的索引值
String lastIndexOf = "lastIndexOf";
System.out.println(lastIndexOf.lastIndexOf('f'));
}
lastIndexOf(int ch, int fromIndex)
@Test
public void LastIndexOf() {
//lastIndexOf(int ch, int fromIndex):返回指定字符的最后一次出现的位置的索引值,从指定索引开始查询
String lastIndexOf = "lastIndexOf";
//查询O字符,从0号索引开始
System.out.println(lastIndexOf.lastIndexOf('l', 0));
}
lastIndexOf(String str)
@Test
public void LastIndexOF() {
//lastIndexOf(String str): 获取指定字符串最后出现的位置的索引值
String lastIndexOf = "lastIndexOf";
System.out.println(lastIndexOf.lastIndexOf("a"));
}
lastIndexOf(String str)
@Test
public void LastIndexOF() {
//lastIndexOf(String str): 获取指定字符串最后出现的位置的索引值
String lastIndexOf = "lastIndexOf";
System.out.println(lastIndexOf.lastIndexOf("a"));
}
LastIndexoF
@Test
public void LastIndexoF() {
//lastIndexOf(String str, int fromIndex): 获取指定字符串最后出现的位置的索引值,从指定索引开始查询
String lastIndexOf = "lastIndexOf";
System.out.println(lastIndexOf.lastIndexOf("s", 1));
}
length
@Test
public void length() {
//length: 返回字符串的长度
String length = "length";
System.out.println(length.length());
}
matches
@Test
public void matches() {
//matches: 比较字符串是否相等,相等返回true否则false
String matches = "matches";
System.out.println(matches.matches("matches"));
}
offsetByCodePoints
@Test
public void offsetByCodePoints() {
//offsetByCodePoints: 获取指定索引区间的值
String offsetByCodePoints = "offsetByCodePoints";
//获取0-2索引区间的值为2个
System.out.println(offsetByCodePoints.offsetByCodePoints(0, 2));
}
regionMatches
@Test
public void regionMatches() {
//regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len): 利用其它字符串比较区域之间是否相等
String regionMatches = "regionMatches";
//参数1为是否忽略大小写,参数二是被比较字符串的索引值开始,参数三是其它比较字符串,参数四五表示索引区间
System.out.println(regionMatches.regionMatches(false, 0, "r", 0, 1));
}
replace(char oldChar, char newChar)
@Test
public void replace() {
//replace(char oldChar, char newChar): 使用新字符替换旧的字符
String replace = "replace";
System.out.println(replace.replace('e', 's'));
}
replace(CharSequence target, CharSequence replacement)
@Test
public void Replace() {
//replace(CharSequence target, CharSequence replacement): 将旧字符串替换为新字符串
String replace = "replace";
System.out.println(replace.replace("replace", "replacies"));
}
replaceAll
@Test
public void replaceAll() {
//replaceAll: 替换每一个匹配的字符串
String replaceAll = "replaceAll";
System.out.println(replaceAll.replaceAll("replaceAll", "replace"));
}
replaceFirst
@Test
public void replaceFirst() {
//replaceFirst(String regex, String replacement): 用给定的值替换第一个字符串
String replaceFirst = "replaceFirst";
System.out.println(replaceFirst.replaceFirst("r", "R"));
}
split(String regex)
@Test
public void split() {
//split(String regex): 切分指定的字符串
String split = "split";
//切分0-1号元素的sp字符
System.out.println(Arrays.toString(split.split("sp")));
}
split(String regex, int limit)
@Test
public void Split() {
//split(String regex, int limit): 根据指定字符串和索引值切分
String split = "split";
//从s字符切分并指定s字符的0号索引
System.out.println(Arrays.toString(split.split("s", 0)));
}
startsWith
@Test
public void startsWith() {
//startsWith: 判断是否以指定字符开头,是则为true,否则false
String startsWith = "startsWith";
System.out.println(startsWith.startsWith("s"));
}
startsWith(String prefix, int toffset)
@Test
public void StartsWith() {
//startsWith(String prefix, int toffset): 判断指定索引处是否以指定字符串开头,是为true,否则false
String startsWith = "startsWith";
//判断0号元素是否以s字符开头
System.out.println(startsWith.startsWith("s", 0));
}
subSequence
@Test
public void subSequence() {
//subSequence: 返回指定索引区间的值
String subSequence = "subSequence";
//获取0-2索引值区间的字符
System.out.println(subSequence.subSequence(0, 2));
}
substring(int beginIndex)
@Test
public void substring() {
//substring(int beginIndex): 根据指定索引值开始获取字符串后面的数据
String substring = "substring";
//获取从1号元素开始后面的字符
System.out.println(substring.substring(1));
}
Substring
@Test
public void Substring() {
//substring(int beginIndex, int endIndex): 截取指定区间的值
String substring = "substring";
System.out.println(substring.substring(0, 2));
}
toCharArray
@Test
public void toCharArray() {
//toCharArray: 将字符串转换为字符数组
String toCharArray = "toCharArray";
char[] chars = toCharArray.toCharArray();
System.out.println(chars);
}
toLowerCase
@Test
public void toLowerCase() {
//toLowerCase: 将字符串以小写形式显示
String toLowerCase = "toLowerCase";
System.out.println(toLowerCase.toLowerCase());
}
toLowerCase(Locale locale)
@Test
public void ToLowerCase() {
//toLowerCase(Locale locale): 以小写形式显示,附带Locale参数
String toLowerCase = "toLowerCase";
System.out.println(toLowerCase.toLowerCase(Locale.US));
}
toString
@Test
public void ToString() {
//toString: 将对象转换为字符串形式,本身String就是字符串
String toString = "toString method";
System.out.println(toString.toString());
}
toUpperCase
@Test
public void toUpperCase() {
//toUpperCase: 以大写形式显示
String toUpperCase = "toUpperCase";
System.out.println(toUpperCase.toUpperCase());
}
trim
@Test
public void trim() {
//trim: 输出字符串内容并修剪多余空格
String trim = " trim ";
System.out.println(trim);
System.out.println(trim.trim());
}
valueOf
@Test
public void valueOf() {
//valueOf(boolean b): 返回值以boolean类型
System.out.println(String.valueOf(1));
//valueOf(char c): 返回值以char类型
System.out.println(String.valueOf('c'));
//valueOf(char[] data): 返回值以char型数组
System.out.println(String.valueOf(new char[]{'1','2','3'}));
//valueOf(char[] data, int offset, int count): 返回值以char型数组,根据索引值开始并指定返回值的个数
System.out.println(String.valueOf(new char[]{'c','h','a','r'},0,3));
//valueOf(double d): 返回值以double形式
System.out.println(String.valueOf(1.2));
//valueOf(float f): 返回值以float形式
System.out.println(String.valueOf(1.3F));
//valueOf(int i): 返回值以int类型
System.out.println(String.valueOf(123));
//valueOf(long l): 返回值以long类型
System.out.println(String.valueOf(233L));
//valueOf(Object obj): 返回值以object类型
System.out.println(Arrays.toString(new Object[]{1, 2F, 3L, 1.2, 1.3F, 'c', "obj", true}));
}