javaeye 写道
因为做的是网页数据抓取工作,相当多的时候是对字符串的处理工作。所以常用的类是JAVA中的String类、Pattern类、Matcher类了。其中String类的trim()方法和Matcher类的matches()方法,下面我们来讲讲它们的作用和注意地方。
以下是测试的类代码
class A{
public static void main(String[] args) {
A a=new A();
a.testTrim();
a.testRegex();
}
public void testTrim(){
String str[]=new String[4];
str[0]=" this is test method trim ";
str[1]=" this is test method trim ";
str[2]="\t this is test method trim \t";
str[3]=" \r\n this is test method trim \r\n\t ";
System.out.println("下面是来检测String中的trim()函数能去除那些字符:\n");
for (int i = 0; i < str.length; i++) {
System.out.println("|"+str[i].trim()+"|");
}
}
public void testRegex(){
String str[]=new String[4];
str[0]="\r\naaaaAAaaa\r\n";
str[1]=" aaaaBBaaa ";
str[2]=" aaaaCCaaa ";
str[3]="\taaaaDDaaa\t";
Pattern p=Pattern.compile(".*([A-Z]{2}).*");
System.out.println("\n没有在每个数组元素加trim()函数之后的结果是:\n");
for (int i = 0; i < str.length; i++) {
Matcher matcher=p.matcher(str[i]);
if(matcher.matches()){
System.out.println("匹配了第 "+i+" 元素,其值是:"+matcher.group(1));
}
else{
System.out.println("匹配第 "+i+" 元素失败");
}
}
System.out.println("\n在每个数组元素上加上trim()函数之后的结果是:\n");
for (int i = 0; i < str.length; i++) {
Matcher matcher=p.matcher(str[i].trim());
if(matcher.matches()){
System.out.println("匹配了第 "+i+" 元素,其值是:"+matcher.group(1));
}
else{
System.out.println("匹配第 "+i+" 元素失败");
}
}
}
}
//以下是代码运行的结果:
下面是来检测String中的trim()函数能去除那些字符:
| this is test method trim |
|this is test method trim|
|this is test method trim|
|this is test method trim|
没有在每个数组元素加trim()函数之后的结果是:
匹配第 0 元素失败
匹配了第 1 元素,其值是:BB
匹配了第 2 元素,其值是:CC
匹配了第 3 元素,其值是:DD
在每个数组元素上加上trim()函数之后的结果是:
匹配了第 0 元素,其值是:AA
匹配了第 1 元素,其值是:BB
匹配了第 2 元素,其值是:CC
匹配了第 3 元素,其值是:DD
javaeye 写道
从上面的结果中我们可以看到:
1.trim()方法是不能去除字符串两端的全角空格,即中文空格
2.matches方法是不能匹配含有换行符和回车符的字符串的。
分享到:
相关推荐
String.trim()Js里面给String添加trim()方法,实现去掉字符串两边空格String.trim()Js里面给String添加trim()方法,实现去掉字符串两边空格String.trim()Js里面给String添加trim()方法,实现去掉字符串两边...
String.prototype.trim 符合ES5规范的String.prototype.trim填充程序。 如果不可用,请调用其“ shim”方法对String.prototype.trim进行填充。 该软件包实现了接口。 它可以在ES3支持的环境中工作,并符合规范( ...
String的Trim()方法失效,在ie8下是有这样的情况的,解决方法也很简单使用$.trim(str)即可,需要的朋友可以了解下
//=> ' unicorn'有关的 left-与String#trim()类似,但仅删除左侧的空白 -与String#trim()类似,但仅删除换行符Tidelift帮助维护人员实现开源的可持续发展,同时为公司提供帮助有关其依赖项的安全性,维护和许可的...
2)string类经常用到find find_first_of find_first_not_of find_last_of find_last_not_of substr replace等,以及联合使用来达到java中的split和trim 3) 有些函数返回的是MyString& 、Char& 等(引用),MyString、...
主要介绍了java中string.trim()函数的作用实例及源码,具有一定借鉴价值,需要的朋友可以参考下
左修剪 与类似,但仅删除左侧的空白 安装 $ npm install trim-left 用法 import trimLeft from 'trim-left' ; trimLeft ( ' unicorn ' ) ; //=> 'unicorn ' ... -与String#trim()类似,但仅删除换行符
主要介绍了Java string.trim()究竟去掉了什么,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
String类 String replace(char oldChar, char newChar) String replace(CharSequence target, CharSequence replacement) String[] split(String regex) boolean contains(CharSequence s):当且仅当此字符串...
JAVA中String 有trim()方法去除字符串左右的空格,js中自定义trim方法,去除字符串左右的杂质,可以去除逗号、句号、空格等等特殊字符。
2.2.1 修剪(trim.hpp) 24 2.2.2 转换(case_conv.hpp) 24 2.2.3 判断式、断言函数(predicate.hpp)【Predicates】 24 2.2.4 查找 24 2.2.5 删除和替换 24 2.2.6 分割和组合 24 2.2.7 分词 24 2.2.8 其它 24 三、 C...
String.Prototype.trim() trim()返回一个字符串两端空白字符被删除的新字符串,不影响原字符串。 第一种实现方式:理论上算是比较不错的 if (!String.prototype.trim) { String.prototype.trim = function () { ...
trim函数.txt
2.2.1 修剪(trim.hpp) 24 2.2.2 转换(case_conv.hpp) 26 2.2.3 判断式、断言函数(predicate.hpp)【Predicates】 27 2.2.4 查找 28 2.2.5 删除和替换 29 2.2.6 分割和组合 31 2.2.7 其它 32 三、 C字符串 32 3.1 ...
std::string string_trim_LR_space(std::string str);//去除字符串前后的空白字符 std::string string_trim_L_space(std::string str);//去除字符串左边的空白字符 std::string string_trim_R_space(std::string ...
电源模块Trim引脚的应用pdf,电源模块Trim引脚的应用
javascript 的 trim 函数的实现,去掉字符串的左右空格,由于只使用了String的原生函数,没有使用正则,运算速度极快,是0毫秒级别的,推荐大家使用.
(3)string类经常用到find find_first_of find_first_not_of find_last_of find_last_not_of substr replace等,以及联合使用来达到java中的split和trim (4) 使用friend 仅仅是在类中进行声明的非内部 却可以访问...
return content.toString().trim(); } public static void main(String[] args) { System.out.println(IndexPath.Index_File_Path); String path = IndexPath.Index_File_Path; try { String...