String.Replace method exception

Stack Overflow Exception

Page 1 of 1

4 Replies - 2504 Views - Last Post: 14 November 2009 - 02:52 PM Rate Topic: -----

#1 Grapevine  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 55
  • Joined: 26-October 09

String.Replace method exception

Posted 14 November 2009 - 09:38 AM

Ok,

I am working on yet another project.

I have a textbox that take a string to search for. The issue was that when a symbol was used in the search string (i.e. c#), the search engine would remove the '#' symbol and just search for 'c'. I know why now and its because the search engines replace symbols with a code that the engine can use. The symbol code (as I call it) for the # symbol is '%23'. I have went through all the symbols and I created this method that will replace the symbol with the correct symbol code.

private void DetermineSymbolReplacement()
		{
			try
			{
				if (searchBox.Text.Contains("#"))
				{
					oldValue = "#";
					newValue = "%23";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("!"))
				{
					oldValue = "!";
					newValue = "%21";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("$"))
				{
					oldValue = "$";
					newValue = "%24";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("%"))
				{
					oldValue = "%";
					newValue = "%25";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("^"))
				{
					oldValue = "^";
					newValue = "%5E";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("&"))
				{
					oldValue = "&";
					newValue = "%26";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("("))
				{
					oldValue = "(";
					newValue = "%28";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains(")"))
				{
					oldValue = ")";
					newValue = "%29";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("+"))
				{
					oldValue = "+";
					newValue = "%2B";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("="))
				{
					oldValue = "=";
					newValue = "%3D";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("{"))
				{
					oldValue = "{";
					newValue = "%7B";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("}"))
				{
					oldValue = "}";
					newValue = "%7D";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("["))
				{
					oldValue = "[";
					newValue = "%5B";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("]"))
				{
					oldValue = "]";
					newValue = "%5D";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("|"))
				{
					oldValue = "|";
					newValue = "%7C";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("\\"))
				{
					oldValue = "\\";
					newValue = "%5C";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains(":"))
				{
					oldValue = ":";
					newValue = "%3A";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains(";"))
				{
					oldValue = ";";
					newValue = "%3B";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("\""))
				{
					oldValue = "\"";
					newValue = "%22";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("'"))
				{
					oldValue = "'";
					newValue = "%27";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("<"))
				{
					oldValue = "<";
					newValue = "%3C";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains(">"))
				{
					oldValue = ">";
					newValue = "%3E";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains(","))
				{
					oldValue = ",";
					newValue = "%2C";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("?"))
				{
					oldValue = "?";
					newValue = "%3F";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("/"))
				{
					oldValue = "/";
					newValue = "%2F";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("~"))
				{
					oldValue = "~";
					newValue = "%7E";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("`"))
				{
					oldValue = "`";
					newValue = "%60";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
			}
			catch (Exception repexc)
			{
				MessageBox.Show(repexc.Message, "Replace Symbol Exception");
			}
		}



I call this method on the searchBox_TextChanged event (for now until I get this thing working properly). It works perfectly when I define only 3 symbols.

try
			{
				if (searchBox.Text.Contains("#"))
				{
					oldValue = "#";
					newValue = "%23";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("!"))
				{
					oldValue = "!";
					newValue = "%21";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
				else if (searchBox.Text.Contains("$"))
				{
					oldValue = "$";
					newValue = "%24";
					searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
				}
}
			catch (Exception repexc)
			{
				MessageBox.Show(repexc.Message, "Replace Symbol Exception");
			}



If I use the code where I define ALL the symbols on the keyboard, I get an exception:

An unhandled exception of type 'System.StackOverflowException' occurred in Grapevine Clarity.exe

sometimes it throws this exception:

An unhandled exception of type 'System.StackOverflowException' occurred in mscorelib.dll



I dont know why im getting this exception. Even when I put the code in try/catch blocks, it does not catch this exception. Can someone help me get my method working?? I need it to replace any symbol I have defined in my method. It wont let me define more than 3 symbols.......Is there another way of doing this?

Thanks,

This post has been edited by Grapevine: 14 November 2009 - 09:49 AM


Is This A Good Question/Topic? 0
  • +

Replies To: String.Replace method exception

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1020
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: String.Replace method exception

Posted 14 November 2009 - 10:28 AM

What do you think happens here:
if (searchBox.Text.Contains("%")) {
	oldValue = "%";
	newValue = "%25";
	searchBox.Text = searchBox.Text.Replace(oldValue, newValue);
}

Was This Post Helpful? 1
  • +
  • -

#3 Grapevine  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 55
  • Joined: 26-October 09

Re: String.Replace method exception

Posted 14 November 2009 - 01:55 PM

Maybe im confusing it somehow? I actually thought that I may have a problem with that particular else block when I wrote it. Is there an escape code for a string that has the % symbol in it?

Anyway, I ommitted that else block from the method and now it's working great. It actually wasnt throwing the exception on that line, it was occuring on the first if block of the method.....

Thanks a lot, bro.
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1020
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: String.Replace method exception

Posted 14 November 2009 - 02:09 PM

View PostGrapevine, on 14 Nov, 2009 - 12:55 PM, said:

Anyway, I ommitted that else block from the method and now it's working great. It actually wasnt throwing the exception on that line, it was occuring on the first if block of the method.....
Yep, the first line would be were it was having the problem. There is nothing wrong with the code, it does the substitution correctly. But when you assign it back to the textbox, it triggers the textchanged event. So it does the substitution again, and again. Since none of the methods actually finish (the event comes first) it quickly fills up the stack memory with all the calls until it explodes :)
Was This Post Helpful? 0
  • +
  • -

#5 Grapevine  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 55
  • Joined: 26-October 09

Re: String.Replace method exception

Posted 14 November 2009 - 02:52 PM

ahh makes perfect sense. you're great man thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1