String
String API
Modifier and Type | Method and Description |
---|---|
char | charAt(int index) 返回 char指定索引处的值。 |
int | codePointAt(int index) 返回指定索引处的字符(Unicode代码点)。 |
int | codePointBefore(int index) 返回指定索引之前的字符(Unicode代码点)。 |
int | codePointCount(int beginIndex, int endIndex) 返回此 String指定文本范围内的Unicode代码点数。 |
int | compareTo(String anotherString) 按字典顺序比较两个字符串。 |
int | compareToIgnoreCase(String str) 按字典顺序比较两个字符串,忽略病例差异。 |
String | concat(String str) 将指定的字符串连接到该字符串的末尾。 |
boolean | contains(CharSequence s) 当且仅当此字符串包含指定的char值序列时才返回true。 |
boolean | contentEquals(CharSequence cs) 将此字符串与指定的CharSequence进行 CharSequence 。 |
boolean | contentEquals(StringBuffer sb) 将此字符串与指定的StringBuffer进行 StringBuffer 。 |
static String | copyValueOf(char[] data) 相当于 valueOf(char[])。 |
static String | copyValueOf(char[] data, int offset, int count) 相当于 valueOf(char[], int, int) 。 |
boolean | endsWith(String suffix) 测试此字符串是否以指定的后缀结尾。 |
boolean | equals(Object anObject) 将此字符串与指定对象进行比较。 |
boolean | equalsIgnoreCase(String anotherString) 将此 String与其他 String比较,忽略案例注意事项。 |
static String | format(Locale l, String format, Object… args) 使用指定的区域设置,格式字符串和参数返回格式化的字符串。 |
static String | format(String format, Object… args) 使用指定的格式字符串和参数返回格式化的字符串。 |
byte[] | getBytes() 使用平台的默认字符集将此 String编码为字节序列,将结果存储到新的字节数组中。 |
byte[] | getBytes(String charsetName) 使用命名的字符集将此 String编码为字节序列,将结果存储到新的字节数组中。 |
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() 返回 true如果,且仅当 length()为 0 。 |
static String | join(CharSequence delimiter, CharSequence… elements) 返回一个新的字符串,由 CharSequence elements的副本组成,并附有指定的delimiter的 delimiter 。 |
static String | join(CharSequence delimiter, Iterable<? extends CharSequence> elements) 返回一个新 String的副本组成 CharSequence elements与指定的副本一起加入 delimiter 。 |
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) 返回此 String内的指数,与 index 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) 返回从替换所有出现的导致一个字符串 oldChar在此字符串 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使用默认语言环境的规则,以小写。 |
String | toLowerCase(Locale locale) 将所有在此字符 String ,以降低使用给定的规则情况下 Locale 。 |
String | toString() 此对象(已经是字符串!)本身已被返回。 |
String | toUpperCase() 将所有在此字符 String使用默认语言环境的规则大写。 |
String | toUpperCase(Locale locale) 将所有在此字符 String使用给定的规则,大写 Locale 。 |
String | trim() 返回一个字符串,其值为此字符串,并删除任何前导和尾随空格。 |
static | String valueOf(boolean b) 返回 boolean参数的字符串 boolean形式。 |
static | String valueOf(char c) 返回 char参数的字符串 char形式。 |
static | String valueOf(char[] data) 返回 char数组参数的字符串 char形式。 |
static | String valueOf(char[] data, int offset, int count) 返回 char数组参数的特定子阵列的字符串 char形式。 |
static | String valueOf(double d) 返回 double参数的字符串 double形式。 |
static | String valueOf(float f) 返回 float参数的字符串 float形式。 |
static | String valueOf(int i) 返回 int参数的字符串 int形式。 |
static | String valueOf(long l) 返回 long参数的字符串 long形式。 |
static | String valueOf(Object obj) 返回 Object参数的字符串 Object形式。 |
Sample Code
package com.sijing.reflect_demo;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class StringDemo {
public static void main(String[] args) {
/**
* 获取指定索引处的字符
*/
char chars = "String sample code".charAt(0);
// S
System.out.println(chars);
/**
* 获取指定索引处字符对应的Unicode码
*/
int strUnicode = "String sample code".codePointAt(0);
// 83
System.out.println(strUnicode);
/**
* 获取指定索引处前一位字符对应的Unicode码
*/
// 83
System.out.println("String sample code".codePointBefore(1));
// 5
System.out.println("String sample code".codePointCount(0, 5));
/**
* 字符串拼接
*/
// str add str
System.out.println("str".concat(" add str"));
}
/**
* 按字典顺序比较两个字符串
* @Function: compareTo
* @Description:
*
*/
public static void compareTo() {
/**
* 按照字典顺序比较两个字符串
*/
System.out.println("s".compareTo("s"));
/**
* 按照字典顺序比较两个字符串,忽略大小写
*/
"S".compareToIgnoreCase("s");
}
/**
* 是否包含指定字符串
* @Function: contains
* @Description:
*
*/
public static void contains() {
// true
System.out.println("str".contains("st"));
// false
System.out.println("str".contains("a"));
CharSequence cs = "cs";
// true
System.out.println("cs".contentEquals(cs));
StringBuffer sb = new StringBuffer("sb");
// true
System.out.println("sb".contentEquals(sb));
}
/**
* 搜索字符串出现的位置
* @Function: indexOf
* @Description:
*
*/
public static void indexOfOrLastIndexOf() {
String str = "ABCDEF";
// char code
str.indexOf(84);
// 从指定索引位置开始
str.indexOf(84, 2);
str.indexOf("S");
str.indexOf("S", 2);
str.lastIndexOf(84);
str.lastIndexOf(84, 2);
str.lastIndexOf("S");
str.lastIndexOf("S", 2);
}
/**
* 是否以指定字符串开始或结尾
* @Function: startWithOrEndWith
* @Description:
*
*/
public static void startWithOrEndsWith() {
System.out.println("str".startsWith("r"));
// 从指定索引处开始
System.out.println("str".startsWith("t",1));
/**
* 是否以指定字符结尾
*/
// true
System.out.println("str".endsWith("r"));
}
/**
* 比较两个字符串的内容
* @Function: equals
* @Description:
*
*/
public static void equals() {
// false
"S".equals("s");
/**
* 比较内容,忽略大小写
*/
// true
"S".equalsIgnoreCase("s");
}
/**
* 全部字符转换为大写或小写
* @Function: toLowerCaseOrtoUpperCase
* @Description:
*
*/
public static void toLowerCaseOrtoUpperCase() {
// using the default system Locale
Locale defloc = Locale.getDefault();
/*
* 转为小写
*/
"LowEr".toLowerCase();
"LowEr".toLowerCase(defloc);
/*
* 转为大写
*/
"UppEr".toUpperCase();
"UppEr".toUpperCase(defloc);
}
/**
* 格式化字符串: 通过占位符来占位,组成新的字符串
* @Function: format
* @Description:
*
*/
public static void format() {
// 新字符串使用本地语言环境
String.format("Hi,%s", "世界");
// 使用指定的语言环境
Locale defloc = Locale.getDefault();
String.format(defloc,"Hi,%s", "世界");
}
/**
* 替换字符串内容
* @Function: replace
* @Description:
*
*/
public static void replace() {
System.out.println("strstr".replace("s", "a"));
System.out.println("strstr".replaceAll("s", "r"));
System.out.println("strstr".replaceFirst("s", "e"));
}
/**
* 截取一段字符串
* @Function: substring
* @Description:
*
*/
public static void substring() {
System.out.println("abcdefg".subSequence(2, 5));
System.out.println("abcdefg".substring(2));
System.out.println("abcdefg".substring(2, 5));
}
/**
* 按字符分割字符串
* @Function: split
* @Description:
*
*/
public static void split() {
String[] strs = "AS,DW,FSA,DWQD".split(",");
System.out.println(strs);
System.out.println("AS,DW,FSA,DWQD".split(",",3));
}
/**
* 当某个字符串调用该方法时,表示从当前字符串的firstStart位置开始,取一个长度为len的子串;然后从另一个字符串other的otherStart位置开始也取一个长度为len的子串,然后比较这两个子串是否相同,
*
* 如果这两个子串相同则返回true,否则返回false
* @Function: regionMatches
* @Description:
*
*/
public static void regionMatches() {
"abcdefg".matches("abcdefg");
/**
* boolean ignoreCase (可选) 是否忽略大小写
* int toffset 本字符串开始索引
* String other 另一字符串
* int ooffset 另一字符串开始索引
* int len 本字符串截取的长度
*/
"abcdefg".regionMatches(true,3, "def", 0, 3);
}
/**
* 在指定 String 数组B的每个元素之间串联指定的分隔符 A,从而产生单个串联的字符串
* @Function: join
* @Description:
*
*/
public static void join() {
List<String> names=new ArrayList<String>();
names.add("1");
names.add("2");
names.add("3");
// 1-2-3
System.out.println(String.join("-", names));
String[] arrStr=new String[]{"a","b","c"};
// a-b-c
System.out.println(String.join("-", arrStr));
}
}