I have an SQL query in a string and I want to replace a part of the string with a new value.
The part in the string I'm looking for is a: until it hits either a, or a ). The part it finds needs to be replaced by a question mark.
I've been working on this for a while but I can't seem to find a solution how to fix this.
Can anyone help me further?
Example String I want to get a part of:
"INSERT INTO ORDERS ( ORDER_ID, CUSTNO, COMPANY_NAME, ITEM, AMOUNT1, AMOUNT2) 
VALUES ( :WS_ORDER_ID, :WS_ORDER_CUSTNO, :WS_ORDER_COMPANY_NAME, :WS_ITEM, :WS_AMOUNT1, :WS_AMOUNT2)"
So what I want the string to look like is:
"INSERT INTO ORDERS ( ORDER_ID, CUSTNO, COMPANY_NAME, ITEM, AMOUNT1, AMOUNT2) 
VALUES ( ?, ?, ?, ?, ?, ?)"
What i was trying to create:
public String Replace(String value, String firstValue, String lastValue, String replacementValue) {
int firstLocation = value.IndexOf(firstValue);
var lastLocation = value.IndexOf(lastValue);
if (value.Contains(firstValue)) {
String modifiedValue;
modifiedValue = value.IndexOf(Convert.ToChar(firstLocation), lastLocation.ToString());
String newValue = value.Replace(modifiedValue, replacementValue);
}
return newValue;
}
Something like this but I can't figure it out.
And then I want to have a foreach loop that takes this method and replaces every:WORD, with a?.
Best regards
A simple method like this should do the trick if ':' is guaranteed to be unique as a token:
public string Replace(string text)
{
string[] items = text.Split(':');
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(items[0]);
for(int i=1;i<items.Length;i++)
{
string item = items[i].TrimEnd();
stringBuilder.Append("?" + item[item.Length - 1]);
}
return stringBuilder.ToString();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.