472,958 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1953
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: beliavsky | last post by:
The code for text in open("file.txt","r"): print text.replace("foo","bar") replaces 'foo' with 'bar' in a file, but how do I avoid changing text inside single or double quotes? For making...
5
by: K.Simon | last post by:
Hello, it's very often neccessary to replace strings or a single character in my stylesheets. My solution looks awful and very long. Now i thought to solve this with an array like structure but...
22
by: Phlip | last post by:
C++ers: Here's an open ended STL question. What's the smarmiest most templated way to use <string>, <algorithms> etc. to turn this: " able search baker search charlie " into this: " able...
4
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said,...
5
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able...
1
by: Random Task | last post by:
Can someone help me by providing an example of how to replace / with \ in a string in xslt2. The characters / and \ seem to cause me grief ... I am trying the below code currently ... Any...
3
by: TOXiC | last post by:
Hi everyone, First I say that I serched and tryed everything but I cannot figure out how I can do it. I want to open a a file (not necessary a txt) and find and replace a string. I can do it...
18
by: Umesh | last post by:
Do you have any answer to it? thx.
10
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index...
3
by: Hvid Hat | last post by:
Hi I want to highlight (make it bold) a word in some text I'm getting in XML format. My plan was to replace the word with a bold (or span) tag with the word within the tag. I've found the code...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.