Package cn.com.nike.util;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
/**
* Operation on string
* @author Soya.song
*2017.3.21
*/
public class Utilstring {
Private static final Logger log = Loggerfactory.getlogger (Utilstring.class);
/**
* Remove spaces, underline, turn all lowercase
* Scenario: Convert Excel column names to object property names
* Object Attribute name Mission rule: Column name remove space, underline, all lowercase is the property name, the object does not take the hump naming rules, because the dynamic value, assignment, solid need to follow the rule
* @param str
* @return
*/
public static string newstring (String str) {
Return Str.tolowercase (). ReplaceAll ("\\s*", ""). ReplaceAll ("_", "");//all lowercase, remove spaces, remove underline
}
/**
* Turn all lowercase
* @param str
* @return
*/
public static string Alllowercase (String str) {
return Str.tolowercase ();
}
/**
* Remove spaces
* @param str
* @return
*/
public static string Removeblankspace (String str) {
Return Str.replaceall ("\\s*", "" ");
}
/**
* Remove the Underline
* @param str
* @return
*/
public static string Removeunderline (String str) {
Return Str.replaceall ("_", "" ");
}
/**
* Slash slash removal
* @param str
* @return
*/
public static string Removeslash (String str) {
Return Str.replaceall ("/", "");
}
/**
* Remove all spaces, all special symbols, first letter lowercase, this method will also remove the numbers
* Application Scenario: Change Excel Column name to Java object property name
* 1 Remove/_ () all spaces
* 2 First Letter Lowercase
* @param str
* @return
*/
public static string getnewstring (String str) {
Log.info ("utilstring.getnewstring." Before converting to .... "+str);
String Newstr=tolowercasefirstone (Str.trim (). ReplaceAll ("_", ""). ReplaceAll ("/", ""). ReplaceAll ("\ \ (|\\)", ""). ReplaceAll ("\\s*", ""));
Log.info ("utilstring.getnewstring. After conversion for ..... "+newstr);
return newstr;
}
First letter to lowercase
public static string Tolowercasefirstone (string s) {
if (Character.islowercase (S.charat (0)))
return s;
Else
Return (new StringBuilder ()). Append (Character.tolowercase (S.charat (0)). Append (s.substring (1)). ToString ();
}
Capitalize first letter
public string Touppercasefirstone (string s) {
if (Character.isuppercase (S.charat (0)))
return s;
Else
Return (new StringBuilder ()). Append (Character.touppercase (S.charat (0)). Append (s.substring (1)). ToString ();
}
/**
* Remove all spaces, all special symbols, first letter lowercase
* This method removes all special symbols, including numbers
* @param str
* @return
*/
public static string Getnewstringnonum (String str) {
Log.info ("utilstring.getnewstring." Before converting to .... "+str);
char[] chars = Str.tochararray ();
StringBuffer buffer=new StringBuffer ();
for (int i = 0; i < chars.length; i + +) {
if ((Chars[i] >= 19968 && chars[i] <= 40869) | | (Chars[i] >= && chars[i] <= 122) | | (Chars[i] >= && chars[i] <= 90)) {
Buffer.append (Chars[i]);
if (Chars[0] >= ' A ' && chars[0] <= ' Z ') {//When a letter is converted to lowercase
Buffer.append (Chars[i]);
}
}
}
System.out.print (Tolowercasefirstone (buffer.tostring ()));
Log.info ("utilstring.getnewstring. After conversion for ..... "+tolowercasefirstone (Buffer.tostring ()));
Return Tolowercasefirstone (Buffer.tostring ());
}
}
Java string handling of special symbols