answersLogoWhite

0

public static final String convertString(final String stringToSearch) {

// text representation of digits

final String[] digits = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

// the new version of stringToSearch, as we convert digits

StringBuffer convertedString = new StringBuffer();

// delimeter for splitting up the string

// - change this if you need to parse on more characters

final String delim = " ,.?!\t\n\r\f";

// parse using StringTokenizer for simplicity

StringTokenizer parser = new StringTokenizer(stringToSearch, delim, true);

// iterate through tokens

while (parser.hasMoreTokens()) {

String currentToken = parser.nextToken();

// if currentToken is a single digit, convert it to text

if (currentToken.matches("\\d")) {

currentToken = digits[Integer.parseInt(currentToken)];

}

// append converted text to convertedString

convertedString.append(currentToken);

}

return convertedString.toString();

}

User Avatar

Wiki User

16y ago

What else can I help you with?