如何将unicode escape seq转换为char

时间:2012-07-23 09:30:34

标签: java

在xml配置文件中,我有一个分隔符属性,配置为unicode escape seq,如下所示:

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

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

String getProperty(String propertyName)

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

我需要的是将property \ 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 

1 个答案:

答案 0 :(得分:0)

输出标签:

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