String Tool Class (method for specifying the length of the string and whether to determine if it is empty)

Source: Internet
Author: User

Package com.sec.util;

/**
* String Tool class
* @author Administrator
*
*/
public class Stringutil {

/**
* Methods for filtering <,>,\n strings
* @param input
* @return
*/
public static string filterhtml (string input) {
if (input = = NULL | | input.length () = = 0) {
return input;
}
input = Input.replaceall ("&", "&amp;")
. ReplaceAll ("<", "LT;")
. ReplaceAll (">", "&gt;")
. ReplaceAll ("", "&nbsp;")
. ReplaceAll (",", "& #39;")
. ReplaceAll ("\" "," &quot; ")
. ReplaceAll ("\ n", "<br>");
return input;
}

/**
* Verify that the string is empty (null or empty string)
* @param str to validate the string
* @return validation result (Null string returns True, otherwise false)
*/
public static Boolean isnullorempty (String str) {
if (str = = NULL | | "". Equals (Str.trim ())) {
return true;
}
return false;
}

/**
* Omit the string part character by the specified length
* @param str string
* @param len preserves the length of the string
* @return The omitted string
*/
public static string Omitstring (String Str,int len) {
StringBuffer sb=new StringBuffer ();
if (ByteLength (str) >len) {
int count=0;
for (int i = 0; i < str.length (); i++) {
Char Temp=str.charat (i);
if (integer.tohexstring (temp). Length () ==4) {
count+=2;
}else {
count++;
}
if (count<len-3) {
Sb.append (temp);
}
if (count==len-3) {
Sb.append (temp);
Break
}
if (count>len-3) {
Sb.append ("");
Break
}
}
Sb.append ("...");
}else {
Sb.append (str);
}
return sb.tostring ();
}

public static int bytelength (String str) {
int count=0;
for (int i = 0; i < str.length (); i++) {
if (integer.tohexstring (Str.charat (i)). Length () ==4) {
count+=2;
}else {
count++;
}
}
return count;
}
}

String Tool Class (method for specifying the length of the string and whether to determine if it is empty)

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.