目录
字符串相关类
String(不可变对象,只读)
StringBuffer(字符串加减,同步,性能好)
StringBuilder(字符串加减,不同步,性能更好)
StringBuffer存储空间问题(StringBuilder同理)
正则表达式的应用
字符串的查找
字符串的匹配
字符串的替换1
字符串的替换2
字符串的替换3
判断字符串是否为邮箱
字符串相关类
速度:String>StringBuffer>StringBuilder
String(不可变对象,只读)
public class StringTest {
public static void main(String[] args) {
String str = "123;456;789;0";
//返回第0个元素
System.out.println(str.charAt(0));
//返回第一个;的位置
System.out.println(str.indexOf(";"));
//链接一个新字符串并返回,str不变
System.out.println(str.concat(";000"));
//判断str是否包含000
System.out.println(str.contains("000"));
//判断str是否以000结尾
System.out.println(str.endsWith("000"));
//判断str是否等于000
System.out.println(str.equals("000"));
//判断在忽略大小写情况下是否等于000
System.out.println(str.equalsIgnoreCase("000"));
//返回str的长度
System.out.println(str.length());
//返回去掉前后空格后的字符串,str不变
System.out.println(str.trim());
//将str按照;分割存入数组
String[] a = str.split(";");
for (String i : a) {
System.out.println(i);
}
//截取并返回str的第2+1到5+1的字符串,str不变
System.out.println(str.substring(2, 5));
String s1 = "12345?67890";
//返回将s1中所有的?替换为a的字符串,s1不变
System.out.println(s1.replace("?", "a"));
//replaceAll中第一个参数是正则表达式
//返回将s1中0-9的字符替换为a的字符串,s1不变
System.out.println(s1.replaceAll("[\d]", "a"));
}
}
StringBuffer(字符串加减,同步,性能好)
StringBuilder(字符串加减,不同步,性能更好)
StringBuilder(字符串加减,不同步,性能更好)
StirngBuffer和StringBuilder方法的使用基本一致。
public class StringBufferTest {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("ABCDEFGHIJKLMNOPQRSTUVW");
//append的参数类型可以为int long float double string char 数组等
//在sb字符串末尾中添加XYZ
sb.append("XYZ");
System.out.println(sb);
//返回索引为10的字符
System.out.println(sb.charAt(10));
//删除索引为10的字符
sb.deleteCharAt(10);
System.out.println(sb);
//删除索引[2, 5)的字符串
sb.delete(2, 5);
System.out.println(sb);
//返回子字符串第一次出现的索引位置
System.out.println(sb.indexOf("OP"));
//从索引4开始向后查找第一次出现OP的位置索引
System.out.println(sb.indexOf("OP", 4));
//返回字符串中最后一次出现OP的索引位置
System.out.println(sb.lastIndexOf("OP"));
//从索引4开始向前查找最后一次出现OP的位置
System.out.println(sb.lastIndexOf("OP", 4));
//insert第二个参数可以为int long float double string char 数组等
//将XYZ插入sb索引为3的位置
sb.insert(3, "XYZ");
System.out.println(sb);
//将索引位置为0-2的字符替换为字符串aa
sb.replace(0, 2, "aa");
System.out.println(sb);
//返回sb的实际长度
System.out.println(sb.length());
//返回sb的存储空间大小
System.out.println(sb.capacity());
//返回一个新的字符串,sb不变
System.out.println(sb.substring(2, 5));
}
}
StringBuffer存储空间问题(StringBuilder同理)
StringBuffer和StringBuilder他们的存储空间是对它们的性能是有一定的影响的。不断的进行append后,这个存储空间会不断的膨胀。所以在程序中若能预估存储空间的大小,则可以使用构造函数指定存储空间的大小。
public class StringBufferSpace {
public static void main(String[] args) {
//Stringbuffer的初始大小为(16+初始字符串长度),即capacity=16+初始字符串长度
//length 实际长度 capacity 存储空间大熊啊
StringBuffer sb1 = new StringBuffer();
System.out.println("实际长度:" + sb1.length());
System.out.println("存储空间大小:" + sb1.capacity());
System.out.println("===============分割线===============");
StringBuffer sb2 = new StringBuffer("123");
sb2.append("456");
System.out.println("实际长度:" + sb2.length());
System.out.println("存储空间大小:" + sb2.capacity());
System.out.println("===============分割线===============");
sb2.append("7890123456789");
System.out.println("实际长度:" + sb2.length());
System.out.println("存储空间大小:" + sb2.capacity());
System.out.println("===============分割线===============");
//当存储空间存满时,StringBuffer将扩充至原存储空间大小加上新增字符串大小后再翻一倍
//即新存储空间大小=(原存储空间大小+新增字符串大小)*2
sb2.append("0");
System.out.println("实际长度:" + sb2.length());
System.out.println("存储空间大小:" + sb2.capacity());
System.out.println("===============分割线===============");
//当存储空间未满,但是新增的字符串大于剩余空间的大小,则存储空间扩充至与实际长度一样大小
//即新存储空间大小=实际字符串长度
//增加70个字符
sb2.append("1234567890123456789012345678901234567890123456789012345678901234567890");
System.out.println("实际长度:" + sb2.length());
System.out.println("存储空间大小:" + sb2.capacity());
System.out.println("===============分割线===============");
sb2.append("0");
System.out.println("实际长度:" + sb2.length());
System.out.println("存储空间大小:" + sb2.capacity());
//在添加字符串较短时,而扩充的空间较大,可以用trimToSize将存储空间大小等于字符串长度
//调用trimToSize后存储空间大小=字符串长度
sb2.trimToSize();
System.out.println("============存储空间清理后============");
System.out.println("实际长度:" + sb2.length());
System.out.println("存储空间大小:" + sb2.capacity());
}
}
正则表达式的应用
字符串的查找
public class MatcherDemo {
//\b表示边界
private static final String REGEX = "\bdog\b";
private static final String INPUT = "dog dog dog doggie dogg";
//检查字符串里面有多少个dog
public static void main(String[] args) {
//编译一个正则表达式
Pattern pattern = Pattern.compile(REGEX);
//匹配字符串
Matcher matcher = pattern.matcher(INPUT);
int count = 0;
while (matcher.find()) {
count++;
System.out.println("Match number: " + count);
System.out.println("start(): " + matcher.start());
System.out.println("end(): " + matcher.end());
}
}
}
字符串的匹配
public class MatchesLooking {
private static final String REGEX = "foo";
private static final String INPUT = "fooooooooooo";
private static Pattern pattern;
private static Matcher matcher;
public static void main(String[] args) {
pattern = Pattern.compile(REGEX);
matcher = pattern.matcher(INPUT);
System.out.println("Current REGEX is:" + REGEX);
System.out.println("CUrrent INPUT is:" + INPUT);
//部分匹配
System.out.println("lookingAt():" + matcher.lookingAt());
//完全匹配
System.out.println("matches():" + matcher.matches());
}
}
字符串的替换1
public class RegexDemo {
//*表示限定前面的a可以有0或者多个
private static String REGEX = "a*b";
private static String INPUT = "aabfooaabfooabfoobcdd";
private static String REPLACE = "-";
public static void main(String[] args) {
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(INPUT);
StringBuffer stringBuffer = new StringBuffer();
//全部替换
while(matcher.find()){
matcher.appendReplacement(stringBuffer, REPLACE);
}
//将最后的尾巴字符串附加上
matcher.appendTail(stringBuffer);
System.out.println(stringBuffer.toString());
}
}
字符串的替换2
public class ReplaceDemo {
private static String REGEX = "dog";
private static String INPUT = "The dog says meow. All dog meow";
private static String REPLACE = "cat";
public static void main(String[] args) {
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(INPUT);
INPUT = matcher.replaceAll(REPLACE);
System.out.println(INPUT);
}
}
字符串的替换3
public class ReplaceDemo2 {
private static String REGEX = "a*b";
private static String INPUT = "aabfooaabfooabfoob";
private static String REPLACE = "-";
public static void main(String[] args) {
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(INPUT);
INPUT = matcher.replaceAll(REPLACE);
System.out.println(INPUT);
}
}
判断字符串是否为邮箱
public class RegexTest {
public static void main(String[] args) {
regularexpression();
}
public static void regularexpression(){
String REGEX_EMAIL = "^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+(([.\-])[A-Za-z0-9]+)*\.[A-Za-z0-9]+$";
Pattern pattern = Pattern.compile(REGEX_EMAIL);
String[] emails = new String[]{
"123^@qq.com",
"name_321@163.com",
"321@163.com",
"+whatever*72@gmail.com"
};
for (String email:emails){
Matcher matcher = pattern.matcher(email);
if (matcher.matches()){
System.out.println(email + "---is");
}
else{
System.out.println(email + "---not");
}
}
}
}
public class MatcherDemo {
//\b表示边界
private static final String REGEX = "\bdog\b";
private static final String INPUT = "dog dog dog doggie dogg";
//检查字符串里面有多少个dog
public static void main(String[] args) {
//编译一个正则表达式
Pattern pattern = Pattern.compile(REGEX);
//匹配字符串
Matcher matcher = pattern.matcher(INPUT);
int count = 0;
while (matcher.find()) {
count++;
System.out.println("Match number: " + count);
System.out.println("start(): " + matcher.start());
System.out.println("end(): " + matcher.end());
}
}
}
字符串的匹配
public class MatchesLooking {
private static final String REGEX = "foo";
private static final String INPUT = "fooooooooooo";
private static Pattern pattern;
private static Matcher matcher;
public static void main(String[] args) {
pattern = Pattern.compile(REGEX);
matcher = pattern.matcher(INPUT);
System.out.println("Current REGEX is:" + REGEX);
System.out.println("CUrrent INPUT is:" + INPUT);
//部分匹配
System.out.println("lookingAt():" + matcher.lookingAt());
//完全匹配
System.out.println("matches():" + matcher.matches());
}
}
字符串的替换1
public class RegexDemo {
//*表示限定前面的a可以有0或者多个
private static String REGEX = "a*b";
private static String INPUT = "aabfooaabfooabfoobcdd";
private static String REPLACE = "-";
public static void main(String[] args) {
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(INPUT);
StringBuffer stringBuffer = new StringBuffer();
//全部替换
while(matcher.find()){
matcher.appendReplacement(stringBuffer, REPLACE);
}
//将最后的尾巴字符串附加上
matcher.appendTail(stringBuffer);
System.out.println(stringBuffer.toString());
}
}
字符串的替换2
public class ReplaceDemo {
private static String REGEX = "dog";
private static String INPUT = "The dog says meow. All dog meow";
private static String REPLACE = "cat";
public static void main(String[] args) {
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(INPUT);
INPUT = matcher.replaceAll(REPLACE);
System.out.println(INPUT);
}
}
字符串的替换3
public class ReplaceDemo2 {
private static String REGEX = "a*b";
private static String INPUT = "aabfooaabfooabfoob";
private static String REPLACE = "-";
public static void main(String[] args) {
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(INPUT);
INPUT = matcher.replaceAll(REPLACE);
System.out.println(INPUT);
}
}
判断字符串是否为邮箱
public class RegexTest {
public static void main(String[] args) {
regularexpression();
}
public static void regularexpression(){
String REGEX_EMAIL = "^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+(([.\-])[A-Za-z0-9]+)*\.[A-Za-z0-9]+$";
Pattern pattern = Pattern.compile(REGEX_EMAIL);
String[] emails = new String[]{
"123^@qq.com",
"name_321@163.com",
"321@163.com",
"+whatever*72@gmail.com"
};
for (String email:emails){
Matcher matcher = pattern.matcher(email);
if (matcher.matches()){
System.out.println(email + "---is");
}
else{
System.out.println(email + "---not");
}
}
}
}
public class RegexDemo {
//*表示限定前面的a可以有0或者多个
private static String REGEX = "a*b";
private static String INPUT = "aabfooaabfooabfoobcdd";
private static String REPLACE = "-";
public static void main(String[] args) {
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(INPUT);
StringBuffer stringBuffer = new StringBuffer();
//全部替换
while(matcher.find()){
matcher.appendReplacement(stringBuffer, REPLACE);
}
//将最后的尾巴字符串附加上
matcher.appendTail(stringBuffer);
System.out.println(stringBuffer.toString());
}
}
字符串的替换2
public class ReplaceDemo {
private static String REGEX = "dog";
private static String INPUT = "The dog says meow. All dog meow";
private static String REPLACE = "cat";
public static void main(String[] args) {
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(INPUT);
INPUT = matcher.replaceAll(REPLACE);
System.out.println(INPUT);
}
}
字符串的替换3
public class ReplaceDemo2 {
private static String REGEX = "a*b";
private static String INPUT = "aabfooaabfooabfoob";
private static String REPLACE = "-";
public static void main(String[] args) {
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(INPUT);
INPUT = matcher.replaceAll(REPLACE);
System.out.println(INPUT);
}
}
判断字符串是否为邮箱
public class RegexTest {
public static void main(String[] args) {
regularexpression();
}
public static void regularexpression(){
String REGEX_EMAIL = "^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+(([.\-])[A-Za-z0-9]+)*\.[A-Za-z0-9]+$";
Pattern pattern = Pattern.compile(REGEX_EMAIL);
String[] emails = new String[]{
"123^@qq.com",
"name_321@163.com",
"321@163.com",
"+whatever*72@gmail.com"
};
for (String email:emails){
Matcher matcher = pattern.matcher(email);
if (matcher.matches()){
System.out.println(email + "---is");
}
else{
System.out.println(email + "---not");
}
}
}
}
public class ReplaceDemo2 {
private static String REGEX = "a*b";
private static String INPUT = "aabfooaabfooabfoob";
private static String REPLACE = "-";
public static void main(String[] args) {
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(INPUT);
INPUT = matcher.replaceAll(REPLACE);
System.out.println(INPUT);
}
}



