Java character operations and Transcoding

Source: Internet
Author: User

Import java. util .*;
Import java. Text .*;
Import java. Lang .*;
Import sun. Io .*;

Public class dealstring
{

Public long makeid (int uid)
{
// 5 Users
UID ++ = 10000;

// Time occupies 9 digits: seconds. Remove the first digit.
Java. util. Date time = new java. util. Date ();
Long second = time. gettime ()/1000;
String STR = Second + "";
STR = Str. substring (1, str. Length ());

// Group Five
STR = uid + STR + 10000;

Return long. parselong (STR );
}

/** Convert null ""*/
Public String tostring (string Str)
{
If (STR = NULL) STR = "";
If (Str. Equals ("null") STR = "";
STR = Str. Trim ();
Return STR;
}

/** Conversion encoding */
Public String togbk (string Str)
{
Try
{
If (STR = NULL)
STR = "";
Else
STR = new string (Str. getbytes ("ISO-8859-1"), "GBK ");
} Catch (exception e) {system. Out. println ("dealstring: togbk (string): Error:" + E );}
Return STR;
}

Public String tomkbyte (INT size)
{
If (size> (1024*1024) {return (float) size/(1024*1024) + ""). substring () + "MB ";}
Else if (size> 1024) {return (float) size/1024 + ""). substring () + "kb ";}
Else return size + "B ";
}

/** Utf8 */
Public String toutf8string (string SRC)
{
Byte [] B = SRC. getbytes ();
Char [] C = new char [B. Length];
For (INT I = 0; I <B. length; I ++)
{
C [I] = (char) (B [I] & 0x00ff );
}
Return new string (C );
}

Public String toascii (string Str)
{
Try
{
If (STR = NULL)
STR = "";
Else
STR = new string (Str. getbytes ("GBK"), "ISO-8859-1 ");
} Catch (exception e) {system. Out. println ("dealstring: togbk (string): Error:" + E );}
Return STR;
}
/** Split string */
Public String [] splitstr (string STR, char C)
{
STR + = C;
Int n = 0;
For (INT I = 0; I <Str. Length (); I ++)
{
If (Str. charat (I) = C) n ++;
}

String out [] = new string [N];

For (INT I = 0; I <n; I ++)
{
Int Index = Str. indexof (C );
Out [I] = Str. substring (0, index );
STR = Str. substring (index + 1, str. Length ());
}
Return out;
}

/** Obtain the system time */
Public String getdatetime ()
{
Java. Text. simpledateformat F = new java. Text. simpledateformat ("yyyy-mm-dd hh: mm: SS ");
String time = f. Format (New java. util. Date ());
Return time;
}

/** Replace string */
Public String Replace (string source, string oldstring, string newstring)
{
Stringbuffer output = new stringbuffer ();

Int lengthofsource = source. Length (); // source String Length
Int lengthofold = oldstring. Length (); // The length of the old string.
Int posstart = 0; // start position search
Int Pos; // search for the location of the old string

While (Pos = source. indexof (oldstring, posstart)> = 0 ){
Output. append (source. substring (posstart, POS ));
Output. append (newstring );
Posstart = POS + lengthofold;
}
If (posstart <lengthofsource ){
Output. append (source. substring (posstart ));
}
Return output. tostring ();
}

/** Get the difference between the days of two dates */
Public long getdaysinterval (date D1, date D2)
{
Return (d2.gettime ()-d1.gettime ()/86400000;
}

/** Format the string to a fixed length */
Public String tolengthstr (string instr, int Len)
{
Int n = instr. Length ();
For (INT I = 0; I <(LEN-N); I ++)
{
Instr = "" + instr;
}
Return instr;
}
/** Format the string to a fixed length (with spaces on the right )*/
Public String tolengthstrright (string instr, int Len)
{
Int n = instr. Length ();
For (INT I = 0; I <(LEN-N); I ++)
{
Instr = instr + "";
}
Return instr;
}
/* Convert ASCII to string */
Public String asciitochinesestring (string S)
{
Char [] orig = S. tochararray ();
Byte [] DEST = new byte [orig. Length];
For (INT I = 0; I <orig. length; I ++)
Dest [I] = (byte) (orig [I] & 0xff );
Try
{
Bytetocharconverter tochar = bytetocharconverter. getconverter ("gb2312 ");
Return new string (tochar. convertall (DEST ));
}
Catch (exception E)
{
System. Out. println ("dealstring: asciitochinesestring (string) error during running; error:" + E );
Return S;
}
}

/** Convert string to ASCII */
Public String chinesestringtoascii (string S)
{
Try
{
Chartobyteconverter tobyte = chartobyteconverter. getconverter ("gb2312 ");
Byte [] orig = tobyte. convertall (S. tochararray ());
Char [] DEST = new char [orig. Length];
For (INT I = 0; I <orig. length; I ++)
Dest [I] = (char) (orig [I] & 0xff );
Return new string (DEST );
}
Catch (exception E)
{
System. Out. println ("dealstring: chinesestringtoascii (string):" + E );
Return S;
}
}

/** Source code for converting Unicode to GB */
Public String unicodetogb (string S)
{
Stringbuffer sb = new stringbuffer ();
Boolean escape = false;
For (INT I = 0; I <S. Length (); I ++)
{
Char c = S. charat (I );
Switch (c)
{
Case '//': Escape = true; break;
Case 'U ':
Case 'U ':
If (escape)
{
Try
{
SB. append (char) integer. parseint (S. substring (I + 1, I + 5), 16 ));
Escape = false;
}
Catch (numberformatexception E)
{System. Out. println ("dealstring: unicodetogb (string) runtime error:" + E + "and throw a new illegalargumentexception exception .");
Throw new illegalargumentexception ();
}
I + = 4;
}
Else
{
SB. append (C );
}
Break;
Default: SB. append (c); break;
}
}
Return sb. tostring ();
}
/* Remove duplicates in Str */
Public String strdistinct (string Str)
{
String [] strarr = Str. Split (",");
String straim = ",";
For (INT I = 0; I <strarr. length; I ++)
{
If (strarr [I]. Equals (""))
Continue;
If (straim. indexof ("," + strarr [I] + ",") =-1)
{
Straim = straim + strarr [I] + ",";
}
}
If (! Straim. Equals (","))
Straim = straim. substring (1, straim. Length ()-1 );
Else straim = "";
Return straim;
}

/** Convert the character to ASCII */
Public int toascii (char C)
{
Int I = C;
Return I;
}

/** Get the length of characters */
Public int bytelength (string Str)
{
Return (Str. getbytes (). Length );
}

/** Obtain a substring of the specified length from the beginning */
Public String strbytecopy (string STR, int nend)
{
If (nend = 0)
Return "";
Byte [] bytestr = Str. getbytes ();
Int K = bytestr. length;
String strsub = new string (bytestr, 0, nend <k? Nend: K );
If (strsub. Length () = 0) strsub = new string (bytestr, 0, nEnd-1 );
Return strsub;
}
Public Boolean strmatch (string motherstr, string childstr)
{
Boolean matched = false;
Int mlength = motherstr. Length ();
Int clength = childstr. Length ();
Int starwith;
If (mlength> = clength ){
Starwith = mlength-clength;
For (INT I = 0; I <= starwith; I ++ ){
Matched = motherstr. startswith (childstr, I );
If (matched) break;
}
}
Return matched;
}
/*

// Convert the string to UTF-8 encoded
Public static string toutf8string (string s ){
Stringbuffer sb = new stringbuffer ();
For (INT I = 0; I <S. Length (); I ++ ){
Char c = S. charat (I );
If (C> = 0 & C <= 255 ){
SB. append (C );
}
Else {
Byte [] B;
Try {
B = character. tostring (c). getbytes ("UTF-8 ");
}
Catch (exception ex ){
System. Out. println (Ex );
B = new byte [0];
}
For (Int J = 0; j <B. length; j ++ ){
Int K = B [J];
If (k <0)
K + = 256;
SB. append ("%" + integer. tohexstring (k ).
Touppercase ());
}
}
}
Return sb. tostring ();
}
*/
Public vector simplify (string [] Str ){
Vector vect = new vector ();
For (INT I = 0; I <Str. length; I ++)
Vect. Add (STR [I]);
For (INT I = 0; I <vect. Size (); I ++ ){
String [] S1 = (string) vect. Get (I). Split ("_");
For (Int J = I + 1; j <vect. Size ();){
Boolean out = true;
String [] S2 = (string) vect. Get (j). Split ("_");
For (int K = 0; k <s1.length; k ++ ){
If (! S1 [K]. Equals (s2 [k]) &! S1 [K]. Equals ("0 ")){
Out = false;
Break;
}
}
If (out)
Vect. Remove (j );
Else
J ++;
}
}
Return vect;
}
Public vector simplify (vector vstr ){
Vector vect = new vector ();
For (INT I = 0; I <vstr. Size (); I ++)
Vect. Add (vstr. Get (I ));
For (INT I = 0; I <vect. Size (); I ++ ){
String [] S1 = (string) vect. Get (I). Split ("_");
For (Int J = I + 1; j <vect. Size ();){
Boolean out = true;
String [] S2 = (string) vect. Get (j). Split ("_");
For (int K = 0; k <s1.length; k ++ ){
If (! S1 [K]. Equals (s2 [k]) &! S1 [K]. Equals ("0 ")){
Out = false;
Break;
}
}
If (out)
Vect. Remove (j );
Else
J ++;
}
}
Return vect;
}

Public static void main (string ARGs []) throws exception
{
Dealstring MB = new dealstring ();
System. Out. println ("begin/R/n/R/N ");
System. Out. println (mb. makeid (2345 ));
System. Out. Print ("/rggggggggggggg ");
System. Out. println ("/rend ");
}
};

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.