Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to check if a char exists in a string then remove it 1

Status
Not open for further replies.

Programmer2006

Programmer
Jun 29, 2006
12
US
I have a strings "Foamtreads" and "Mens_Foamtreads"
I have an if else statement
if (there is a dash){
//remove dash
}
I can't figure out my if statement because I am getting an Argument out of Range exception when there is no dash.
if(dashPositioon=strTemp.IndexOfAny('_')!=null)
 
You can have something like this...
Code:
string tmpStr = "Mens_Foamtreads";
int dashPosition = tmpStr.IndexOf("_");
if ( dashPosition > -1)
{
    tmpStr = tmpStr.Remove(dashPosition, 1);
    MessageBox.Show(tmpStr);
}

Sharing the best from my side...

--Prashant--
 
You can also just remove it as a rule. It doesn't hurt anything to do this.

myString.Replace("_", "");

If it is there, it is removed. I can't imagine the impact is any worse than checking the indexof every time. You can also keep adding .Replace() to the end of the string and have a good old time.

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top