Reputation: 5981
I have to remove leading and trailing numeric characters from a string in our web-service client project as for some reason the web-service at the other end in Norway won't accept Nicosia 1121
as a string...
So I decided to build a little re-useable function that would do it:
public static string CleanNosFromStr(string text, char charsToRemove)
{
var CleanedStr = text.TrimEnd(charsToRemove).TrimStart(charsToRemove);
return CleanedStr.ToString();
}
and I wanted to call it like this:
char chars2Remove= new string("0,1,2,3,4, 5,6,7,8,9,0, ").Split(",");
wsSoapBody.CardCity =
myextensions.CleanNosFromStr(aMessage[(int)cardCreate.CardCity].ToString(),chars2Remove);
But it won't compile...
The compilation errors I am getting are:
The best overloaded method match for 'string.String(char*)' has some invalid arguments
Argument 1: cannot convert from 'string' to 'char*'
Where am I going wrong, and is there a better way?
Of course I could just use
wsSoapBody.CardCity = aMessage[(int)cardCreate.CardCity].ToString().TrimEnd
('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ' ').
TrimStart('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ' ');
but I'd prefer to have something re-useable.
Thanks
Upvotes: 2
Views: 1814
Reputation: 2894
You could use Regex. For example, this replaces any character between (and including) the values of 0
and 9
, with ""
, which essentially just removes numbers from the string.
public static string RemoveNumbersFromStr(string text)
{
return Regex.Replace(text, "[0-9]", "");
}
And a use case:
wsSoapBody.CardCity =
myextensions.RemoveNumbersFromStr(aMessage[(int)cardCreate.CardCity].ToString());
Or if you like being able to debug things more easily:
var message = aMessage[(int)cardCreate.CardCity];
string strMessage = message.ToString();
wsSoapBody.CardCity = myextensions.RemoveNumbersFromStr(strMessage);
Upvotes: 1
Reputation: 62488
As you want remove all numbers from the string.
You can achieve it using linq to just filter all characters that are letters using Char.IsLetter()
:
public static string CleanNosFromStr(string text)
{
var CleanedStr = new String(text.Where(x=>Char.IsLetter(x)).ToArray());
return CleanedStr;
}
and use it :
wsSoapBody.CardCity =
myextensions.CleanNosFromStr(aMessage[(int)cardCreate.CardCity].ToString());
Upvotes: 2
Reputation: 711
Some points:
text.TrimEnd(charsToRemove).TrimStart(charsToRemove)
can be shortened with text.Trim(charsToRemove)
.Trim
, TrimStart
and TrimEnd
accepts single or multiple chars (see String.Trim).new string("0,1,2,3,4, 5,6,7,8,9,0, ").Split(",")
should be "0,1,2,3,4,5,6,7,8,9, ".Split(',')
..."0123456789 ".ToCharArray()
.Finally your code could be:
private static readonly char[] NosCharsToRemove = "0123456789 ".ToCharArray();
public static string CleanNosFromStr(string text)
{
return text.Trim(NosCharsToRemove);
}
and
wsSoapBody.CardCity = myextensions.CleanNosFromStr(aMessage[(int)cardCreate.CardCity].ToString());
Upvotes: 5
Reputation: 5380
You could also use a Regex:
Regex rg = new Regex("\\d+");
string test = "12345allo12345";
string result = rg.Replace(test, "");
Cheers
Upvotes: 0