String类方法总结及简单应用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了String类方法总结及简单应用相关的知识,希望对你有一定的参考价值。
import java.io.UnsupportedEncodingException; import java.util.Locale; /** * Created by q0999 on 2017/9/5. */ public class StringTest { public static void main(String[] args) { /** * method: charAt() * define: public char charAt(int index) * function: 返回指定索引处的值 */ String charAtStr = "charAtStr"; char charAtCh = charAtStr.charAt(5); System.out.println(charAtCh); // charAtCh = t /** * method: codePointAt() * define: public int codePointAt(int index) * function: 返回指定索引处的字符(Unicode 代码点),其范围从 0 到 length() - 1 */ String codePointAtStr = "codePointAt"; int codePointAtInt = codePointAtStr.codePointAt(5); System.out.println(codePointAtInt); //codePointAtInt = 111 (小写o的Unicode码) /** * method: codePointBefore() * define: public int codePointBefore(int index) * function: 返回指定索引之前的字符(Unicode 代码点),其范围从 1 到 length */ String codePointBeforeStr = "codePointBefore"; int codePointBeforeInt = codePointBeforeStr.codePointBefore(5); System.out.println(codePointBeforeInt); //codePointBeforeInt = 80 (大写P的Unicode码) /** * method: codePointCount() * define: public int codePointCount(int beginIndex, int endIndex) * function: 返回此 String 的指定文本范围中的 Unicode 代码点数,文本范围始于指定的 beginIndex,一直到索引 endIndex - 1 处的 char */ String codePointCountStr = "codePointCount"; int codePointCountInt = codePointCountStr.codePointCount(2,5); System.out.println(codePointCountInt); //codePointCountInt = 3 (d,e,P的代码点数为3) /** * method: offsetByCodePoints() * define: public int offsetByCodePoints(int index, int codePointOffset) * function: 返回此 String 中从给定的 index 处偏移 codePointOffset 个代码点的索引 */ String offsetByCodePointsStr = "offsetByCodePoints"; int offsetByCodePointsInt = offsetByCodePointsStr.offsetByCodePoints(2,5); System.out.println(offsetByCodePointsInt); //offsetByCodePointsInt = 7 /** * method: getChars() * define: public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) * function: 将字符从此字符串复制到目标字符数组 */ String getCharsStr = "getCharsStr"; char[] chars = {‘*‘,‘*‘,‘*‘,‘*‘,‘*‘,‘*‘,‘*‘,‘*‘,‘*‘,‘*‘}; try{ getCharsStr.getChars(0,8,chars,1); System.out.println(chars); // chars = *getChars* } catch(Exception e) { System.out.println("Failed to test getChars()!"); } /** * method: getBytes() * define1: public byte[] getBytes(String charsetName) throws UnsupportedEncodingException * function1: 使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中 * define2: public byte[] getBytes() * function2: 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中 */ String getBytesStr = "getBytesStr"; try{ byte[] bytes1 = getBytesStr.getBytes("UTF-8"); System.out.println(bytes1.toString()); // bytes1 = [[email protected] byte[] bytes2 = getBytesStr.getBytes(); System.out.println(bytes2.toString()); // bytes2 = [[email protected] } catch (UnsupportedEncodingException e) { System.out.println("不支持的字符集!"); } /** * method: equals() * define: public boolean equals(Object anObject) * function: 将此字符串与指定的对象比较。当且仅当该参数不为 null,并且是与此对象表示相同字符序列的 String 对象时,结果才为 true */ String equalsStr = "equals"; boolean equalsBool1 = equalsStr.equals("equals"); System.out.println(equalsBool1); // equalsBool1 = true boolean equalsBool2 = equalsStr.equals("equal"); System.out.println(equalsBool2); // equalsBool2 = false /** * method: contentEquals() * define1: public boolean contentEquals(StringBuffer sb) * function1: 将此字符串与指定的 StringBuffer 比较。当且仅当此 String 与指定 StringBuffer 表示相同的字符序列时,结果才为 true * define2: public boolean contentEquals(CharSequence cs) * function2: 将此字符串与指定的 CharSequence 比较。当且仅当此 String 与指定序列表示相同的 char 值序列时,结果才为 true */ String contentStr = "contentEquals"; StringBuffer contentSb = new StringBuffer("contentEquals"); boolean contentBool1 = contentStr.contentEquals(contentSb); System.out.println(contentBool1); // contentBool1 = true CharSequence contentChar = "contentEquals"; boolean contentBool2 = contentStr.contentEquals(contentChar); System.out.println(contentBool2); // contentBool2 = true /** * method: equalsIgnoreCase() * define: public boolean equalsIgnoreCase(String anotherString) * function: 将此 String 与另一个 String 比较,不考虑大小写。如果两个字符串的长度相同,并且其中的相应字符都相等(忽略大小写), * 则认为这两个字符串是相等的 */ String caseStr1 = "equalsIgnoreCase"; String caseStr2 = "EQUALSIGNORECASE"; boolean caseBool = caseStr1.equalsIgnoreCase(caseStr2); System.out.println(caseBool); // caseBool = true /** * method: compareTo() * define: public int compareTo(String anotherString) * function: 按字典顺序比较两个字符串。该比较基于字符串中各个字符的 Unicode 值。如果参数字符串等于此字符串,则返回值 0; * 如果此字符串按字典顺序小于字符串参数,则返回一个小于 0 的值;如果此字符串按字典顺序大于字符串参数,则返回一个大于 0 的值 */ String compareStr1 = "abc"; String compareStr2 = "abd"; int compareInt1 = compareStr1.compareTo(compareStr2); System.out.println(compareInt1); // compareInt = -1 /** * method: compareToIgnoreCase() * define: public int compareToIgnoreCase(String str) * function: 按字典顺序比较两个字符串,不考虑大小写 */ String compareStr3 = "ABD"; int compareInt2 = compareStr1.compareToIgnoreCase(compareStr3); int compareInt3 = compareStr2.compareToIgnoreCase(compareStr3); System.out.println(compareInt2); // compareInt2 = -1 System.out.println(compareInt3); // compareInt3 = 0 /** * method: startsWith() * define1: public boolean startsWith(String prefix, int toffset) * function1: 测试此字符串从指定索引开始的子字符串是否以指定前缀开始 * define2: public boolean startsWith(String prefix) * function2: 测试此字符串是否以指定的前缀开始 */ String startsStr = "V1.3.1_Str1_starts"; boolean startBool1 = startsStr.startsWith("Str1",7); boolean startBool2 = startsStr.startsWith("V1.3.1"); System.out.println(startBool1); // startBool1 = true System.out.println(startBool2); // startBool2 = true /** * method: endsWith() * define: public boolean endsWith(String suffix) * function: 测试此字符串是否以指定的后缀结束 */ String endsStr = "alibaba.zip"; boolean endsBool = endsStr.endsWith("zip"); System.out.println(endsBool); // endBool = true /** * method: hashCode() * define: public int hashCode() * function: 返回此字符串的哈希码 */ String hashStr = "hash"; int hashInt = hashStr.hashCode(); System.out.println(hashInt); //hashInt = 3195150 /** * method: indexOf() * define1: public int indexOf(String str) * function1: 返回指定字符串在此字符串中第一次出现处的索引 * define2: public int indexOf(String str, int fromIndex) * function: 返回在此字符串中第一次出现指定字符串处的索引,从指定的索引开始搜索 */ String indexStr = "indexOf.indexOf"; int indexInt1 = indexStr.indexOf("Of"); System.out.println(indexInt1); // indexInt = 5 int indexInt2 = indexStr.indexOf("Of",8); System.out.println(indexInt2); // indexInt2 = 13 /** * method: lastIndexOf() * define1: public int lastIndexOf(String str) * function1: 返回指定字符串在此字符串中最后一次出现处的索引 * define2: public int lastIndexOf(String str, int fromIndex) * function2: 返回指定字符串在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索 */ int lastIndexInt1 = indexStr.lastIndexOf("Of"); System.out.println(lastIndexInt1); // lastIndexInt1 = 13 int lastIndexInt2 = indexStr.lastIndexOf("Of",8); System.out.println(lastIndexInt2); // lastIndexInt2 = 5 /** * method: substring() * define1: public String substring(int beginIndex) * function1: 返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾 * define2: public String substring(int beginIndex, int endIndex) * function2: 返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符 */ String subStr = "subString"; String newSubStr1 = subStr.substring(3); System.out.println(newSubStr1); // newSubStr = String String newSubStr2 = subStr.substring(3,6); System.out.println(newSubStr2); // newSubStr2 = Str /** * method: subSequence() * define: public CharSequence subSequence(int beginIndex, int endIndex) * function: 返回一个新的字符序列,它是此序列的一个子序列。该子序列从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符 */ CharSequence ch = "subSequence"; CharSequence newCh = ch.subSequence(3,6); System.out.println(newCh); // newCh = Seq /** * method: concat() * define: public String concat(String str) * function: 将指定字符串连接到此字符串的结尾 */ String concatStr = "concat"; String newConcatStr = concatStr.concat("concat"); System.out.println(newConcatStr); // newConcatStr = concatconcat /** * method: replace() * define: public String replace(char oldChar, char newChar) * function: 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的 */ String replaceStr = "replaceREPLACE"; String newReplaceStr = replaceStr.replace("REPLACE","replace"); System.out.println(newReplaceStr); // newReplaceStr = replacereplace /** * method: matches() * define; public boolean matches(String regex) * function: 告知此字符串是否匹配给定的 正则表达式 */ String matchesStr = "t5"; boolean matchBool = matchesStr.matches("^[a-z][0-9]"); System.out.println(matchBool); // matchBool = true /** * method: contains() * define: public boolean contains(CharSequence s) * function: 当且仅当此字符串包含指定的 char 值序列时,返回 true */ String conStr = "contains"; boolean conBool = conStr.contains("con"); System.out.println(conBool); // conBool = true /** * method: replaceFirst() * define: public String replaceFirst(String regex, String replacement) * function: 使用给定的 replacement 替换此字符串匹配给定的正则表达式的第一个子字符串 */ String firstStr = "d2Firsto2"; String newFirstStr1 = firstStr.replaceFirst("[a-z][0-9]","replace"); System.out.println(newFirstStr1); // newFirstStr = replaceFirstd2 /** * method: replaceAll() * define: public String replaceAll(String regex, String replacement) * function: 使用给定的 replacement 替换此字符串所有匹配给定的 正则表达式的子字符串 */ String newFirstStr2 = firstStr.replaceAll("[a-z][0-9]","replace"); System.out.println(newFirstStr2); // newFirstStr2 = replaceFirstreplace /** * method: split() * define1: public String[] split(String regex, int limit) * function1: 根据匹配给定的 正则表达式来拆分此字符串。limit 参数控制模式应用的次数,因此影响所得数组的长度。如果该限制 n 大于 0, * 则模式将被最多应用 n - 1 次,数组的长度将不会大于 n,而且数组的最后一项将包含所有超出最后匹配的定界符的输入。 * 如果 n 为非正,那么模式将被应用尽可能多的次数,而且数组可以是任何长度。如果 n 为 0,那么模式将被应用尽可能多的次数, * 数组可以是任何长度,并且结尾空字符串将被丢弃。 * define2: public String[] split(String regex) * function2: 根据给定 正则表达式的匹配拆分此字符串 */ String splStr = "split,method,test"; String[] splArray1 = splStr.split(",",2); for(int i = 0; i < splArray1.length; i++) { System.out.println(splArray1[i].toString()); // splArray1 = {"split","method,test"} } String[] splArray2 = splStr.split(","); for(int i = 0; i < splArray2.length; i++) { System.out.println(splArray2[i].toString()); // splAttay2 = {"split","method","test"} } /** * method: trim() * define: public String trim() * function: 返回字符串的副本,忽略前导空白和尾部空白 */ String trimStr = " trim "; String newTrimStr = trimStr.trim(); System.out.println("‘" + newTrimStr + "‘"); // newTrimStr = ‘trim‘ /** * method: toCharArray() * define: public char[] toCharArray() * function: 将此字符串转换为一个新的字符数组 */ String toCharStr = "to"; char[] array = toCharStr.toCharArray(); for(int i = 0; i < array.length; i++){ System.out.println(array[i]); // array = {‘t‘,‘o‘} } /** * method: format() * define1: public static String format(String format, Object... args) * function1: 使用指定的格式字符串和参数返回一个格式化字符串 * define2: public static String format(Locale locale, String format, Object... args) * function2: 使用指定的语言环境、格式字符串和参数返回一个格式化字符串 */ String formatStr = null; formatStr = String.format("halo,%s:%s.%s","张三","李四","王五"); System.out.println(formatStr); // formatStr = "halo,张三:李四.王五" formatStr = String.format(Locale.CHINA,"halo,%s:%s.%s","张三","李四","王五"); System.out.println(formatStr); // formatStr = "halo,张三:李四.王五" /** * method: copyValueOf() * define: public static String copyValueOf(char[] data, int offset, int count) * function: 返回指定数组中表示该字符序列的 String */ char[] copyCh = {‘*‘,‘c‘,‘o‘,‘p‘,‘y‘,‘*‘}; String copyStr = String.copyValueOf(copyCh,1,4); System.out.println(copyStr); // copyStr = copy } }
以上是关于String类方法总结及简单应用的主要内容,如果未能解决你的问题,请参考以下文章
java常用类详细介绍及总结:字符串相关类日期时间API比较器接口SystemMathBigInteger与BigDecimal