Monday, April 25, 2011

How to insert a value in a string at certain positions c#

I have a program that gets a string from a method. I want to know how to insert a string value to that string at certain positions.

For example:

mystring = "column1 in('a','b')column2 in('c','d')column3 in('e','f')";

Here, how would I insert the string value " and " after every occurance of the character ')' in mystring?

PS. If possible, also include how not to insert it right at the end.

From stackoverflow
  • Strings are immutable, so you cannot 'just' change the value of that string. Each modification that you want to make to a string, leads to a new instance of a string.

    This is maybe how you could achieve what you want:

    string s = " x in (a, b) y in (c, d) z in (e , f)";
    string[] parts = s.Split (')');
    StringBuilder result = new StringBuilder ();
    foreach( string part in parts )
       result.Append (part + ") and ");
    Console.WriteLine (result.ToString ());

    But maybe there are better solutions ...

    Anyway, how come you receive that string (which looks like a part of a where clause of a sql statement) in that way ?

  • Probably the simplest:

    mystring = mystring.Replace(")", ") and ");
    mystring = mystring.Substring(0, mystring.Length - " and ".Length);
    Frederik Gheysels : That I didn't think of it ... 8)7
  • You could accomplish this with replace..

    string mystring = "column1 in('a','b')column2 in('c','d')column3 in('e','f')";
    mystring = mystring.Replace(")", ") and ").TrimEnd(" and".ToCharArray());

    Resulting In:

    "column1 in('a','b') and column2 in('c','d') and column3 in('e','f')"
    Lucas : be careful with that TrimEnd(), i will trim ANY ' ', 'a', 'n', or 'd' at the end, not just " and".
    Quintin Robinson : Yes I should've mentioned that it works for the case in the question, but there could be unintentional side effects if the end of the string contains any of the characters in the trimend that aren't intended to be removed.
  • If you mean, and I'm taking your string literally and as it comes:

    mystring = "column1 in('a','b')column2 in('c','d')column3 in('e','f')"

    Then you could just do:

    mystring = mystring.Replace(")c", ") and c");

    Which would result in:

    mystring = 
        "column1 in('a','b') and column2 in('c','d') and column3 in('e','f')"

    This is presuming you don't want a trailing "and".


  • System.Text.RegularExpressions.Regex.Replace(
        mystring, "\\)(?=.+$)", ") and ");

    The .+$ portion of the regular expression ensures that the closing parenthesis is not at the end of the line. If you are going to be doing this often, I'd recommend creating and persisting a Regex object for the pattern.

    // Do this once somewhere:
    System.Text.RegularExpressions.Regex insertAndPattern =
        new System.Text.RegularExpressions.Regex("\\)(?=.+$)");
    // And later:
    insertAndPattern.Replace(mystring, ") and ");

    EDIT: Just realized I'm an idiot. Fixed the patterns above from "\\).+$" to "\\)(?=.+$)", so that the .+$ part is not included (and thereby replaced) in the match.

    zohair : Sorry...I have no idea what Regex is and what it does...
    NTulip : So because you don't know what regex is - you're just going to write off his comment? Wow.


Post a Comment