471,337 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Replace text in string

I need to replace a value in a string that could be any case:

CREATE
Create
create

And I don't want to change the case of the whole string. Just find the work
"Create" and change it to "ALTER".

I am doing this way at the moment which works:

linePosition = textLine.ToUpper().IndexOf("CREATE");
if (linePosition -1)
{
textLine = textLine.Replace(line.Substring(linePosition, 6), "ALTER");
}

I tried to do:

textLine = textLine.toupper().Replace("CREATE","ALTER");

But that changes the whole line to uppercase.

Is there a better way to do this?

Thanks,

Tom
Aug 22 '08 #1
3 1885
On Thu, 21 Aug 2008 19:04:08 -0700, tshad <tf*@dslextreme.comwrote:
I need to replace a value in a string that could be any case:

CREATE
Create
create

And I don't want to change the case of the whole string. Just find the
work
"Create" and change it to "ALTER".

I am doing this way at the moment which works:

linePosition = textLine.ToUpper().IndexOf("CREATE");
if (linePosition -1)
{
textLine = textLine.Replace(line.Substring(linePosition, 6),
"ALTER");
}
Assuming you know ahead of time the word will appear only once at most, I
think the above is fine. You could probably use the Regex class to do the
case-insensitive search for you (though I don't know the exact format off
the top of my head), but that seems like overkill for the purpose.

Pete
Aug 22 '08 #2
tshad wrote:
I need to replace a value in a string that could be any case:

CREATE
Create
create

And I don't want to change the case of the whole string. Just find the work
"Create" and change it to "ALTER".

I am doing this way at the moment which works:

linePosition = textLine.ToUpper().IndexOf("CREATE");
if (linePosition -1)
{
textLine = textLine.Replace(line.Substring(linePosition, 6), "ALTER");
}

I tried to do:

textLine = textLine.toupper().Replace("CREATE","ALTER");

But that changes the whole line to uppercase.

Is there a better way to do this?
s = Regex.Replace(s, "CREATE", "ALTER", RegexOptions.IgnoreCase);

is simple and readable.

Arne
Aug 22 '08 #3
Yes, using a regex like
textLine = Regex.Replace(textLine, "CREATE", "ALTER",
RegexOptions.IgnoreCase);
(just like Arne wrote it!)
If that doesn't work, change the CREATE and ALTER in the regex
statement to lowercase.

textLine = textLine.toupper().Replace("CREATE","ALTER");
doesn't work because it's saying that textline equals Upper Case
textline, but with CREATE replaced with ALTER as well.

I recommend using the regex statement, but if you're not up to it,
what you already have is fine.
Oh, yeah, and don't forget to reference System.Text.RegularExpressions
for the regex stuff to work.

-Maximz2005

P.S. You should definetly learn to write Regex statements. There are
lots of programs out there that simplify it greatly. Just google for
regex workbench, regex creator, etc.
On Aug 21, 7:13*pm, Arne Vajhøj <a...@vajhoej.dkwrote:
tshad wrote:
I need to replace a value in a string that could be any case:
CREATE
Create
create
And I don't want to change the case of the whole string. *Just find the work
"Create" and change it to "ALTER".
I am doing this way at the moment which works:
*linePosition = textLine.ToUpper().IndexOf("CREATE");
*if (linePosition -1)
*{
* * *textLine = textLine.Replace(line.Substring(linePosition, 6), "ALTER");
*}
I tried to do:
textLine = textLine.toupper().Replace("CREATE","ALTER");
But that changes the whole line to uppercase.
Is there a better way to do this?

s = Regex.Replace(s, "CREATE", "ALTER", RegexOptions.IgnoreCase);

is simple and readable.

Arne
Aug 22 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by beliavsky | last post: by
5 posts views Thread by K.Simon | last post: by
22 posts views Thread by Phlip | last post: by
5 posts views Thread by djc | last post: by
1 post views Thread by Random Task | last post: by
3 posts views Thread by Hvid Hat | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.