简体   繁体   中英

How to convert unicode escape seq to char

In a xml config file I have a delimiter property configured as unicode escape seq like this:

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

There is also method getting any property in Adapter class:

String getProperty(String propertyName)

For getProperty("adapter.delimiter") it returns already escaped string "\\t"

What I need is to convert property \ into char to be able to provide the \\t char to any further methods.

  • How to verify that the method really returns tab char in unit test?

This does not work (I am not sure if the code or unit test is wrong):

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();
    }
}

Unit test:

@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 

Outputs a tab:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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