首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

String种中的方法

2012-09-25 
String类中的方法String类在我们的面试中是一个经常被考到的考点,尤其是其中的方法也会经常被问到,不过之

String类中的方法

String类在我们的面试中是一个经常被考到的考点,尤其是其中的方法也会经常被问到,不过之前一直都没有对其产生重视,现在就总结下我在写程序中用到的几个函数:

?

?

String mm="abcdefr";mm=mm.replaceFirst("bc", "hello");

?

上面输出的mm的值为ahellodefr

其中replaceFirst()方法说明如下,第一个字符串是正则表达式,

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

?

?

?

String test1="my i speak to you. you are the person that i want to speak";test1=test1.replaceAll("you", "nancy");

?

上面输出的test1 的值为my i speak to nancy. nancy are the person that i want to speak

其中replaceAll方法说明如下,第一个字符串是正则表达式,

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

?

?

String test1="my i speak to you. you are the person that i want to speak";  test1=test1.replace("you", "nancy");

?

上面输出的test1的值和replaceAll方法的结果一样,不同的是其参数类型和replaceAll不一样。

其中replace方法说明如下:

replace(CharSequence?target, CharSequence?replacement)
??????????使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。

其中CharSequence?类型是用一个接口类型,它在String类中实现

?

?

?

?

String abc="Did you know that %NOUN% is %ADJECTIVE%?";String[] words = abc.split("[^\\w]");for(int i=0;i<words.length;i++)System.out.println(words[i]);

?

上面的输出结果为:

?

Did
you
know
that

NOUN

is

ADJECTIVE

?

?

具体解释如下:

\w匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'。 \W匹配任何非单词字符。等价于 '[^A-Za-z0-9_]'。

?

split(String?regex)
??????????根据给定正则表达式的匹配拆分此字符串。

热点排行