- 浏览: 335382 次
- 性别:
- 来自: 重庆
文章分类
最新评论
-
hjl0722:
...
Java中的异或 -
lucd:
f(New.<Person, List<Pet&g ...
第15章泛型 -
liujunhao225:
[Error: could not access: List; ...
mvel的使用 -
superscorpio:
public void testImportInContex ...
mvel的使用 -
yuyangtina:
哦,知道了,是继承的方法。谢谢你的分享。
HttpClient3.x发送Soap请求的方法
1.
public class Concatenation {
public static void main(String[] args) {
String mango = "mango";
String s = "abc" + mango + "def" + 47;
System.out.println(s);
}
}
上面这段代码,编译器会自动引入StringBuilder来构造s对象。这种情况下编译器会自动地为你优化性能。
但是,不要盲目相信编译器,请看下面的代码:
public class WhitherStringBuilder {
//这种构建字符串的方式,没循环一次都要构建一个StringBuilder对象,很影响性能
public String implicit(String[] fields) {
String result = "";
for(int i = 0; i < fields.length; i++)
result += fields[i];
return result;
}
//这种显示地构建StringBuilder对象,比较好,它不会再每次循环都构建对象。
public String explicit(String[] fields) {
StringBuilder result = new StringBuilder();
for(int i = 0; i < fields.length; i++)
result.append(fields[i]);
return result.toString();
}
}
2.在使用StringBuilder的append的时,参数不能是字符串相加,例如:
append(a+":"+b),这样编译器会掉入陷阱,从而为你创建一个StringBuilder对象来处理括号内的字符串操作。
3.无意识的递归
public class InfiniteRecursion {
public String toString() {
return " InfiniteRecursion address: " + this + "\n";
}
public static void main(String[] args) {
List<InfiniteRecursion> v =
new ArrayList<InfiniteRecursion>();
for(int i = 0; i < 10; i++)
v.add(new InfiniteRecursion());
System.out.println(v);
}
}
当字符串后跟+,而+后面又不是字符串时,编译器就会试着把它转换成字符串,从而调用其toString方法,这样就造成了递归,从而
在运行时报错。正确的做法是:super.toString()
4.String类的方法都会返回一个新的String对象。同时,如果没有发生改变,String的方法只是返回指向原来对象的引用而已,这样
可以节省存储空间和避免额外的开销。
5.正则表达式就是以某种方式来描述字符串。在java中“\\”表示要插入一个正则表达式的反斜杠。
6.
\d 表示[0-9]
\D 表示[^0-9]非数字
\w 表示[a-zA-Z0-9]
\W 表示[^\w]
. 匹配任意字符 所以,在java中要匹配点,必须把它转义成普通的点“\\.”
x? 表示0次或一次
x+ 表示1次或多次
x* 表示0次货多次
x{n} 恰好n次
x{n,} 至少n次
x{n,m} 至少n次,最多m次
例如:
public static void main(String[] args) {
System.out.println("--4".matches("-?\\d+"));
System.out.println("5678".matches("-?\\d+"));
System.out.println("+911".matches("-?\\d+"));
//表示一个-或+,或二者都没有,|表示或得意思。因为在+在正则表达式中有特殊的含义,所有必须用\\将其转义成普通的+号
System.out.println("+911".matches("(-|\\+)?\\d+"));
}
7.abc+,不是表示匹配1次或多次abc,是表示匹配1次或多次c,(abc)+,这样才是匹配abc;
8.
[size=x-large][/size]
第二部分 相关类说明:
1.java.util.regex
Class Pattern
public final class Pattern
extends Object
implements Serializable
A compiled representation of a regular expression.
A regular expression, specified as a string, must first be compiled into an instance of this class. The resulting pattern can then be used to create a
Matcher object that can match arbitrary character sequences against the regular expression. All of the state involved in performing a match resides in the
matcher, so many matchers can share the same pattern.
A typical invocation sequence is thus
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();
A matches method is defined by this class as a convenience for when a regular expression is used just once. This method compiles an expression and matches an
input sequence against it in a single invocation. The statement
boolean b = Pattern.matches("a*b", "aaaaab");
is equivalent to the three statements above, though for repeated matches it is less efficient since it does not allow the compiled pattern to be reused.
一些正则表达式:
\\ The backslash character
\s 空白字符:[ \t\n\x0B\f\r]
\S 非空白字符:[^\s]
p("192".matches("[0-2][0-9][0-9]"));//true []表示范围,[0-2]表示在0到2范围内一个数字,[0-9]表示在0到9范围内一个数字
p("a".matches("[abc]"));// true ,匹配中括号内有一个a
p("a".matches("[abc]"));// true ,匹配中括号内有一个a
p("a".matches("[^abc]"));//false,匹配除了abc以外的任意字符
p("a".matches("[a-zA-Z]"));//true,匹配小写的a-z或者大写的A-Z范围内一个字符
p("a".matches("[a-z | A-Z]"));//true 匹配小写的a-z或者大写的A-Z范围内一个字符
p("a".matches("[a-z[A-Z]]"));//true 匹配小写的a-z或者大写的A-Z范围内一个字符
p("R".matches("[A-Z && [RGB]]"));//true 必须在A-Z范围内并且是RGB范围中的一个
[]表示范围,一个中括号匹配一个字符
p(" \n\r\t\f".matches("\\s{5}"));//true,匹配5个空白字符
p(" ".matches("\\s"));//true,匹配一个空白字符
p(" ".matches("\\S"));//false,匹配一个非空白字符
p("a_9".matches("\\w{3}"));//true,匹配3位字符组成的单词
p("$_%".matches("\\w{3}"));//false
p("abc866666%&^#".matches("[a-z]{1,3}\\d+[&%^#]+"));//true,a-z 出现1次到3次,数字出现一次或多次,&%^# 出现一次或多次
p("\\abc".matches("\\\\[a-z A-Z]{1,3}"));//true,正则表达式匹配一个反斜线必须用\\
边界匹配器
^ 行的开头 $ 行的结尾 \b 单词边界 \z 输入的结尾
// \B 非单词边界 \A 输入的开头 \G 上一个匹配的结尾 \Z 输入的结尾,仅用于最后的结束符(如果有的话)
//匹配以h开头的紧跟a-z的字符出现1到3次,后接o的单词边界,后面为零个或者多个任意的字符
p("hello world".matches("^h[a-z]{1,3}o\\b.*"));//true
p("hello world".matches("^h.*d$"));//true,匹配以h开头的后接任意零个或者多个以d结尾的字符
p("helloWorld".matches("^h[a-z]{1,3}o\\b.*"));//false
捕获组()里面的最为一个整体
捕获组(capturing group)是将多个字符作为单独的单元来对待的一种方式
对一个正则表达式编译之后,可以重复使用;
相关方法:
(1)compile
public static Pattern compile(String regex)
Compiles the given regular expression into a pattern.
Parameters:
regex - The expression to be compiled
(2)matcher
public Matcher matcher(CharSequence input)
Creates a matcher that will match the given input against this pattern.
Parameters:
input - The character sequence to be matched
Returns:
A new matcher for this pattern
(3)matches
public static boolean matches(String regex,
CharSequence input)
Compiles the given regular expression and attempts to match the given input against it.
An invocation of this convenience method of the form
Pattern.matches(regex, input);
behaves in exactly the same way as the expression
Pattern.compile(regex).matcher(input).matches()
If a pattern is to be used multiple times, compiling it once and reusing it will be more efficient than invoking this method each time.
Parameters:
regex - The expression to be compiled
input - The character sequence to be matched
2.java.lang.Object
java.util.regex.Matcher
An engine that performs match operations on a character sequence by interpreting a Pattern.
相关方法:
(1)matches
public boolean matches()
Attempts to match the entire region against the pattern.
If the match succeeds then more information can be obtained via the start, end, and group methods.
Returns:
true if, and only if, the entire region sequence matches this matcher's pattern
public class Concatenation {
public static void main(String[] args) {
String mango = "mango";
String s = "abc" + mango + "def" + 47;
System.out.println(s);
}
}
上面这段代码,编译器会自动引入StringBuilder来构造s对象。这种情况下编译器会自动地为你优化性能。
但是,不要盲目相信编译器,请看下面的代码:
public class WhitherStringBuilder {
//这种构建字符串的方式,没循环一次都要构建一个StringBuilder对象,很影响性能
public String implicit(String[] fields) {
String result = "";
for(int i = 0; i < fields.length; i++)
result += fields[i];
return result;
}
//这种显示地构建StringBuilder对象,比较好,它不会再每次循环都构建对象。
public String explicit(String[] fields) {
StringBuilder result = new StringBuilder();
for(int i = 0; i < fields.length; i++)
result.append(fields[i]);
return result.toString();
}
}
2.在使用StringBuilder的append的时,参数不能是字符串相加,例如:
append(a+":"+b),这样编译器会掉入陷阱,从而为你创建一个StringBuilder对象来处理括号内的字符串操作。
3.无意识的递归
public class InfiniteRecursion {
public String toString() {
return " InfiniteRecursion address: " + this + "\n";
}
public static void main(String[] args) {
List<InfiniteRecursion> v =
new ArrayList<InfiniteRecursion>();
for(int i = 0; i < 10; i++)
v.add(new InfiniteRecursion());
System.out.println(v);
}
}
当字符串后跟+,而+后面又不是字符串时,编译器就会试着把它转换成字符串,从而调用其toString方法,这样就造成了递归,从而
在运行时报错。正确的做法是:super.toString()
4.String类的方法都会返回一个新的String对象。同时,如果没有发生改变,String的方法只是返回指向原来对象的引用而已,这样
可以节省存储空间和避免额外的开销。
5.正则表达式就是以某种方式来描述字符串。在java中“\\”表示要插入一个正则表达式的反斜杠。
6.
\d 表示[0-9]
\D 表示[^0-9]非数字
\w 表示[a-zA-Z0-9]
\W 表示[^\w]
. 匹配任意字符 所以,在java中要匹配点,必须把它转义成普通的点“\\.”
x? 表示0次或一次
x+ 表示1次或多次
x* 表示0次货多次
x{n} 恰好n次
x{n,} 至少n次
x{n,m} 至少n次,最多m次
例如:
public static void main(String[] args) {
System.out.println("--4".matches("-?\\d+"));
System.out.println("5678".matches("-?\\d+"));
System.out.println("+911".matches("-?\\d+"));
//表示一个-或+,或二者都没有,|表示或得意思。因为在+在正则表达式中有特殊的含义,所有必须用\\将其转义成普通的+号
System.out.println("+911".matches("(-|\\+)?\\d+"));
}
7.abc+,不是表示匹配1次或多次abc,是表示匹配1次或多次c,(abc)+,这样才是匹配abc;
8.
[size=x-large][/size]
第二部分 相关类说明:
1.java.util.regex
Class Pattern
public final class Pattern
extends Object
implements Serializable
A compiled representation of a regular expression.
A regular expression, specified as a string, must first be compiled into an instance of this class. The resulting pattern can then be used to create a
Matcher object that can match arbitrary character sequences against the regular expression. All of the state involved in performing a match resides in the
matcher, so many matchers can share the same pattern.
A typical invocation sequence is thus
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();
A matches method is defined by this class as a convenience for when a regular expression is used just once. This method compiles an expression and matches an
input sequence against it in a single invocation. The statement
boolean b = Pattern.matches("a*b", "aaaaab");
is equivalent to the three statements above, though for repeated matches it is less efficient since it does not allow the compiled pattern to be reused.
一些正则表达式:
\\ The backslash character
\s 空白字符:[ \t\n\x0B\f\r]
\S 非空白字符:[^\s]
p("192".matches("[0-2][0-9][0-9]"));//true []表示范围,[0-2]表示在0到2范围内一个数字,[0-9]表示在0到9范围内一个数字
p("a".matches("[abc]"));// true ,匹配中括号内有一个a
p("a".matches("[abc]"));// true ,匹配中括号内有一个a
p("a".matches("[^abc]"));//false,匹配除了abc以外的任意字符
p("a".matches("[a-zA-Z]"));//true,匹配小写的a-z或者大写的A-Z范围内一个字符
p("a".matches("[a-z | A-Z]"));//true 匹配小写的a-z或者大写的A-Z范围内一个字符
p("a".matches("[a-z[A-Z]]"));//true 匹配小写的a-z或者大写的A-Z范围内一个字符
p("R".matches("[A-Z && [RGB]]"));//true 必须在A-Z范围内并且是RGB范围中的一个
[]表示范围,一个中括号匹配一个字符
p(" \n\r\t\f".matches("\\s{5}"));//true,匹配5个空白字符
p(" ".matches("\\s"));//true,匹配一个空白字符
p(" ".matches("\\S"));//false,匹配一个非空白字符
p("a_9".matches("\\w{3}"));//true,匹配3位字符组成的单词
p("$_%".matches("\\w{3}"));//false
p("abc866666%&^#".matches("[a-z]{1,3}\\d+[&%^#]+"));//true,a-z 出现1次到3次,数字出现一次或多次,&%^# 出现一次或多次
p("\\abc".matches("\\\\[a-z A-Z]{1,3}"));//true,正则表达式匹配一个反斜线必须用\\
边界匹配器
^ 行的开头 $ 行的结尾 \b 单词边界 \z 输入的结尾
// \B 非单词边界 \A 输入的开头 \G 上一个匹配的结尾 \Z 输入的结尾,仅用于最后的结束符(如果有的话)
//匹配以h开头的紧跟a-z的字符出现1到3次,后接o的单词边界,后面为零个或者多个任意的字符
p("hello world".matches("^h[a-z]{1,3}o\\b.*"));//true
p("hello world".matches("^h.*d$"));//true,匹配以h开头的后接任意零个或者多个以d结尾的字符
p("helloWorld".matches("^h[a-z]{1,3}o\\b.*"));//false
捕获组()里面的最为一个整体
捕获组(capturing group)是将多个字符作为单独的单元来对待的一种方式
对一个正则表达式编译之后,可以重复使用;
相关方法:
(1)compile
public static Pattern compile(String regex)
Compiles the given regular expression into a pattern.
Parameters:
regex - The expression to be compiled
(2)matcher
public Matcher matcher(CharSequence input)
Creates a matcher that will match the given input against this pattern.
Parameters:
input - The character sequence to be matched
Returns:
A new matcher for this pattern
(3)matches
public static boolean matches(String regex,
CharSequence input)
Compiles the given regular expression and attempts to match the given input against it.
An invocation of this convenience method of the form
Pattern.matches(regex, input);
behaves in exactly the same way as the expression
Pattern.compile(regex).matcher(input).matches()
If a pattern is to be used multiple times, compiling it once and reusing it will be more efficient than invoking this method each time.
Parameters:
regex - The expression to be compiled
input - The character sequence to be matched
2.java.lang.Object
java.util.regex.Matcher
An engine that performs match operations on a character sequence by interpreting a Pattern.
相关方法:
(1)matches
public boolean matches()
Attempts to match the entire region against the pattern.
If the match succeeds then more information can be obtained via the start, end, and group methods.
Returns:
true if, and only if, the entire region sequence matches this matcher's pattern
发表评论
-
final变量
2012-07-07 10:47 843final变量必须被初始化,不管是静态的还是非静态的,初始化的 ... -
第10章内部类
2012-07-05 00:40 810一、概述 package com.test; ... -
第12章 异常处理
2012-05-22 13:03 8251.Throwable类是所有异常类的基类,Throwable ... -
线程类中的同步关键字
2012-03-19 17:28 1207public class Constants { publ ... -
第20章注解
2012-03-03 11:32 8411.注解也被称为元数据 ... -
使用Executor
2012-02-29 17:24 1371相关代码: public class CachedThread ... -
死锁的问题
2012-02-29 15:35 9051.某个任务在等待另个任务,而后者有等待别的任务,这样一直下去 ... -
生产者消费者
2012-02-29 11:39 5051. class Meal { private final ... -
第21章 并发
2012-02-22 17:39 9371.基本上所有的并非模式在解决线程冲突问题时,都是采用序列化访 ... -
对象序列化
2012-02-06 17:49 1182当你创建对象时,只要你需要,它就会一直存在,但是在程序终止时, ... -
JAVA IO结构图
2012-02-05 16:00 1251图1 http://blog.sina.com.cn/s/b ... -
第18章IO系统
2012-02-03 18:11 9811. File类既能代表一个文件,也能代表某个目录下文件和子 ... -
第17章容器深入研究
2012-02-02 17:47 9191.List接口的相关方法 1)toArray Object ... -
第11章持有对象
2012-02-01 17:52 10241.向上转型也可作用于泛型(当指定了某个确切类型作为类型参数时 ... -
随机数
2012-01-31 10:23 1233java.util.Random类 1.public Ran ... -
第15章泛型
2012-01-30 17:25 19771.泛型,就是“适用于 ... -
第16章数组
2012-01-29 17:56 9091.数组和其他容器相比是一种效率最高的存储和随机访问对象的方式 ... -
第14章类型信息
2012-01-16 15:27 8111.类是程序的一部分, ... -
Interrupt
2010-11-01 20:36 958interrupt()只是改变中断状态而已 inte ... -
volatile
2010-10-09 09:08 1068以前就看到过Volatile关键字, 只知道跟多线程同步有关, ...
相关推荐
Python从入门到精通 第13章 文件及目录操作.ppt Python从入门到精通 第14章 操作数据库.ppt Python从入门到精通 第15章 GUI界面编程.pptx Python从入门到精通 第16章 Pygame游戏编程.pptx Python从入门到精通 第17章...
第4章 熟练操作字符串.ppt 第5章 程序的控制结构.ppt 第6章 函数.ppt 第7章 对象与类.ppt 第8章 程序调试和异常处理.ppt 第9章 模块与类库.ppt 第10章 日期和时间.ppt 第11章 迭代器、生成器与装饰器.ppt 第12章 ...
1.4 字符串操作 18 1.4.1 连接字符串 18 1.4.2 从字符串中提取子串 20 1.4.3 比较字符串 20 1.4.4 字符串转换 21 1.4.5 格式化字符串 21 1.5 字符串用法 22 1.5.1 构建字符串 22 1.5.2 分析字符串 24 1.6 国际化 25 ...
Python从入门到精通 第13章 文件及目录操作.ppt Python从入门到精通 第14章 操作数据库.ppt Python从入门到精通 第15章 GUI界面编程.pptx Python从入门到精通 第16章 Pygame游戏编程.pptx Python从入门到精通 第17章...
第十三章 时间和日期 79 第十四章 系统调用 84 第十五章 可移植性 92 第十六章 ANSI/ISO标准 96 第十七章 用户界面——屏幕和键盘 99 第十八章 程序的编写和编译 108 第十九章 编程风格和标准 114 第二十章 杂项...
第三章其他操作符与表达式、流程控制.ppt 第四章字符串处理.ppt 第五章代码重用.ppt 第六章面向对象I.ppt ...第八章文件操作函数库与面向对象编程.ppt ...第十三章面向对象的数据库操作.ppt 第十四章错误和异常.ppt
第9章 字符串和正则表达式 第10章 处理文件和操作系统 第11章 PEAR 第12章 日期和时间 第13章 PHP 5.1 第14章 表单和导航提示 第15章 处理文件上传 第16章 网络 第17章 PHP和LDAP 第18章 会话处理器 第19章 用Smarty...
第一章 java概述 第二章 数据类型 第三章 运算符和表达式 ...第十三章 用Swing创建用户界面 第十四章 Java小应用程序\(Applet和JApplet\) 第十五章 集合框架 第十六章 网络通信 第十七章 JSP技术 以及它们的例子
Python从入门到精通 第13章 文件及目录操作.ppt Python从入门到精通 第14章 操作数据库.ppt Python从入门到精通 第15章 GUI界面编程.pptx Python从入门到精通 第16章 Pygame游戏编程.pptx Python从入门到精通 第17章...
01第一章 Python基础,网盘文件,永久连接 02第二章 Python流程控制 03第三章 Python字符串 ...13第十三章Web框架之 Django三 14第十四章CMDB系统开发 15第十五章CMDB系统开发二 16第十六章CMDB系统开发三
第4章 熟练操作字符串.ppt 第5章 程序的控制结构.ppt 第6章 函数.ppt 第7章 对象与类.ppt 第8章 程序调试和异常处理.ppt 第9章 模块与类库.ppt 第10章 日期和时间.ppt 第11章 迭代器、生成器与装饰器.ppt 第12章 ...
Python从入门到精通 第13章 文件及目录操作.ppt Python从入门到精通 第14章 操作数据库.ppt Python从入门到精通 第15章 GUI界面编程.pptx Python从入门到精通 第16章 Pygame游戏编程.pptx Python从入门到精通 第17章...
Python从入门到精通 第13章 文件及目录操作.ppt Python从入门到精通 第14章 操作数据库.ppt Python从入门到精通 第15章 GUI界面编程.pptx Python从入门到精通 第16章 Pygame游戏编程.pptx Python从入门到精通 第17章...
第7章 字符串、数组和集合 第8章 委托和事件 第9章 内存和资源管理 第10章 数据流和文件 第11章 Internet访问 第12章 序列化 第13章 远程处理和XML Web Service 第14章 线程和异步编程 第15章 托管代码与非托管...
第7章 字符串、数组和集合 第8章 委托和事件 第9章 内存和资源管理 第10章 数据流和文件 第11章 Internet访问 第12章 序列化 第13章 远程处理和XML Web Service 第14章 线程和异步编程 第15章 托管代码与非托管...
第13章(/14/) Matches集合与Macth对象.asp 指定忽略大小写.asp 获取Matches集合.asp 进行正则匹配替换.asp 进行正则匹配检验.asp 第14章(/14/) 浮点型数据...
第4章 熟练操作字符串.ppt 第5章 程序的控制结构.ppt 第6章 函数.ppt 第7章 对象与类.ppt 第8章 程序调试和异常处理.ppt 第9章 模块与类库.ppt 第10章 日期和时间.ppt 第11章 迭代器、生成器与装饰器.ppt 第12章 ...
第13章 文件及目录操作 第14章 操作数据库 第15章 GUI界面编程 第16章 Pygame游戏编程 第17章 网络爬虫开发 第18章 使用进程和线程 第19章 网络程 第20章Web编程 第21章 Flask框架 适用人群:Python初学者 使用场景...
第4章 熟练操作字符串.ppt 第5章 程序的控制结构.ppt 第6章 函数.ppt 第7章 对象与类.ppt 第8章 程序调试和异常处理.ppt 第9章 模块与类库.ppt 第10章 日期和时间.ppt 第11章 迭代器、生成器与装饰器.ppt 第12章 ...
第6章 字符串、数组、簇和矩阵 第7章 图形化显示数据——图表和图形 第8章 Express VI——快速搭建专业测试系统 第9章 文件IO 第10章 子VI 第11章 属性节点和方法节点 第12章 人机界面交互设计 第13章 数学分析与...