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?
mystring
PS. If possible, also indicate how not to insert it directly at the end.
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')"
Perhaps the simplest:
mystring = mystring.Replace(")", ") and "); mystring = mystring.Substring(0, mystring.Length - " and ".Length);
, "" . , , .
, , :
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) ?
, :
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')"
, "".
System.Text.RegularExpressions.Regex.Replace( mystring, "\\)(?=.+$)", ") and ");
.+$ , . , Regex .
.+$
Regex
// Do this once somewhere: System.Text.RegularExpressions.Regex insertAndPattern = new System.Text.RegularExpressions.Regex("\\)(?=.+$)"); // And later: insertAndPattern.Replace(mystring, ") and ");
: , . "\\).+$" "\\)(?=.+$)", .+$ ( ) .
"\\).+$"
"\\)(?=.+$)"
Source: https://habr.com/ru/post/1704675/More articles:Decrypt C # string - securityhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1704671/alerting-library-users-to-an-inconsistent-state&usg=ALkJrhhy1_ez6x4FCdBKCXNTNfz9fTI0mASQL 2005 instance will not work with DNS - sql-serverIs there a way to get notified when a child is added / removed from the WPF panel? - wpfHow to create an Excel worksheet through VB6 without an Excel object? - excel-vbaCorrect treeview CSS output? Test adapters? ASP.NET 3.5 - cssHow to make textarea draggable? (ie block text selection, but allow cursor positioning) - jquery-uiWhat web video formats can do this? - htmlSystem.UnauthorizedAccessException error in mscorwks.dll causing the application to crash - debuggingwhat is the best place for me to put nunit tests for a library that should be used from both ASP.Net and WinForms - unit-testingAll Articles