细节的积累 ---字符串的常用方法总结

系统 1457 0
字符串的常用方法总结:

package cn.com.huawei.opensource.common.lang;

import java.io.*;

public class StringConverter {

public StringConverter() {
}

byte[] stringToFullByteArray(String s) {
int i = s.length();
byte abyte0[] = new byte[i * 2];
for (int j = 0; j < i; j++) {
char c = s.charAt(j);
abyte0[j * 2] = (byte) ((c & 0xff00) >> ;
abyte0[j * 2 + 1] = (byte) (c & 0xff);
}

return abyte0;
}

public static byte[] hexToByte(String s) throws IOException {
int i = s.length() / 2;
byte abyte0[] = new byte[i];
int j = 0;
if (s.length() % 2 != 0)
throw new IOException(
"hexadecimal string with odd number of characters");
for (int k = 0; k < i; k++) {
char c = s.charAt(j++);
int l = "0123456789abcdef0123456789ABCDEF".indexOf(c);
if (l == -1)
throw new IOException(
"hexadecimal string contains non hex character");
int i1 = (l & 0xf) << 4;
c = s.charAt(j++);
l = "0123456789abcdef0123456789ABCDEF".indexOf(c);
i1 += l & 0xf;
abyte0[k] = (byte) i1;
}

return abyte0;
}

public static String byteToHex(byte abyte0[]) {
int i = abyte0.length;
char ac[] = new char[i * 2];
int j = 0;
int k = 0;
for (; j < i; j++) {
int l = abyte0[j] & 0xff;
ac[k++] = HEXCHAR[l >> 4 & 0xf];
ac[k++] = HEXCHAR[l & 0xf];
}

return new String(ac);
}

public static int unicodeToAscii(OutputStream outputstream, String s,
boolean flag) throws IOException {
int i = 0;
if (s == null || s.length() == 0)
return 0;
int j = s.length();
for (int k = 0; k < j; k++) {
char c = s.charAt(k);
if (c == '\\') {
if (k < j - 1 && s.charAt(k + 1) == 'u') {
outputstream.write(c);
outputstream.write(117);
outputstream.write(48);
outputstream.write(48);
outputstream.write(53);
outputstream.write(99);
i += 6;
} else {
outputstream.write(c);
i++;
}
continue;
}
if (c >= ' ' && c <= '\177') {
outputstream.write(c);
i++;
if (c == '\'' && flag) {
outputstream.write(c);
i++;
}
} else {
outputstream.write(92);
outputstream.write(117);
outputstream.write(HEXCHAR[c >> 12 & 0xf]);
outputstream.write(HEXCHAR[c >> 8 & 0xf]);
outputstream.write(HEXCHAR[c >> 4 & 0xf]);
outputstream.write(HEXCHAR[c & 0xf]);
i += 6;
}
}

return i;
}

public static String asciiToUnicode(byte abyte0[], int i, int j) {
if (j == 0)
return "";
char ac[] = new char[j];
int k = 0;
for (int l = 0; l < j; l++) {
byte byte0 = abyte0[i + l];
if (byte0 == 92 && l < j - 5) {
byte byte1 = abyte0[i + l + 1];
if (byte1 == 117) {
l++;
int i1 = "0123456789abcdef0123456789ABCDEF"
.indexOf(abyte0[i + ++l]) << 12;
i1 += "0123456789abcdef0123456789ABCDEF".indexOf(abyte0[i
+ ++l]) << 8;
i1 += "0123456789abcdef0123456789ABCDEF".indexOf(abyte0[i
+ ++l]) << 4;
i1 += "0123456789abcdef0123456789ABCDEF".indexOf(abyte0[i
+ ++l]);
ac[k++] = (char) i1;
} else {
ac[k++] = (char) byte0;
}
} else {
ac[k++] = (char) byte0;
}
}

return new String(ac, 0, k);
}

public static String asciiToUnicode(String s) {
if (s == null || s.indexOf("\\u") == -1)
return s;
int i = s.length();
char ac[] = new char[i];
int j = 0;
for (int k = 0; k < i; k++) {
char c = s.charAt(k);
if (c == '\\' && k < i - 5) {
char c1 = s.charAt(k + 1);
if (c1 == 'u') {
k++;
int l = "0123456789abcdef0123456789ABCDEF".indexOf(s
.charAt(++k)) << 12;
l += "0123456789abcdef0123456789ABCDEF".indexOf(s
.charAt(++k)) << 8;
l += "0123456789abcdef0123456789ABCDEF".indexOf(s
.charAt(++k)) << 4;
l += "0123456789abcdef0123456789ABCDEF".indexOf(s
.charAt(++k));
ac[j++] = (char) l;
} else {
ac[j++] = c;
}
} else {
ac[j++] = c;
}
}

return new String(ac, 0, j);
}

public static int writeUTF(String s, OutputStream outputstream)
throws IOException {
int i = s.length();
boolean flag = false;
int j = 0;
for (int k = 0; k < i; k++) {
char c = s.charAt(k);
if (c >= '\001' && c <= '\177') {
outputstream.write(c);
j++;
continue;
}
if (c > '\u07FF') {
outputstream.write(0xe0 | c >> 12 & 0xf);
outputstream.write(0x80 | c >> 6 & 0x3f);
outputstream.write(0x80 | c >> 0 & 0x3f);
j += 3;
} else {
outputstream.write(0xc0 | c >> 6 & 0x1f);
outputstream.write(0x80 | c >> 0 & 0x3f);
j += 2;
}
}

return j;
}

public static int getUTFSize(String s) {
int i = s != null ? s.length() : 0;
int j = 0;
for (int k = 0; k < i; k++) {
char c = s.charAt(k);
if (c >= '\001' && c <= '\177') {
j++;
continue;
}
if (c > '\u07FF')
j += 3;
else
j += 2;
}

return j;
}

public static String inputStreamToString(InputStream inputstream, int i)
throws IOException {
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
StringWriter stringwriter = new StringWriter();
char c = '\u2000';
char ac[] = new char[c];
int j = i;
do {
if (j <= 0)
break;
int k = inputstreamreader.read(ac, 0, j <= c ? j : ((int) (c)));
if (k == -1)
break;
stringwriter.write(ac, 0, k);
j -= k;
} while (true);
stringwriter.close();
return stringwriter.toString();
}

public static String toQuotedString(String s, char c, boolean flag) {
if (s == null)
return null;
int i = flag ? count(s, c) : 0;
int j = s.length();
char ac[] = new char[2 + i + j];
int k = 0;
int l = 0;
ac[l++] = c;
for (; k < j; k++) {
char c1 = s.charAt(k);
ac[l++] = c1;
if (flag && c1 == c)
ac[l++] = c1;
}

ac[l] = c;
return new String(ac);
}

static int count(String s, char c) {
int i = 0;
if (s != null) {
int j = s.length();
do {
if (--j < 0)
break;
char c1 = s.charAt(j);
if (c1 == c)
i++;
} while (true);
}
return i;
}

private static final char HEXCHAR[] = { '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

private static final String HEXINDEX = "0123456789abcdef0123456789ABCDEF";

}

细节的积累 ---字符串的常用方法总结


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论