Previous, I am having a C++ string processing code which is able to do this.
input -> Hello 12
output-> Hello
input -> Hello 12 World
output-> Hello World
input -> Hello12 World
output-> Hello World
input -> Hello12World
output-> HelloWorld
The following is the C++ code.
std::string Utils::toStringWithoutNumerical(const std::string& str) {
std::string result;
bool alreadyAppendSpace = false;
for (int i = 0, length = str.length(); i < length; i++) {
const char c = str.at(i);
if (isdigit(c)) {
continue;
}
if (isspace(c)) {
if (false == alreadyAppendSpace) {
result.append(1, c);
alreadyAppendSpace = true;
}
continue;
}
result.append(1, c);
alreadyAppendSpace = false;
}
return trim(result);
}
May I know in Python, what is the Pythonic way for implementing such functionality? Is regular expression able to achieve so?
Thanks.