首页 > 学院 > 开发设计 > 正文

String类的概念以及常用方法总结

2019-11-08 02:53:23
字体:
来源:转载
供稿:网友

概念:java字符串就是Unicode字符序列;在标准的Java类库中提供了一个预定的类,很自然地叫做String,每个用双引号括起来的字符串都是String类的一个实例.

String 字符串API

char—————-charAt (int index) 返回给定位置的代码单元。除非对底层的代码单元感兴趣,否则不需要调用这个方法。

int —————-codePointAt(int index) 返回从给定位位置开始或结束的代码点

int —————-offsetByCodePoints(int startIndex,int cpCount)5.0 返回从startIndex代码点开始,位移cpCount后的代码点索引。

int —————-compareTo(String other) 按照字典顺序,如果字符串位于ohter之前,返回一个负数。如果字符串位于other之后,返回一个正数、如果两个字符串相等,返回0.

boolean —————-endwith(String suffix) 如果字符串以suffix结尾,返回true

boolean —————-startwith(String suffix) 如果字符串以suffix开头,返回true

boolean —————-equals(object other) 如果字符串与other相等。返回true

boolean —————-equalsIgnoreCase(object other) 如果字符串与other相等。(不区分大小写)返回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开始计算。

int—————- length() 返回字符串的长度。

int —————-codePointCount(int startIndex ,int endIndex) 返回startsIndex和endIndex-1之间的代码点数量。没有配成对的代用字符将计入代码点。

String ——–replace(charSequence oldString ,charSquence newString) 返回一个新的字符串,这个字符串用newString代替原始字符串中所有的oldString。可以用String或StringBuilder对象作为charSequence参数

boolean —————-startsWith(String PRefix) 如果字符串以prefix字符串开始,返回true

String —————-subString(int beginIndex)

String —————-subString(int beginIndex,int endIndex) 返回一个新的字符串,这个字符串包含原始字符串中从beginIndex到串尾endIndex-1的所有代码单元。

String —————- toLowerCase() 返回一个新的字符串,这个字符串将原始字符串的所有大写字母改变成小写字母

String —————-toUpperCase() 返回一个新的字符串,这个字符串将原始的字符串所有小写字母改变成大写字母String —————-trim() 返回一个新的字符串。这个字符串将删除了原始字符串头和尾部的空格

空串与Null串 空串“”是长度为0的字符串;空串是一个java对象,有自己的长度(0)和内容(空)。不过,String变量换可以放一个特殊的值,名为null,这表示目前没有任何对象与该变量有关联; 有时要检查一个字符串既不是null也不是空串,这种情况下就需要使用以下条件: if(str !=null && str.length() !=0) 首先要检查str不为null,如果在一个null值上调用方法,会出现错误。


上一篇:第一个博客

下一篇:Recursion

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表