String splitting or concatenation in Android

In android development, we often need to do some processing on some strings to achieve the effect we want. Here are several methods:

Pattern pattern = Pattern.compile("(http://|https://){1}[\\w\\.\\-/:]+");
Matcher matcher = pattern.matcher("dsdsds<http://dsds//gfgffdfd>fdf");
StringBuffer buffer = new StringBuffer();
while(matcher.find()){             
buffer.append(matcher.group());       
buffer.append("\r\n");             
System.out.println(buffer.toString());
Log.i(TAG, "截取得到字符串是:"+buffer.toString());

}

 

 

String url="http://192.144.136.44:8080/cms/apizsa.ptg?authkey=141414&do=get_category";
StringBuilder sb = new StringBuilder();
sb.append(url.split(":")[0]).append(":").append(url.split(":")[1]).append(":");
sb.toString();
Log.i(TAG, "截取得到的字符是:"+sb.toString());

 

 

 

  Only allow letters and numbers      
   String regEx = "[^a-zA-Z0-9]";                    
  Clear all special characters 
String regEx="[`~!@#$%^&*()+=|{} ':;',\\[\\].<>/?~!@#¥%...& amp;*()——+|{}[]';:""'.,,?]" ; 
Pattern p = Pattern.compile(regEx);    
Matcher m = p.matcher(str);    
return m.replaceAll("").trim();    
}    

 

◆For example, when the string contains validation

//Find strings starting with Java and ending arbitrarily
Pattern pattern = Pattern.compile("^Java.*");
Matcher matcher = pattern.matcher("Java is not a person");
boolean b= matcher.matches();
//When the condition is met, it will return true, otherwise it will return false
System.out.println(b);


◆When splitting strings with multiple conditions
Pattern pattern = Pattern.compile("[, |]+");
String[] strs = pattern.split("Java Hello World Java,Hello,,World|SUN");
for ( int i=0;i<strs.length;i++) {
System.out.println(strs[i]);
}

 

◆Text replacement (the first occurrence of the character)
Pattern pattern = Pattern.compile("regular expression");
Matcher matcher = pattern.matcher("regular expression Hello World, regular expression Hello World");
//Replace the first Regular data
System.out.println(matcher.replaceFirst("Java"));

◆Text replacement (all)
Pattern pattern = Pattern.compile("regular expression");
Matcher matcher = pattern.matcher("regular expression Hello World, regular expression Hello World");
//Replace the first matching regular expression data
System.out.println(matcher.replaceAll("Java"));


◆Text replacement (replacement characters)
Pattern pattern = Pattern.compile("regular expression");
Matcher matcher = pattern.matcher("regular expression Hello World, regular expression Hello World ");
StringBuffer sbr = new StringBuffer() ;
while (matcher.find()) {
matcher.appendReplacement(sbr, "Java");
}
matcher.appendTail(sbr);
System.out.println(sbr.toString());

 

◆Verify whether it is an email address

String str="[email protected]";
Pattern pattern = Pattern.compile("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+",Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
System.out.println(matcher.matches());

 

◆去除html标记
Pattern pattern = Pattern.compile("<.+?>", Pattern.DOTALL);
Matcher matcher = pattern.matcher("<a href=\"index.html\">主页</a>");
String string = matcher.replaceAll("");
System.out.println(string);

 

◆Find the corresponding conditional string in html
Pattern pattern = Pattern.compile("href=\"(.+?)\"");
Matcher matcher = pattern.matcher("<a href=\"index.html\"> Home</a>");
if(matcher.find())
System.out.println(matcher.group(1));
}

 

◆Replace the specified {} Chinese characters

String str = "Java's current development history is from {0}years-{1}years";
String[][] object={new String[]{" \\{0\\}","1995"}, new  String[]{" \\{1\\}","2007 "}};
System.out.println(replace(str,object));

public static String replace(final String sourceString,Object[] object) {
String temp=sourceString;   
for(int i=0;i<object.length;i++){
String[] result=(String[])object[i];
Pattern    pattern = Pattern.compile(result[0]);
Matcher matcher = pattern.matcher(temp);
temp=matcher.replaceAll(result[1]);
}
return temp;
}

 

 

Reference: http://www.cnblogs.com/doll-net/archive/2006/10/31/545398.html

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326676940&siteId=291194637