String类 字符串

charAt(int index):

参数index是指定的索引,返回该索引处的char值


compareTo(String anotherString):

  • 如果参数字符串等于此字符串,则返回值 0;
  • 如果此字符串小于字符串参数,则返回一个小于 0 的值;
  • 如果此字符串大于字符串参数,则返回一个大于 0 的值。

说明:

如果第一个字符和参数的第一个字符不等,结束比较,返回第一个字符的ASCII码差值。

如果第一个字符和参数的第一个字符相等,则以第二个字符和参数的第二个字符做比较,以此类推,直至不等为止,返回该字符的ASCII码差值。 如果两个字符串不一样长,可对应字符又完全一样,则返回两个字符串的长度差值。


 compareToIgnoreCase(String str):

compareToIgnoreCase() 方法用于按字典顺序比较两个字符串,不考虑大小写。

  • 如果参数字符串等于此字符串,则返回值 0;
  • 如果此字符串小于字符串参数,则返回一个小于 0 的值;
  • 如果此字符串大于字符串参数,则返回一个大于 0 的值。


concat(String str):

语法:public String concat(String s)

参数:s -- 要连接的字符串。

返回值:返回连接后的新字符串。


 contains(CharSequence s):

语法:public boolean contains(CharSequence chars)

参数:s -- 要判断的字符或字符串。

返回值:如果包含指定的字符或字符串返回 true,否则返回 false。


 copyValueOf(char[ ]  date):

 copyValueOf(char[] data, int offset, int count): 

语法:public  static  String  copyValueOf(char[ ] date)

           public static String copyValueOf(char[] data, int offset, int count)

参数:date -- 字符数组;offest -- 子数组的初识偏移量;count -- 子数组的长度;

返回值:返回指定数组中表示该字符序列的字符串


 endsWith(String suffix):

语法:public boolean endsWith(String suffix)

参数:suffix -- 指定的后缀

返回值:如果参数表示的字符序列是此对象表示的字符序列的后缀,则返回 true;否则返回 false。注意,如果参数是空字符串,或者等于此 String 对象(用 equals(Object) 方法确定),则结果为 true。


 equals(Object anObject):(会判断大小写)

语法:public boolean equals(Object anObject)

参数:anObject -- 与字符串比较的对象

返回值:如果给定对象与字符串相等,返回true;否则返回false。


 equalsIgnoreCase(String anotherString):(不会判断大小写区别)

语法:public  boolean  equalsIgnoreCase(String anotherString)


 format(String format,Object...args):

 format(Object...args):


getBytes():

getBytes(String charsetName):

语法:public byte[] getBytes()

           public byte[] getBytes(String charsetName)

参数:charsetName --  支持的字符集名称

返回值:返回byte数组


getBytes(int srcBegin,int srcEnd,char[ ] dst,int dstBegin):

语法:public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

参数:srcBegin -- 字符串中要复制的第一个字符的索引。

           srcEnd -- 字符串中要复制的最后一个字符之后的索引。

           dst -- 目标数组。

           dstBegin -- 目标数组中的起始偏移量。

返回值:没有返回值,但会抛出 IndexOutOfBoundsException 异常。


hashCode():

语法:public  int  hashCode()

返回值:返回对象的哈希码值


 indent(int n):


int  indexOf()方法有以下四种形式:

  • public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

  • public int indexOf(int ch, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

  • int indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

  • int indexOf(String str, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

语法: public int indexOf(int ch )

            public int indexOf(int ch, int fromIndex)

            int indexOf(String str)

            int indexOf(String str, int fromIndex)

参数:ch -- 字符,Unicode编码;

           fromIndex -- 开始搜索的索引位置,第一个字符是0,第二个是1,以此类推;

           str -- 要搜索的子字符串;


isEmpty():

语法:public  boolean  isEmpty()

返回值:如果字符串为空返回true;否则返回false

               字符串通过 length() 方法计算字符串长度,如果返回 0,即为空字符串。


lastIndexOf()方法有以下四种形式:

  • public int lastIndexOf(int ch): 返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

  • public int lastIndexOf(int ch, int fromIndex): 返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索,如果此字符串中没有这样的字符,则返回 -1。

  • public int lastIndexOf(String str): 返回指定子字符串在此字符串中最右边出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

  • public int lastIndexOf(String str, int fromIndex): 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索,如果此字符串中没有这样的字符,则返回 -1。

 语法:public int lastIndexOf(int ch) 

            public int lastIndexOf(int ch, int fromIndex) 

            public int lastIndexOf(String str) 

            public int lastIndexOf(String str, int fromIndex)

参数:ch -- 字符;fromIndex -- 开始搜索的索引位置;str -- 要搜索的子字符串;

返回值:指定子字符串在字符串中第一次出现处的索引值


 length():

语法:public  int  length()

返回值:返回字符串长度


 matches(String regex):

matches() 方法用于检测字符串是否匹配给定的正则表达式。

调用此方法的 str.matches(regex) 形式与以下表达式产生的结果完全相同:Pattern.matches(regex, str)

语法:public boolean matches(String regex)

参数:regex -- 匹配字符串的正则表达式

返回值:在字符串匹配给定的正则表达式时,返回true。


replace():

replace() 方法通过用 newChar 字符替换字符串中出现的所有 searchChar 字符,并返回替换后的新字符串。

语法:public String replace(char  searchChar,char newChar)

参数:searchChar -- 原字符;newChar -- 新字符。

返回值:替换后生产的新字符串


 replaceFirst():

replaceFirst() 方法使用给定的参数 replacement 替换字符串第一个匹配给定的正则表达式的子字符串。

语法:public String replaceFirst(String regex, String replacement)

参数:regex -- 匹配此字符串的正则表达式。

           replacement -- 用来替换第一个匹配项的字符串。

返回值:成功则返回替换的字符串,失败则返回原字符串。


 replaceAll():

replaceAll() 方法使用给定的参数 replacement 替换字符串所有匹配给定的正则表达式的子字符串。

语法:public String replaceAll(String regex, String replacement)

参数:与replaFirst相同

返回值:与replaFirst相同

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值