/**
* Licensed to Jarry.DK under one or more
* contributor license agreements.
* Jarry.DK licenses this file to you under the
* Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package dk.jarry.util;
/**
*
* @author Michael Bornholdt Nielsen (mni @ jarry.dk)
*
*/
public class StringUtil {
/**
* Converts any hex entity type chars ("æ") or ("ø") to their
* ascii equivalent
*
* @param theString
* String the string to conver
* @return String the converted string
*/
public static String hexEntityToStringConvert(String theString) {
int nextEntity = theString.indexOf("&#x");
while (nextEntity != -1) {
String value = "";
int nextSemicolon = theString.indexOf(";");
if (nextSemicolon == -1) {
value = theString.substring(nextEntity + 3, nextEntity + 7);
} else {
value = theString.substring(nextEntity + 3, nextEntity
+ nextSemicolon - nextEntity);
}
char newChar = (char) Integer.valueOf(value, 16).intValue();
String charString = String.valueOf(newChar);
theString = theString.replaceAll("&#x" + value + ";", charString);
theString = theString.replaceAll("&#x" + value, charString);
nextEntity = theString.indexOf("&#x", nextEntity + 3);
}
return theString;
}
/**
* Test
*
* @param args
*/
public static void main(String[] args) {
System.out.println(hexEntityToStringConvert("æ"));
System.out.println(hexEntityToStringConvert("ø"));
}
}
|
Created 10/07-2013 by Michael Bornholdt Nielsen