String类使用

一、构造方法

1、public String(char[] value,int offset,int count)

  • 参数:
    value - 作为字符源的数组。
    offset - 初始偏移量。
    count - 长度。
char[] a={'a','d','f','r','d','e'};
         String s = new String(a,2,4);
         System.out.println(s);
   //输出:frde

2、public String(StringBuffer buffer)
分配一个新的字符串,它包含字符串缓冲区参数中当前包含的字符序列。该字符串缓冲区的内容已被复制,后续对它的修改不会影响新创建的字符串。
参数:
buffer - 一个 StringBuffer

二、方法

1、public char charAt(int index)
返回指定索引处的 char 值。索引范围为从 0 到 length() - 1。序列的第一个 char 值位于索引 0 处,第二个位于索引 1 处,依此类推,这类似于数组索引。

2、public int codePointAt(int index)
返回指定索引处的字符的int值

3、public boolean startsWith(String prefix)
测试此字符串是否以指定的前缀开始。

4、public boolean startsWith(String prefix,int toffset)
测试此字符串从指定索引开始的子字符串是否以指定前缀开始。

 char[] a={'a','d','f','r','d','e'};
         String s = new String(a);
         boolean t =s.startsWith("fr",2);
         System.out.println(t);
        //字符串的子字符串frde以什么开头

5、public int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。

char[] a={'a','d','f','r','d','e'};
         String s = new String(a);
         int t = s.indexOf('f');
         System.out.println(t);
         //输出2

6、public int lastIndexOf(int ch)
返回指定字符在此字符串中最后一次出现处的索引。

7、public int lastIndexOf(int ch,int fromIndex)
返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。

8、public int indexOf(String str)
返回指定子字符串在此字符串中第一次出现处的索引

9、public String substring(int beginIndex)
返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。

"unhappy".substring(2) returns "happy"
"Harbison".substring(3) returns "bison"
"emptiness".substring(9) returns "" (an empty string)

10、public String substring(int beginIndex,int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。

  • 参数:
    beginIndex - 起始索引(包括)。
    endIndex - 结束索引(不包括)。
"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"

11、public String concat(String str)
将指定字符串连接到此字符串的结尾。

"cares".concat("s") returns "caress"
"to".concat("get").concat("her") returns "together"

12、public String replace(char oldChar,char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
如果 oldChar 在此 String 对象表示的字符序列中没有出现,则返回对此 String 对象的引用。否则,创建一个新的 String 对象,它所表示的字符序列除了所有的 oldChar 都被替换为 newChar 之外,与此 String 对象表示的字符序列相同。

"mesquite in your cellar".replace('e', 'o')
returns "mosquito in your collar"
"the war of baronets".replace('r', 'y')
returns "the way of bayonets"
"sparring with a purple porpoise".replace('p', 't')
returns "starring with a turtle tortoise"
"JonL".replace('q', 'x') returns "JonL" (no change)

13、public String replaceAll(String regex,String replacement)
使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。

14、public String[] split(String regex,int limit)
根据匹配给定的正则表达式来拆分此字符串。

//例如,字符串 "boo:and:foo" 使用这些参数可生成以下结果:

这里写图片描述

15、public String[] split(String regex)
根据给定正则表达式的匹配拆分此字符串。该方法的作用就像是使用给定的表达式和限制参数 0 来调用两参数 split 方法。因此,所得数组中不包括结尾空字符串。

//例如,字符串 "boo:and:foo" 使用这些参数可生成以下结果:

这里写图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值