Java基本程序设计结构——String API

1.String API java.lang.string 1.0:


char charAt(int index)

返回给定位置的代码单元。


int codePointAt(int index)

返回从给定位置开始的码点


int offsetByCodePoints(int startIndex , int cpCount)

返回从startIndex代码点开始,位移cpCount后的码点索引


int compareTo(String other)

按照字典顺序,如果字符串位于other之前返回负数,之后返回正数,相等返回0


IntStream codePoints()

将这个字符串的码点作为一个流返回。调用toArray将他们放在一个数组中


new String(int[] codePoints , int offset , int count)

用数组中从offset开始的count个码点构造一个字符串


boolean equals(Object other)

如果字符串与other相等,返回true


boolean equalsIgnoreCase(String other)

如果字符串与other相等(忽略大小写),返回true


boolean startsWith(String prefix)

boolean endsWith(String suffix)

如果字符串以suffix开头或结尾,则返回true


int indexOf(String str)

int indexOf(String str , int fromIndex)

int indexOf(int cp)

int indexOf(int cp , int fromIndex)

返回与字符串str或者代码点cp匹配的第一个字串的开始位置。这个位置从索引0或fromIndex开始计算。如果在原始串中不存在str,返回-1


int lastIndexOf(String str)

int lastIndexOf(String str , int fromIndex)

int lastIndexOf(int cp)

int lastIndexOf(int cp , int fromIndex)

返回与字符串str或者代码点cp匹配的最后一个字串的开始位置。这个位置从原始串尾端或fromIndex开始计算。如果在原始串中不存在str,返回-1


int length()

返回字符串的长度


int codePointCount(int startIndex , int endIndex)

返回startIndex和endIndex-1之间的代码点数量。没有配成对的代用字符将计入代码点


String replace(CharSequence oldString , CharSequence newString)

返回一个新字符串。这个字符串用newString代替原始字符串中所有的oldString。可以用String或StringBuilder对象作为CharSequence参数


String substring(int beginIndex)

String substring(int beginIndex , int endIndex)

返回一个新字符串。这个字符串包含原始字符串中从beginIndex到串尾或endIndex-1的所有代码单元


String toLowerCase()

String toUpperCase()

返回一个新字符串。这个字符串将原始字符串中的大写字母改为小写字母,或者将原始字符串中的所有小写字母改成了大写字母


String trim()

返回一个新字符串。这个字符串将删除原始字符串头部和尾部的空格


String join(CharSequence delimiter , CharSequence... elements)

返回一个新字符串,用给定的定界符连接所有元素


CharSequence类型的参数是一种接口类型,所有字符串都属于这个接口


JDK5.0中引入StringBuilder类。这个类的前身是StringBuffer,效率有些低,但允许采用多线程的方式执行添加或者删除字符的操作。


2.StringBuilder java.lang.StringBuilder 5.0:


StringBuilder()

构造一个空的字符串构建器


int length()

返回构建器或缓冲器中的代码单元数量


StringBuilder append(String str)

追加一个字符串并返回this


StringBuilder append(char c)

追加一个代码单元并返回this


StringBuilder appendCodePoint(int cp)

追加一个代码点,并将其转换为一个或两个代码单元并返回this


void setCharAt(int i , char c)

将第i个代码单元设置为c


StringBuilder insert(int offset , String str)

在offset位置插入一个字符串并返回this


StringBuilder insert(int offset , char c)

在offset位置插入一个代码单元并返回this


StringBuilder delete(int startIndex , int endIndex)

删除偏移量从startIndex到endIndex-1的代码单元并返回this


String toString()

返回一个与构建器或缓冲器内容相同的字符串

猜你喜欢

转载自blog.csdn.net/qq_38386085/article/details/80576366