Java string handling of special symbols

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.