0

I am basically looking for a way to check if a certain string contains any of a certain list of chars, and if contains one of these to split the string and then insert the same char infront/after it. This is because these certain chars are breaking my search when they are input due to SQL not handling them well.

This is how far I have actually got so far:

string[] errorChars = new string[]
{
    "!",
    "}",
    "{",
    "'",
};

for (int i = 0; i < errorChars.Count(); i++)
{
    if(fTextSearch.Contains(errorChars[i]))
    {
    }
}
NominSim
  • 8,447
  • 3
  • 28
  • 38
Hello World
  • 1,379
  • 4
  • 20
  • 41
  • Let me see if I understand: Given the string "f!o}o", you want to split it into f! o} and o? I'm not sure if I completely understood - if that is what you want to accomplish, then you can use the String.Split method. – Daniel Jul 25 '12 at 12:40
  • 'split the string and insert before and after it' can mean a lot of things. Why don't you give us an example of an expected input and output. – A.R. Jul 25 '12 at 12:40
  • 1
    Probably http://stackoverflow.com/questions/995478/sql-server-full-text-search-escape-characters will help. – GSerg Jul 25 '12 at 12:40

2 Answers2

1

I think what you are really wanting is a replace function.

for (int i = 0; i < errorChars.Count(); i++)
{
    if(fTextSearch.Contains(errorChars[i]))
    {
        fTextSearch.Replace(errorChars[i],errorChars[i] + errorChars[i]);
    }
}

although doubling up the character is probably not the answer. You need the escape char which is \ so the replace string would be

ftextSearch.Replace(errorChars[i],"\"+errorChars[i]);
Brian
  • 2,229
  • 17
  • 24
1

The problem with several answers (in their current rendition) is that they are dropping your split character. If you need to keep your split character, try this:

StringBuilder sb = new StringBuilder();

string[] splitString = fTextSearch.Split(errorChars, StringSplitOptions.None);

int numNewCharactersAdded = 0;
foreach( string itm in splitString)
{
   sb.Append(itm); //append string
   if (fTextSearch.Length > (sb.Length - numNewCharactersAdded))
   {
      sb.Append(fTextSearch[sb.Length - numNewCharactersAdded]); //append splitting character
      sb.Append(fTextSearch[sb.Length - numNewCharactersAdded - 1]); //append it again
      numNewCharactersAdded ++;
   }
}

fTextSearch = sb.ToString();

Here's an IDEOne example

Jaime Torres
  • 10,365
  • 1
  • 48
  • 56
  • The only issue with this is that I do not wish to "replace" the character so to speak, but more so add the same character next to it, so there are two of these characters (as this will cause the character to escape in SQL stopping the errors) – Hello World Jul 25 '12 at 12:52
  • Oh, you just want to duplicate whatever character you found? – Jaime Torres Jul 25 '12 at 12:55
  • Pretty much, if you repeat characters in an SQL query it escapes them. – Hello World Jul 25 '12 at 12:57
  • But this doesn't keep the original string name, I need to keep the same name as else it will need changing hugely. – Hello World Jul 25 '12 at 13:04
  • All you have to do is reassign it to the same string. A piece of code like this should be in a function call, and you should probably do something to the effect of `fTextSearch = CustomEncodeForSQL(fTextSearch);` and simply `return sb.ToString()` from that function. – Jaime Torres Jul 25 '12 at 13:06