How to insert a value into a string in certain C # positions

I have a program that gets a string from a method. I want to know how to insert a string value into this string in 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 each occurrence of the character ")" in mystring?

PS. If possible, also indicate how not to insert it directly at the end.

+3
source share
5 answers

You can accomplish this with a replacement.

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

Result:

"column1 in('a','b') and column2 in('c','d') and column3 in('e','f')"
+2
source

Perhaps the simplest:

mystring = mystring.Replace(")", ") and ");
mystring = mystring.Substring(0, mystring.Length - " and ".Length);
+5
source

, "" . , , .

, , :

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 ());

, , ...

, ( where sql) ?

+2

, :

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

:

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

:

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

, "".

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

.+$ , . , Regex .

// Do this once somewhere:
System.Text.RegularExpressions.Regex insertAndPattern =
    new System.Text.RegularExpressions.Regex("\\)(?=.+$)");

// And later:
insertAndPattern.Replace(mystring, ") and ");

: , . "\\).+$" "\\)(?=.+$)", .+$ ( ) .

0

Source: https://habr.com/ru/post/1704675/


All Articles