A common Java string class

Source: Internet
Author: User

Package COM. tienway. util; </P> <p> Import Java. util. arraylist; </P> <p> public class stringutil <br/> {</P> <p> Public static string contexttype1totype2 (string type1context) <br/>{< br/> If (type1context = NULL | type1context. length () = 0) <br/>{< br/> return "; <br/>}< br/> char [] C = type1context. tochararray (); <br/> stringbuffer sb = new stringbuffer (); <br/> for (INT I = 0; I <C. length; I ++) <br />{< Br/> switch (C [I]) <br/>{< br/> case '>': <br/> Sb. append (">"); <br/> break; <br/> case '<': <br/> Sb. append ("<"); <br/> break; <br/> case '': <br/> Sb. append (""); <br/> break; <br/> case '"': <br/> Sb. append ("); <br/> break; <br/> case '&': <br/> Sb. append ("&"); <br/> break; <br/> case '/N': <br/> Sb. append ("<br>"); <br/> break; <br/> default: <br/> try <br/>{< br/> Sb. append (C [I]); <B R/>}catch (exception ex) <br/>{< br/>}< br/> return sb. tostring (); <br/>}</P> <p> Public static Boolean isletter (char c) <br/>{< br/> int K = 0x80; <br/> return C/k = 0? True: false; <br/>}</P> <p> Public static int length (string s) <br/>{< br/> char [] C = S. tochararray (); <br/> int Len = 0; <br/> for (INT I = 0; I <C. length; I ++) <br/>{< br/> Len ++; <br/> If (! Isletter (C [I]) <br/>{< br/> Len ++; <br/>}< br/> return Len; <br/>}</P> <p> Public static string replaceall (string strin, string RegEx, <br/> string replacement) <br/>{< br/> stringbuffer sb = new stringbuffer (); <br/> int rbegin = 0; <br/> int rend = strin. indexof (RegEx); <br/> int rlen = RegEx. length (); <br/> while (rend> = 0) <br/> {<br/> Sb. append (strin. substring (rbegin, rend); <br /> Sb. append (replacement); <br/> rbegin = rend + rlen; <br/> rend = strin. indexof (RegEx, rbegin); <br/>}< br/> Sb. append (strin. substring (rbegin); <br/> strin = sb. tostring (); <br/> return strin; <br/>}</P> <p> Public static string [] Split (string strin, char C) <br/>{< br/> return split (strin, C, 0 ); <br/>}</P> <p> Public static string [] Split (string strin, char C, int it) <br/>{< br/> arraylis T arraylist = new arraylist (); <br/> char [] chark = strin. tochararray (); <br/> stringbuffer sb = new stringbuffer (); <br/> for (INT I = it; I <chark. length; I ++) <br/>{< br/> If (chark [I] = C) <br/>{< br/> arraylist. add (sb. tostring (); <br/> Sb = new stringbuffer (); <br/>}else <br/>{< br/> try <br/>{< br/> Sb. append (chark [I]); <br/>} catch (exception E) <br/>{</P> <p >}< br/> if (I = Cha Rk. length-1) <br/>{< br/> arraylist. add (sb. tostring (); <br/>}</P> <p> int K = arraylist. size (); <br/> string as [] = new string [k]; <br/> return (string []) arraylist. sublist (0, K ). toarray (AS); <br/>}</P> <p> Public static string [] Split (string strin, string C) <br/>{< br/> If (strin = NULL) <br/>{< br/> return NULL; <br/>}</P> <p> arraylist = new arraylist (); <br/> Int begin = 0; <br/> int end = 0; <br/> while (begin = strin. indexof (C, end ))! =-1) <br/>{< br/> string S2 = strin. substring (end, begin); </P> <p >{< br/> arraylist. add (strin. substring (end, begin); <br/>}< br/> end = begin + C. length (); <br/>}< br/> If (end! = Strin. length () <br/>{< br/> arraylist. add (strin. substring (end); <br/>}</P> <p> int K = arraylist. size (); <br/> string as [] = new string [k]; <br/> return (string []) arraylist. sublist (0, K ). toarray (AS); <br/>}</P> <p> Public static string Tobin (char C) <br/>{< br/> int K = 0x8000; <br/> stringbuffer sb = new stringbuffer (16); <br/> for (INT I = 0; I <16; k >>>= 1, I ++) <br/>{< br/> Sb. append ((C & K )! = 0 )? 1: 0); <br/>}< br/> return sb. tostring (); <br/>}</P> <p>}

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.