📄 cnstring.java
字号:
package com.cn.darkblue.util;
final public class CNString {
private String sStr_ = ""; //$NON-NLS-1$
public CNString(String s) {
sStr_ = s;
}
public String subStringB(int beginIndex, int length) {
return subStringB(sStr_, beginIndex, length);
}
public String subStringB(int beginIndex) {
return subStringB(sStr_, beginIndex);
}
public String leftB(int length) {
return leftB(sStr_, length);
}
public String rightB(int length) {
return rightB(sStr_, length);
}
public String rTrim() {
return rTrim(sStr_);
}
public String lTrim() {
return lTrim(sStr_);
}
public String trim() {
return trim(sStr_);
}
public int lengthB() {
return lengthB(sStr_);
}
/*
* subString() �� byte ��ȡ���ִ� ����: String s => ��Դ�ִ� int beginIndex => �_ʼλԪ�Mλ��
* int length => ��ȡλԪ�M�L�� ���: String
*/
public static String subStringB(String s, int beginIndex, int length) {
if (s == null)
s = ""; //$NON-NLS-1$
String _sRet = ""; // ����ִ� //$NON-NLS-1$
int _iByteOff = 0; // �� byte Ӌ������Mλ��
int _iNumBytes = 0; // ȡ�� byte ��
try {
StringBuffer sb = new StringBuffer(length);
for (int i = 0; i < s.length(); i++) {
// �Д���Ԫ�Ƿ������
if (Character.UnicodeBlock.of(s.charAt(i)) != Character.UnicodeBlock.BASIC_LATIN) {
// ������Ԫ
if (_iByteOff == (beginIndex - 1)) {
// ��һbyte������������_ʼ, ԓ��Ԫ��ȡ, �� 1byte �հ�ȡ��
sb.append(" "); //$NON-NLS-1$
_iNumBytes++;
} else if (_iByteOff >= beginIndex) {
if (_iNumBytes + 2 > length) {
// ���_��ȡ�L��, ����������ֵ�ǰ��, ԓ��Ԫ��ȡ, �� 1byte �հ�ȡ��
sb.append(" "); //$NON-NLS-1$
_iNumBytes++;
} else {
sb.append(s.charAt(i));
_iNumBytes += 2;
}
}
_iByteOff += 2; // ������Ԫ byte λ�����M 2
} else {
// ��������Ԫ
if (_iByteOff >= beginIndex) {
sb.append(s.charAt(i));
_iNumBytes++;
}
_iByteOff++; // ��������Ԫ byte λ�����M 1
}
if (_iNumBytes >= length)
break; // ���_��ȡ�L��, �Y��ޒȦ
} // end for
_sRet = sb.toString();
} catch (Exception e) {
} finally {
return _sRet;
} // end try
} // end subStringB()
public static String subStringB(String s, int beginIndex, int length,
boolean isGetFirstWord) {
if (isGetFirstWord) {
return CNString.subStringB(s, beginIndex, length);
} else {
return CNString.subStringBE(s, beginIndex, length);
}
}
public static String subStringBE(String s, int beginIndex, int length) {
if (s == null) {
return ""; //$NON-NLS-1$
}
if (beginIndex < 0 || length < 0 || (length - beginIndex) < 0) {
return ""; //$NON-NLS-1$
}
if (s == null)
s = ""; //$NON-NLS-1$
String _sRet = ""; // ����ִ� //$NON-NLS-1$
int _iByteOff = 0; // �� byte Ӌ������Mλ��
int _iNumBytes = 0; // ȡ�� byte ��
try {
StringBuffer sb = new StringBuffer(length);
for (int i = 0; i < s.length(); i++) {
// �Д���Ԫ�Ƿ������
if (Character.UnicodeBlock.of(s.charAt(i)) != Character.UnicodeBlock.BASIC_LATIN) {
// ������Ԫ
if (_iByteOff == (beginIndex - 1)) {
// ��һbyte������������_ʼ, ԓ��Ԫ��ȡ, �� 1byte �հ�ȡ��
sb.append(s.charAt(i));
_iNumBytes += 2;
// _iNumBytes++;
} else if (_iByteOff >= beginIndex) {
if (_iNumBytes + 2 > length) {
// ���_��ȡ�L��, ����������ֵ�ǰ��, ԓ��Ԫ��ȡ, �� 1byte �հ�ȡ��
sb.append(" "); //$NON-NLS-1$
_iNumBytes++;
} else {
sb.append(s.charAt(i));
_iNumBytes += 2;
}
}
_iByteOff += 2; // ������Ԫ byte λ�����M 2
} else {
// ��������Ԫ
if (_iByteOff >= beginIndex) {
sb.append(s.charAt(i));
_iNumBytes++;
}
_iByteOff++; // ��������Ԫ byte λ�����M 1
}
if (_iNumBytes >= length)
break; // ���_��ȡ�L��, �Y��ޒȦ
} // end for
_sRet = sb.toString();
} catch (Exception e) {
} finally {
return _sRet;
} // end try
}
public static String subStringB(String s, int beginIndex) {
return subStringB(s, beginIndex, (lengthB(s) - beginIndex));
}
public static String leftB(String s, int length) {
return subStringB(s, 0, length);
}
public static String rightB(String s, int length) {
if (s == null)
s = ""; //$NON-NLS-1$
StringBuffer sb = new StringBuffer(s);
String sTemp = subStringB(new String(sb.reverse()), 0, length);
sb = new StringBuffer(sTemp);
return new String(sb.reverse());
}
public static String replicate(char c, int length) {
StringBuffer sb = new StringBuffer(length);
for (int i = 0; i < length; i++)
sb.append(c);
return sb.toString();
}
public static String replicate(String s, int length) {
return replicate(s.charAt(0), length);
}
public static String space(int length) {
return replicate(' ', length);
}
public String lPadB(int length) {
return lPadB(sStr_, length, ' ');
}
public static String lPadB(String s, int length) {
return lPadB(s, length, ' ');
}
/**
* �Ԃ����L�ȁ�������Ԫ.
*
* @param s
* @param length
* @param cPad;
* Ҫ�������Ԫ, �A�O�ǿհ�
* @return ������֮�ִ�
*/
public static String lPadB(String s, int length, char cPad) {
if (s == null)
s = ""; //$NON-NLS-1$
String _sRet = ""; //$NON-NLS-1$
int _n = length - lengthB(s);
if (_n <= 0) {
_sRet = subStringB(s, 0, length);
} else {
_sRet = replicate(cPad, _n) + s;
}
return _sRet;
}
public String rPadB(int length) {
return rPadB(sStr_, length, ' ');
}
public static String rPadB(String s, int length) {
return rPadB(s, length, ' ');
}
/**
* �Ԃ����L�ȁ����ҷ���Ԫ.
*
* @param s
* @param length
* @param cPad;
* Ҫ�������Ԫ, �A�O�ǿհ�
* @return ������֮�ִ�
*/
public static String rPadB(String s, int length, char cPad) {
if (s == null)
s = ""; //$NON-NLS-1$
String _sRet = ""; //$NON-NLS-1$
int _n = length - lengthB(s);
if (_n <= 0) {
_sRet = subStringB(s, 0, length);
} else {
_sRet = s + replicate(cPad, _n);
}
return _sRet;
}
public String cPadB(int length) {
return cPadB(sStr_, length);
}
public static String cPadB(String s, int length) {
return cPadB(s, length, ' ');
}
/**
* �Ԃ����L�ȁ������g��Ԫ.
*
* @param s
* @param length
* @param cPad;
* Ҫ�������Ԫ, �A�O�ǿհ�
* @return ������֮�ִ�
*/
public static String cPadB(String s, int length, char cPad) {
if (s == null)
s = ""; //$NON-NLS-1$
String _sRet = ""; //$NON-NLS-1$
int _n = length - lengthB(s);
if (_n <= 0) {
_sRet = subStringB(s, 0, length);
} else {
int _mod = _n % 2;
_n = (int) _n / 2;
_sRet = replicate(cPad, _n) + s + replicate(cPad, _n)
+ ((_mod == 1) ? " " : ""); //$NON-NLS-1$ //$NON-NLS-2$
}
return _sRet;
}
public static String lTrim(String s) {
if (s == null)
s = ""; //$NON-NLS-1$
String _sRet = ""; //$NON-NLS-1$
int _iCharOff = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != ' '
&& s.charAt(i) != '\012' && s.charAt(i) != '\015'
&& s.charAt(i) != '\011')
break;
_iCharOff++;
}
_sRet = s.substring(_iCharOff);
return _sRet;
}
public static String rTrim(String s) {
if (s == null)
s = ""; //$NON-NLS-1$
String _sRet = ""; //$NON-NLS-1$
// String _sSpace = " ";
int _iCharOff = s.length();
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) != ' ' && s.charAt(i) != '\012' && s.charAt(i) != '\015'
&& s.charAt(i) != '\011')
break;
_iCharOff--;
}
_sRet = s.substring(0, _iCharOff);
return _sRet;
}
public static String trim(String s) {
return lTrim(rTrim(s));
}
public static int lengthB(String s) {
return s.getBytes().length;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -