Java基础-String

String

String API

Modifier and TypeMethod and Description
charcharAt(int index)
返回 char指定索引处的值。
intcodePointAt(int index)
返回指定索引处的字符(Unicode代码点)。
intcodePointBefore(int index)
返回指定索引之前的字符(Unicode代码点)。
intcodePointCount(int beginIndex, int endIndex)
返回此 String指定文本范围内的Unicode代码点数。
intcompareTo(String anotherString)
按字典顺序比较两个字符串。
intcompareToIgnoreCase(String str)
按字典顺序比较两个字符串,忽略病例差异。
Stringconcat(String str)
将指定的字符串连接到该字符串的末尾。
booleancontains(CharSequence s)
当且仅当此字符串包含指定的char值序列时才返回true。
booleancontentEquals(CharSequence cs)
将此字符串与指定的CharSequence进行 CharSequence 。
booleancontentEquals(StringBuffer sb)
将此字符串与指定的StringBuffer进行 StringBuffer 。
static StringcopyValueOf(char[] data)
相当于 valueOf(char[])。
static StringcopyValueOf(char[] data, int offset, int count)
相当于 valueOf(char[], int, int) 。
booleanendsWith(String suffix)
测试此字符串是否以指定的后缀结尾。
booleanequals(Object anObject)
将此字符串与指定对象进行比较。
booleanequalsIgnoreCase(String anotherString)
将此 String与其他 String比较,忽略案例注意事项。
static Stringformat(Locale l, String format, Object… args)
使用指定的区域设置,格式字符串和参数返回格式化的字符串。
static Stringformat(String format, Object… args)
使用指定的格式字符串和参数返回格式化的字符串。
byte[]getBytes()
使用平台的默认字符集将此 String编码为字节序列,将结果存储到新的字节数组中。
byte[]getBytes(String charsetName)
使用命名的字符集将此 String编码为字节序列,将结果存储到新的字节数组中。
voidgetChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将此字符串中的字符复制到目标字符数组中。
inthashCode()
返回此字符串的哈希码。
intindexOf(int ch)
返回指定字符第一次出现的字符串内的索引。
intindexOf(int ch, int fromIndex)
返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索。
intindexOf(String str)
返回指定子字符串第一次出现的字符串内的索引。
intindexOf(String str, int fromIndex)
返回指定子串的第一次出现的字符串中的索引,从指定的索引开始。
Stringintern()
返回字符串对象的规范表示。
booleanisEmpty()
返回 true如果,且仅当 length()为 0 。
static Stringjoin(CharSequence delimiter, CharSequence… elements)
返回一个新的字符串,由 CharSequence elements的副本组成,并附有指定的delimiter的 delimiter 。
static Stringjoin(CharSequence delimiter, Iterable<? extends CharSequence> elements)
返回一个新 String的副本组成 CharSequence elements与指定的副本一起加入 delimiter 。
intlastIndexOf(int ch)
返回指定字符的最后一次出现的字符串中的索引。
intlastIndexOf(int ch, int fromIndex)
返回指定字符的最后一次出现的字符串中的索引,从指定的索引开始向后搜索。
intlastIndexOf(String str)
返回指定子字符串最后一次出现的字符串中的索引。
intlastIndexOf(String str, int fromIndex)
返回指定子字符串的最后一次出现的字符串中的索引,从指定索引开始向后搜索。
intlength()
返回此字符串的长度。
booleanmatches(String regex)
告诉这个字符串是否匹配给定的 regular expression 。
intoffsetByCodePoints(int index, int codePointOffset)
返回此 String内的指数,与 index codePointOffset代码点。
booleanregionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。
booleanregionMatches(int toffset, String other, int ooffset, int len)
测试两个字符串区域是否相等。
Stringreplace(char oldChar, char newChar)
返回从替换所有出现的导致一个字符串 oldChar在此字符串 newChar 。
Stringreplace(CharSequence target, CharSequence replacement)
将与字面目标序列匹配的字符串的每个子字符串替换为指定的字面替换序列。
StringreplaceAll(String regex, String replacement)
用给定的替换替换与给定的 regular expression匹配的此字符串的每个子字符串。
StringreplaceFirst(String regex, String replacement)
用给定的替换替换与给定的 regular expression匹配的此字符串的第一个子字符串。
String[]split(String regex)
将此字符串分割为给定的 regular expression的匹配。
String[]split(String regex, int limit)
将这个字符串拆分为给定的 regular expression的匹配。
booleanstartsWith(String prefix)
测试此字符串是否以指定的前缀开头。
booleanstartsWith(String prefix, int toffset)
测试在指定索引处开始的此字符串的子字符串是否以指定的前缀开头。
CharSequencesubSequence(int beginIndex, int endIndex)
返回一个字符序列,该序列是该序列的子序列。
Stringsubstring(int beginIndex)
返回一个字符串,该字符串是此字符串的子字符串。
Stringsubstring(int beginIndex, int endIndex)
返回一个字符串,该字符串是此字符串的子字符串。
char[]toCharArray()
将此字符串转换为新的字符数组。
StringtoLowerCase()
将所有在此字符 String使用默认语言环境的规则,以小写。
StringtoLowerCase(Locale locale)
将所有在此字符 String ,以降低使用给定的规则情况下 Locale 。
StringtoString()
此对象(已经是字符串!)本身已被返回。
StringtoUpperCase()
将所有在此字符 String使用默认语言环境的规则大写。
StringtoUpperCase(Locale locale)
将所有在此字符 String使用给定的规则,大写 Locale 。
Stringtrim()
返回一个字符串,其值为此字符串,并删除任何前导和尾随空格。
staticString valueOf(boolean b)
返回 boolean参数的字符串 boolean形式。
staticString valueOf(char c)
返回 char参数的字符串 char形式。
staticString valueOf(char[] data)
返回 char数组参数的字符串 char形式。
staticString valueOf(char[] data, int offset, int count)
返回 char数组参数的特定子阵列的字符串 char形式。
staticString valueOf(double d)
返回 double参数的字符串 double形式。
staticString valueOf(float f)
返回 float参数的字符串 float形式。
staticString valueOf(int i)
返回 int参数的字符串 int形式。
staticString valueOf(long l)
返回 long参数的字符串 long形式。
staticString 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));
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值