本文整理了Java中org.apache.commons.lang3.StringUtils.toString()
方法的一些代码示例,展示了StringUtils.toString()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。StringUtils.toString()
方法的具体详情如下:
包路径:org.apache.commons.lang3.StringUtils
类名称:StringUtils
方法名:toString
[英]Converts a byte[]
to a String using the specified character encoding.
[中]使用指定的字符编码将byte[]
转换为字符串。
代码示例来源:origin: org.apache.commons/commons-lang3
/**
* Tests {@link StringUtils#toString(byte[], String)}
*
* @throws java.io.UnsupportedEncodingException because the method under test max throw it
* @see StringUtils#toString(byte[], String)
*/
@Test
public void testToString() throws UnsupportedEncodingException {
final String expectedString = "The quick brown fox jumps over the lazy dog.";
byte[] expectedBytes = expectedString.getBytes(Charset.defaultCharset());
// sanity check start
assertArrayEquals(expectedBytes, expectedString.getBytes());
// sanity check end
assertEquals(expectedString, StringUtils.toString(expectedBytes, null));
assertEquals(expectedString, StringUtils.toString(expectedBytes, SystemUtils.FILE_ENCODING));
final String encoding = "UTF-16";
expectedBytes = expectedString.getBytes(Charset.forName(encoding));
assertEquals(expectedString, StringUtils.toString(expectedBytes, encoding));
}
代码示例来源:origin: org.kuali.common/kuali-util
/**
* Given a string in <code>strictly hex</code> format and the <code>encoding</code> that was used to produce the hex, convert it back to a Java <code>String</code>.
* <code>strictly hex</code> in the context of this method means that the string:<br>
* 1 - Contains only the characters <code>a-f</code>, <code>A-F</code>, and <code>0-9</code><br>
* 2 - Its length is an even number.
*/
public static final String toStringFromHex(String hex, String encoding) throws UnsupportedEncodingException {
byte[] bytes = getBytesFromHexString(hex);
return StringUtils.toString(bytes, encoding);
}
内容来源于网络,如有侵权,请联系作者删除!