* Class description: String tool Class Name: String_U, tool class string_u

Source: Internet
Author: User
Tags return tag

* Class description: String tool Class Name: String_U, tool class string_u

/*************************************** * *** Class description: string tool Class Name: string_U *************************************** * ***/public class String_U {private String_U () {}/*** check whether the mobile phone number is valid ** @ param phoneNum * @ return boolean true indicates that the mobile phone number is valid, and false indicates that the mobile phone number is invalid */public static boolean isMobileNum (String phoneNum) {if (TextUtils. isEmpty (phoneNum) return false; Pattern p = Pattern. compile ("(\ + 86 | 86 )? (13 [0-9] \ d {8}) | (15 [0-9] \ d {8}) | (170 \ d {8 }) | (18 [0-9] \ d {8}) "); Matcher m = p. matcher (phoneNum); if (m. matches () {return true;} else {return false;} public static boolean isEmail (String email) {boolean tag = true; // final String pattern1 = // "^ [0-9a-z] [a-z0-9 \\. _-] {1,} @ [a-z0-9-] {1,} [a-z0-9] \. [a-z \.] {1,} [a-z] $ "; final String pattern1 =" ^ [\ w] [a-z0-9 \\. _-] {1,} @ [a-z0-9-] {1,} [a-z0-9] \. [a-z \.] {1 ,}[ A-z] $ "; final Pattern pattern = Pattern. compile (pattern1); final Matcher mat = pattern. matcher (email); if (! Mat. find () {tag = false;} return tag ;} /*** set the font color of the specified position ** @ param color * hexadecimal color value * @ param text * @ param start * set the start position of the color * @ param end * set the end position of the color * @ return mSpannableString */public static SpannableString getSpecialSpannableString (String color, string text, int start, int end) {SpannableString mSpannableString = new SpannableString (text); // set the foreground color of mSpannableString. setSpan (new ForegroundColorSp An (Color. parseColor (color), start, end, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); return mSpannableString;}/*** verify that the password complies with the rules ** @ param password * @ return boolean true indicates that the password is valid, false indicates that the password is invalid */public static boolean checkPassword (String password) {if (password = null | password. length ()> 16 | password. length () <6) {return false;} Pattern pattern = Pattern. compile ("^ [a-zA-Z0-9] {6, 16} $"); Matcher invalid = patte Rn. matcher (password); return invalid. matches ();}/*** String Security comparison ** @ param lStr * String on the left * @ param rStr * String on the right * @ return boolean true the two strings share the same content, false two strings are inconsistent */public static boolean equal (String lStr, String rStr) {if (lStr = null) {return lStr = rStr;} return lStr. equals (rStr);}/*** calculate the percentage ** @ param y * @ param z * @ return */public static String myPercent (int y, int z) {String percent = null ;/ /The value of percentage accepted is float en y = y; float baiz = z; float fen = y/baiz; // NumberFormat nf = NumberFormat. getPercentInstance (); the comment is also a method // nf. setMinimumFractionDigits (2); number of decimal places after which DecimalFormat df1 = new DecimalFormat ("##% ");//##. 00% // percentage format. for less than two digits, use 0 to complete the format. // baifenbi = nf. format (fen); percent = df1.format (fen); return percent;}/*** splits the specified text, and append the solid circle or serial number ** @ param text * before the specified text to specify the text * @ param separator * @ Param circle * entry symbol (for example, "•") * @ param isAppendNum * whether to add the serial number * @ return */public static String separatorString (String text, String separator, String circle, boolean isAppendNum) {if (TextUtils. isEmpty (text) {return "";} StringBuffer strBuffer = new StringBuffer (); if (TextUtils. isEmpty (separator) {// No separator strBuffer. append (circle); strBuffer. append (text);} else {String [] strArray = text. split (separator ); For (int I = 0; I <strArray. length; I ++) {if (! TextUtils. isEmpty (circle) {// Add the entry symbol strBuffer. append (circle);} if (isAppendNum) {// Add the serial number strBuffer. append (I-1) + ",");} strBuffer. append (strArray [I]); if (I! = StrArray. length-1) {// Add line feed strBuffer. append ("\ n") ;}} return strBuffer. toString ();}}


Defines a string class, which includes two data members: (added below)

# Include <iostream>
# Include <memory. h>

Using namespace std;

Class c_string {
Typedef unsigned size_t;
Friend c_string & contact (c_string & str1, const c_string & str2 );
Public:
C_string (size_t length = 20): m_nLength (length ){
M_pStr = new char [m_nLength + 1];
Memset (m_pStr, 0, m_nLength + 1 );
}

C_string (const char * pStr ){
M_nLength = strlen (pStr );
M_pStr = new char [m_nLength + 1];
Memcpy (m_pStr, pStr, m_nLength + 1 );
}

C_string (const c_string & cstr ){
M_nLength = cstr. length ();
M_pStr = new char [m_nLength + 1];
Memcpy (m_pStr, cstr. c_str (), m_nLength + 1 );
}

C_string & operator = (const c_string & cstr ){
If (this = & cstr ){
Return * this;
}
Delete m_pStr;
M_nLength = cstr. length ();
M_pStr = new char [m_nLength + 1];
Memcpy (m_pStr, cstr. c_str (), m_nLength + 1 );
Return * this;
}

Void print () const {
Cout <m_pStr <endl;
}

~ C_string (){
If (m_pStr! = NULL ){
Delete m_pStr;
}
}

Size_t length () const {
Return m_nLength;
}

Const char * c_str () const {
Return m_pStr;
}
Private:
Char * m_pStr;
Size_t m_nLength;
};

C_string & contact (c_string & str1, const c_string & str2 ){
If (str2.length () = 0 ){
Return str1;
}
Char * tem = new char [str1.m _ nLength + st ...... remaining full text>

2) simulate the string class in the C ++ library and declare a string of the C ++ string class. The basic framework of this class is as follows:

# Include <iostream>
# Include <string>
Using namespace std;

Class Mystring
{
Public:
// Interface functions here
Mystring (); // No parameter constructor,
Mystring (char * c); // use the C string as the initial value of s
Mystring (int num, char c); // generate a string containing num c characters
Mystring (Mystring & s); // copy the constructor
~ Mystring (); // destructor

Void assign (char * c); // assign a new value
Int compare (Mystring & s); // compare string, less than-1 returned, equal to 0, greater than 1
Int length (); // number of returned characters
Char at (int index); // returns a single character
String assign (Mystring & s); // assign a new value
Void clear (); // Delete All characters
Void show (); // print the string content

Private:
Char * m_data; // record the string's first address
Int m_length; // specifies the string length.

};

Mystring: Mystring () // No parameter Constructor
{
M_data = new char [10];
Strcpy (m_data, "Hello ");
M_length = strlen ("Hello ");
}

Mystring: Mystring (char * c) // use the C string as the initial value of s.
{
M_data = new char [strlen (c) + 1];
Strcpy (m_data, c );
M_length = strlen (m_data );
}

Mystring: Mystring (int num, char c) // generate a string containing num c characters
{
M_data = new char [num + 1];
For (int I = 0; I <num; I ++)
{
M_data [I] = c;
}
M_data [I] = '\ 0 ';
M_length = strlen (m_data );
}

Mystring: Mystring (Mystring & s) // copy the constructor
{
M_data = new char [strlen (s. m_data) + 1];
Strcpy (m_data, s. m_data );
M_length = strlen (s. m_data );
}

Mystring ::~ Mystring () ...... the remaining full text>

Related Article

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.