CharUtil
package com.opslab.util;
import java.io.UnsupportedEncodingException;
/**
* Various character and character sequence utilities, including <code>char[]</code> - <code>byte[]</code> conversions.
*/
public final class CharUtil {
/**
* Converts (signed) byte to (unsigned) char.
*/
public static char toChar(byte b) {
return (char) (b & 0xFF);
}
/**
* Converts char array into byte array by stripping the high byte of each character.
*/
public final static byte[] toSimpleByteArray(char[] carr) {
byte[] barr = new byte[carr.length];
for (int i = 0; i < carr.length; i++) {
barr[i] = (byte) carr[i];
}
return barr;
}
/**
* Converts char sequence into byte array.
*
* @see #toSimpleByteArray(char[])
*/
public final static byte[] toSimpleByteArray(CharSequence charSequence) {
byte[] barr = new byte[charSequence.length()];
for (int i = 0; i < barr.length; i++) {
barr[i] = (byte) charSequence.charAt(i);
}
return barr;
}
// ---------------------------------------------------------------- ascii
/**
* Converts byte array to char array by simply extending bytes to chars.
*/
public final static char[] toSimpleCharArray(byte[] barr) {
char[] carr = new char[barr.length];
for (int i = 0; i < barr.length; i++) {
carr[i] = (char) (barr[i] & 0xFF);
}
return carr;
}
/**
* Returns ASCII value of a char. In case of overload, 0x3F is returned.
*/
public final static int toAscii(char c) {
if (c <= 0xFF) {
return c;
} else {
return 0x3F;
}
}
/**
* Converts char array into {@link #toAscii(char) ASCII} array.
*/
public final static byte[] toAsciiByteArray(char[] carr) {
byte[] barr = new byte[carr.length];
for (int i = 0; i < carr.length; i++) {
barr[i] = (byte) ((int) (carr[i] <= 0xFF ? carr[i] : 0x3F));
}
return barr;
}
// ---------------------------------------------------------------- raw arrays
/**
* Converts char sequence into ASCII byte array.
*/
public final static byte[] toAsciiByteArray(CharSequence charSequence) {
byte[] barr = new byte[charSequence.length()];
for (int i = 0; i < barr.length; i++) {
char c = charSequence.charAt(i);
barr[i] = (byte) ((int) (c <= 0xFF ? c : 0x3F));
}
return barr;
}
/**
* Converts char array into byte array by replacing each character with two bytes.
*/
public final static byte[] toRawByteArray(char[] carr) {
byte[] barr = new byte[carr.length << 1];
for (int i = 0, bpos = 0; i < carr.length; i++) {
char c = carr[i];
barr[bpos++] = (byte) ((c & 0xFF00) >> 8);
barr[bpos++] = (byte) (c & 0x00FF);
}
return barr;
}
// ---------------------------------------------------------------- encoding
public final static char[] toRawCharArray(byte[] barr) {
int carrLen = barr.length >> 1;
if (carrLen << 1 < barr.length) {
carrLen++;
}
char[] carr = new char[carrLen];
int i = 0, j = 0;
while (i < barr.length) {
char c = (char) (barr[i] << 8);
i++;
if (i != barr.length) {
c += barr[i] & 0xFF;
i++;
}
carr[j++] = c;
}
return carr;
}
/**
* Converts char array to byte array using default Jodd encoding.
*/
public final static byte[] toByteArray(char[] carr) throws UnsupportedEncodingException {
return new String(carr).getBytes(CharsetUtil.UTF_8);
}
/**
* Converts char array to byte array using provided encoding.
*/
public final static byte[] toByteArray(char[] carr, String charset) throws UnsupportedEncodingException {
return new String(carr).getBytes(charset);
}
/**
* Converts byte array of default Jodd encoding to char array.
*/
public final static char[] toCharArray(byte[] barr) throws UnsupportedEncodingException {
return new String(barr, CharsetUtil.UTF_8).toCharArray();
}
// ---------------------------------------------------------------- find
/**
* Converts byte array of specific encoding to char array.
*/
public final static char[] toCharArray(byte[] barr, String charset) throws UnsupportedEncodingException {
return new String(barr, charset).toCharArray();
}
/**
* Match if one character equals to any of the given character.
*
* @return <code>true</code> if characters match any character from given array,
* otherwise <code>false</code>
*/
public final static boolean equalsOne(char c, char[] match) {
for (char aMatch : match) {
if (c == aMatch) {
return true;
}
}
return false;
}
/**
* Finds index of the first character in given array the matches any from the
* given set of characters.
*
* @return index of matched character or -1
*/
public final static int findFirstEqual(char[] source, int index, char[] match) {
for (int i = index; i < source.length; i++) {
if (equalsOne(source[i], match) == true) {
return i;
}
}
return -1;
}
/**
* Finds index of the first character in given array the matches any from the
* given set of characters.
*
* @return index of matched character or -1
*/
public final static int findFirstEqual(char[] source, int index, char match) {
for (int i = index; i < source.length; i++) {
if (source[i] == match) {
return i;
}
}
return -1;
}
/**
* Finds index of the first character in given array the differs from the
* given set of characters.
*
* @return index of matched character or -1
*/
public final static int findFirstDiff(char[] source, int index, char[] match) {
for (int i = index; i < source.length; i++) {
if (equalsOne(source[i], match) == false) {
return i;
}
}
return -1;
}
/**
* Finds index of the first character in given array the differs from the
* given set of characters.
*
* @return index of matched character or -1
*/
public final static int findFirstDiff(char[] source, int index, char match) {
for (int i = index; i < source.length; i++) {
if (source[i] != match) {
return i;
}
}
return -1;
}
/**
* Returns <code>true</code> if character is a white space ({@code <= ' '}).
* White space definition is taken from String class (see: <code>trim()</code>).
*/
public final static boolean isWhitespace(char c) {
return c <= ' ';
}
/**
* Returns <code>true</code> if specified character is lowercase ASCII.
* If user uses only ASCIIs, it is much much faster.
*/
public final static boolean isLowercaseAlpha(char c) {
return (c >= 'a') && (c <= 'z');
}
/**
* Returns <code>true</code> if specified character is uppercase ASCII.
* If user uses only ASCIIs, it is much much faster.
*/
public final static boolean isUppercaseAlpha(char c) {
return (c >= 'A') && (c <= 'Z');
}
public final static boolean isAlphaOrDigit(char c) {
return isDigit(c) || isAlpha(c);
}
public final static boolean isWordChar(char c) {
return isDigit(c) || isAlpha(c) || (c == '_');
}
public final static boolean isPropertyNameChar(char c) {
return isDigit(c) || isAlpha(c) || (c == '_') || (c == '.') || (c == '[') || (c == ']');
}
/**
* Indicates whether the given character is in the {@code ALPHA} set.
*
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
public final static boolean isAlpha(char c) {
return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
}
/**
* Indicates whether the given character is in the {@code DIGIT} set.
*
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
public final static boolean isDigit(char c) {
return c >= '0' && c <= '9';
}
/**
* Indicates whether the given character is the hexadecimal digit.
*/
public final static boolean isHexDigit(char c) {
return (c >= '0' && c <= '9') || ((c >= 'a') && (c <= 'f')) || ((c >= 'A') && (c <= 'F'));
}
/**
* Indicates whether the given character is in the <i>gen-delims</i> set.
*
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
public final static boolean isGenericDelimiter(int c) {
switch (c) {
case ':':
case '/':
case '?':
case '#':
case '[':
case ']':
case '@':
return true;
default:
return false;
}
}
/**
* Indicates whether the given character is in the <i>sub-delims</i> set.
*
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
public final static boolean isSubDelimiter(int c) {
switch (c) {
case '!':
case '$':
case '&':
case '\'':
case '(':
case ')':
case '*':
case '+':
case ',':
case ';':
case '=':
return true;
default:
return false;
}
}
/**
* Indicates whether the given character is in the <i>reserved</i> set.
*
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
public final static boolean isReserved(char c) {
return isGenericDelimiter(c) || isSubDelimiter(c);
}
/**
* Indicates whether the given character is in the <i>unreserved</i> set.
*
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
public final static boolean isUnreserved(char c) {
return isAlpha(c) || isDigit(c) || c == '-' || c == '.' || c == '_' || c == '~';
}
/**
* Indicates whether the given character is in the <i>pchar</i> set.
*
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
public final static boolean isPchar(char c) {
return isUnreserved(c) || isSubDelimiter(c) || c == ':' || c == '@';
}
/**
* Uppers lowercase ASCII char.
*/
public final static char toUpperAscii(char c) {
if (isLowercaseAlpha(c)) {
c -= (char) 0x20;
}
return c;
}
/**
* Lowers uppercase ASCII char.
*/
public final static char toLowerAscii(char c) {
if (isUppercaseAlpha(c)) {
c += (char) 0x20;
}
return c;
}
}
CharUtil的更多相关文章
- 《Java学习笔记(第8版)》学习指导
<Java学习笔记(第8版)>学习指导 目录 图书简况 学习指导 第一章 Java平台概论 第二章 从JDK到IDE 第三章 基础语法 第四章 认识对象 第五章 对象封装 第六章 继承与多 ...
- 20145330第六周《Java学习笔记》
20145330第六周<Java学习笔记> . 这周算是很忙碌的一周.因为第六周陆续很多实验都开始进行,开始要准备和预习的科目日渐增多,对Java分配的时间不知不觉就减少了,然而第十和十一 ...
- 20145225《Java程序设计》 第6周学习总结
20145225<Java程序设计> 第6周学习总结 教材学习内容总结 第十章 输入/输出 10.1InputStream与OutputStream 串流:衔接数据的来源和目的地就是串流对 ...
- Java 完美判断中文字符
Java判断一个字符串是否有中文一般情况是利用Unicode编码(CJK统一汉字的编码区间:0x4e00–0x9fbb)的正则来做判断,但是其实这个区间来判断中文不是非常精确,因为有些中文的标点符号比 ...
- 20145235 《Java程序设计》第6周学习总结
教材学习内容总结 10.1 InputStream与OutputStream 串流设计的概念 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象. 从应用程序角度来看,如果要将 ...
- 20145207《Java程序设计》第6周学习总结
教材学习内容总结 一.输入/输出 InputStream与Outputstream • 串流设计的概念 从应用程序角度看,将数据从来源取出,可以使用输入串流,将数据写入目的地,可以使用输出串流:在Ja ...
- 《Java程序设计》第六周学习总结
20145224 <Java程序设计>第六周学习总结 教材学习内容总结 第十章输入和输出 10.1.1 ·若要将数据从来源中取出,可以使用输入串流:若要将数据写入目的地,可以使用输出串流. ...
- # 20145210 《Java程序设计》第06周学习总结
教材学习内容总结 第十章 输入\输出 10.1 InputStream与OutputStream •串流设计的概念 •java将输入\输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象 •从应 ...
- Java 完美判断中文字符的方法
Java判断一个字符串是否有中文一般情况是利用Unicode编码(CJK统一汉字的编码区间:0x4e00–0x9fbb)的正则来做判断,但是其实这个区间来判断中文不是非常精确,因为有些中文的标点符号比 ...
随机推荐
- spring boot学习笔记(二)创建spring boot项目
用eclipse(需要用高版本,要不然弄不出来):new →Spring Sarter Project 用IDEA:一般默认 一般默认 入门级的先 剩下的一般默认... 一.项目至少有下面的东西,里面 ...
- LINQ查询表达式(5) - LINQ Null值处理&异常处理
查询表达式中处理Null值 此示例演示如何处理源集合中可能的 null 值. 诸如 IEnumerable<T> 等对象集合可能包含值为 null 的元素. 如果源集合为 null 或包含 ...
- Python——python3的requests模块的导入
前言 突然就要搞python,我这个心哦~ 版本 | python 3.7 步骤 配置环境变量 右击-->属性-->打开文件位置 进入到脚本目录: C:\Users\Administrat ...
- [DUBBO] qos-server can not bind localhost:22222错误解决
问题描述 在启动dubbo-consumer工程时报错,信息如下: 2019-11-29 09:22:18.001 ERROR [RMI TCP Connection(2)-127.0.0.1] [o ...
- mongoDB线上数据库连接报错记录
报错信息提示:无法在第一次连接时连接到服务器 别的一切正常,经过查询得知,是因为如果电脑没设定固定IP,并且重启情况下可能会导致IP地址更改. 解决方法: 将本机ip地址添加到cluster的白名单即 ...
- 洛谷P1650赛马与codevs 2181 田忌赛马
洛谷P1650 赛马 题目描述 我国历史上有个著名的故事: 那是在2300年以前.齐国的大将军田忌喜欢赛马.他经常和齐王赛马.他和齐王都有三匹马:常规马,上级马,超级马.一共赛三局,每局的胜者可以从负 ...
- 1071 Speech Patterns (25)(25 分)
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- 【Python】安装MySQLdb模块centos 6.1 宝塔Linux面板 MySQL5.6
[Python]安装MySQLdb模块centos 6.1 宝塔Linux面板 MySQL5.6 总之是各种坑 先说一下,宝塔安装在centos 6.1 i368 也就是32位系统上的方法 https ...
- PowerBuilder 这么古老的语言(破解一软件)
PowerBuilder 这么古老的语言,编辑器用的6.5的好古老的气息,好吧破解木有兴趣了, 不过嘛可以说一下破解思路,这个系统使用的是圣天狗,联网版的. 复制狗(暴力,没技术味道) 模拟狗(也是 ...
- shell history 命令
1.history命令可以显示历史执行过的命令: 2.使用!+序号执行该序号对应的命令: 例子 $ history sed 's/haha/hello/g' test cat test cat tes ...