繁体   English   中英

如何将Unicode转义seq转换为char

[英]How to convert unicode escape seq to char

在xml配置文件中,我将定界符属性配置为Unicode转义序列,如下所示:

<adapter name="Adapter2">
  <property name="adapter.delimiter" value="\u0009"/>
</adapter>

在Adapter类中还有一种获取任何属性的方法:

String getProperty(String propertyName)

对于getProperty(“ adapter.delimiter”),它返回已经转义的字符串“ \\ t”

我需要将\\ u0009属性转换为char以便能够将\\ t char提供给任何其他方法。

  • 如何在单元测试中验证该方法是否确实返回了tab char?

这不起作用(我不确定代码或单元测试是否错误):

public char getDelimiter() throws VTBaseException {
    String delimiterProperty = getProperty("adapter.delimiter");

    if (delimiterProperty == null || "".equals(delimiterProperty)) {
        //default
        return DEFAULT_DELIMITER;
    } else if (delimiterProperty.length() == 1) {
        return delimiterProperty.charAt(0);
    } else {
        throw new VTBaseException();
    }
}

单元测试:

@Test
public void testReturnDelimiterProperty()
    throws Exception
{
    VTAdapter adapter = manager.getAdapter("Adapter2");
    assertEquals(',', adapter.getDelimiter());//passes if property not set
    adapter = world.getAdapterList().getAdapter("Adapter1");
    assertEquals("\t", adapter.getDelimiter());//fails with exception bellow


}

junit.framework.AssertionFailedError: expected:<    > but was:< >
at junit.framework.Assert.fail(Assert.java:47)
at junit.framework.Assert.failNotEquals(Assert.java:283)
at junit.framework.Assert.assertEquals(Assert.java:64)
at junit.framework.Assert.assertEquals(Assert.java:71)
at 

输出选项卡:

String s = "\\u0009";
s = s.substring(2);
char i = (char)(int)Integer.valueOf(s, 16);
System.out.println(i);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM