Kulvinder

Hi,

I have a string "1AA,1BB,1CC" and i want to remove all the integers from this string.

What should be the best way (Hint : Don't look at the "," and use string.split(',') and operate and rejoin)

Thanks

Kulvinder Singh




Re: Visual C# General How to remove all integers from a string ?

Missa

Go through each char in the string and try to parse it in integer (try{ int.Parse() }). If that operation success delete the caracter from the string and that is it.





Re: Visual C# General How to remove all integers from a string ?

In Love With Dotnet

Hi,

Another suggestion.

string str = "1AA1BB1CC";

char[] str1 = str.ToCharArray();

Array.Sort(str1);

I have sorted string and its now in array. Now you can iterate through values in array and find where exactly to split from. Another way to split could be using ascii code of character rathar than making "A" as static to split from. This could be handy when you have long list to sort n split.

Hope this will help.






Re: Visual C# General How to remove all integers from a string ?

My Vizai

Use regular Expressions to Find and remove Integers





Re: Visual C# General How to remove all integers from a string ?

In Love With Dotnet

how did i forget RegEx. Havent used it since long time so

Here u can find stuff to do this. http://www.dotnetcoders.com/web/Learning/Regex/regexobject.aspx

Hope this will resolve.






Re: Visual C# General How to remove all integers from a string ?

Zamial

RegEx FTW!!

So easily forgotten, yet so powerful.





Re: Visual C# General How to remove all integers from a string ?

Bulldog.NET

Hi

I just call the method below and it works well even if the string has spaces in between, see code below:-

Code Block

public static string BuildChar(string str)

{

StringBuilder buildchar = new StringBuilder();

foreach (Char c in str)

{

try

{

int.Parse(c.ToString());

}

catch

{

buildchar.Append(c);

}

}

return buildchar.ToString();

}

Usage :-

Code Block

string str = "1AA,1BB,1CC";

Console.WriteLine(BuildChar(str));

Hope this helps.





Re: Visual C# General How to remove all integers from a string ?

CalvinR

No no no no no BullDog.Net never ever ever use exceptions like that, EVER!

Exceptions are for exceptional circumstances, you should NEVER use exceptions like that. It will never be the correct way for doing things.

Exceptions are slow, and by slow I mean SLOW.

If one of my coworkers ever produced code that used exceptions in that manner, I would seriously have to consider their skill at programming. It may seem like a clever way of doing things, but it shows a distinct lack of knowledge about basic programming practices.

Now I don't mean to sound insulting, so I apologize if I do but never ever again even think of using exceptions to handle the normal flow of code.

Remember just because something works doesn't mean it's even remotely the right way to do it.








Re: Visual C# General How to remove all integers from a string ?

H. Tony

for single char, Char.IsDigit could be used

or Regex.Replace(strIn, "\d", "");







Re: Visual C# General How to remove all integers from a string ?

Bulldog.NET

CalvinR wrote:
No no no no no BullDog.Net never ever ever use exceptions like that, EVER!

Exceptions are for exceptional circumstances, you should NEVER use exceptions like that. It will never be the correct way for doing things.

Exceptions are slow, and by slow I mean SLOW.

If one of my coworkers ever produced code that used exceptions in that manner, I would seriously have to consider their skill at programming. It may seem like a clever way of doing things, but it shows a distinct lack of knowledge about basic programming practices.

Now I don't mean to sound insulting, so I apologize if I do but never ever again even think of using exceptions to handle the normal flow of code.

Remember just because something works doesn't mean it's even remotely the right way to do it.



Mhh I see CalvinR and you are right the app does delay. this should do the trick then.

private static string BuildChar(string str)

{

StringBuilder buildstring = new StringBuilder();

foreach (Char c in str)

{

if (!Char.IsDigit(c))

buildstring.Append(c);

}

return buildstring.ToString();

}






Re: Visual C# General How to remove all integers from a string ?

Cliff Benoist

I use the Regex method in most cases to replace items in strings.





Re: Visual C# General How to remove all integers from a string ?

CalvinR

BullDog.Net:

That is a much better way of doing it, although I personally prefer to use regular expressions for any string manipulation but that's just because I think regular expressions are the best way to do anything with manipulating text.






Re: Visual C# General How to remove all integers from a string ?

Bulldog.NET

CalvinR wrote:
BullDog.Net:

That is a much better way of doing it, although I personally prefer to use regular expressions for any string manipulation but that's just because I think regular expressions are the best way to do anything with manipulating text.

I agree with u CalvinR, Regular Expressions are much easier than string manipulation. This is how I have done it.

The nice thing is less coding and its simple.

public static string BuildCharUsingRegex(string str)

{

Regex regx = new Regex("([0-9])");

return regx.Replace(str, "");

}






Re: Visual C# General How to remove all integers from a string ?

CalvinR

Even less typing
public static string BuildCharUsingRegex(string str)
{
return (new Regex("([\d])")).Replace(str, "");
}


And Knowledge of regular expressions will also allow you more power when you use the find function in Visual studio, also it's great for Grep, or Perl.