Java String类

字符串广泛应用在Java 编程中,在Java中字符创属于对象,Java提供了String类来创建操作字符串。

1.求字符串的长度

       length()方法 ,它返回的是字符对象中包含的字符数。

2.连接字符串

    string1.concat(string2) ,返回的是string2连接string1的新的字符串。

    我们常用的是+连接字符串。

3.创建格式化字符串

   我们知道输出格式化数字可以使用printf() 和format()方法。

4.String 的方法

   

//语法:
(1)public char charAt(int index) //用于返回指定索引处的字符。


(2)int compareTo(Object o)    //o是要比较的对象
或 int compareTo(String anotherString)  //anotherString 要比较的字符串 
返回值:如果参数的字符串等于此字符串 就返回0
       如果参数的字符串小于此字符串,则返回一个大于0的值
       如果参数的字符串大于此字符串,则返回一个小于0的值
(3)int compareToIgnoreCase(String s) //按字典序列比较两个字符串,不考虑大小写,返回值和上一个函数相同的。
(4)public boolean contentEquals(StringBuffer sb); 方法是将此字符串与指定的StringBuffer比较
(5)public static String copyValueOf(char[] data); 返回指定数组中表示该字符序列的字符串
    public static String copyValueOf(char[],int offset,int count);
 (6) public boolean endsWith(String suffix)//方法用于测试字符串是以指定的后缀结束。
(7)public void getChars(int srcBegin,int srcEnd,char[]dst,int dstBegin) 
    srcBegin -- 字符串中要复制的第一个字符的索引。

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

    dst -- 目标数组。

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

   返回值:没有返回值,但是会抛出IndexOutOfBoundsException异常。
(8)public int hashCode(); 
    字符串对象的哈希码根据下面公式计算
    s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
(9)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)
(10)astIndexOf() 方法有以下四种形式:

    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)
(11)trim() 方法用于删除字符串的头尾空白符。
 (12)toUpperCase()//将字符串小写的转换为大写。
   public String toUpperCase()
  或者
   public String toUpperCase(Locale locale)
(13)public char[] toCharArray()
   方法将字符串装换为字符数组
 (14)public String substring(int beginIndex)
     或:public String substring(int beginIndex,int endIndex)
(15)public String[] split(String regex, int limit)
    split() 方法根据匹配给定的正则表达式来拆分字符串。
    注意: . 、 | 和 * 等转义字符,必须得加 \\。
    注意:多个分隔符,可以用 | 作为连字符。
    参数: regex -- 正则表达式分隔符。limit -- 分割的份数。
    返回值:字符串数组。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值