一個Java的字串常用類

來源:互聯網
上載者: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]);<br /> } catch (Exception ex)<br /> {<br /> }<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 /> }<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 /> ArrayList 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 == chark.length - 1)<br /> {<br /> arraylist.add(sb.toString());<br /> }<br /> }<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 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>}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.