Using the previous linked duplicate, i've modified it to specify where to start the search from. I don't know if this is what you were looking for:
string ReplaceFirst(string text, string search, string replace, string from)
{
int posFrom= text.ToLower().IndexOf(from.ToLower());
if (posFrom < 0)
{
return text;
}
int pos = text.ToLower().IndexOf(search.ToLower(),posFrom);
if (pos < 0)
{
return text;
}
return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
}
Usage:
string tempString1 = ReplaceFirst(tempString, "brands", "tempbrands ", "FROM");
Edit
To replace the string between two strings you may modify the previous method like the following, but note the using just a letter as a limit is not possible. For example, i you use "t" as a limit as you ask in the comment, if the table name contains a "t" it's not going to work. You'd rather use "t0":
string ReplaceFirst(string text, string search, string replace, string from, string to)
{
int posFrom = text.ToLower().IndexOf(from.ToLower());
if (posFrom < 0)
{
return text;
}
int posTo = text.ToLower().IndexOf(to.ToLower(),posFrom);
if (posTo < 0)
{
return text;
}
int pos = text.ToLower().IndexOf(search.ToLower(), posFrom);
if (pos < 0 ||pos >posTo)
{
return text;
}
return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
}
Usage:
string tempString1 = ReplaceFirst(tempString, "brands", "tempbrands ", "FROM","t0");