JJ.
JJ.

Reputation: 9950

How do I do multiple replaces on a string at the same time?

string rawConnString = Properties.Settings.Default.ConnectionString;

I want to do this with "rawConnString":

rawConnString.Replace("<<DATA_SOURCE>>", server);
rawConnString.Replace("<<INITIAL_CATALOG>>", "tempdb");

Then set the final string to variable finalConnString.

How would I go about doing this?

This is ASP .NET 4.0/C# btw.

Upvotes: 0

Views: 96

Answers (6)

COLD TOLD
COLD TOLD

Reputation: 13579

you mean something like this

string tempString=rawConnString.Replace("<<DATA_SOURCE>>", server);
sting finalstring=tempString.Replace("<<INITIAL_CATALOG>>", "tempdb");

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726509

To avoid using two back-to-back calls of Replace, you can use regular expressions. However, this is far less readable than the original:

string connString = Regex.Replace(
    rawConnString
,   "(<<DATA_SOURCE>>)|(<<INITIAL_CATALOG>>)"
,   m => m.Groups[1].Success ? server : "tempdb"
);

Link to ideone.

Upvotes: 1

Jay
Jay

Reputation: 27474

First, String.replace does not change the original string: it creates a new string. So you have to assign the return value to something. So the logically simplest thing to do is:

finalstring=rawConnString.Replace("<<DATA_SOURCE>>", server);  
finalstring=finalstring.Replace("<<INITIAL_CATALOG>>", "tempdb");

Note that for the second replace, you want to start with the results of the first replace, not the original string.

As String.replace returns a string, and String.replace takes a string, you might find it easier to run them together:

finalstring=rawConnString.Replace("<<DATA_SOURCE>>", server).Replace("<<INITIAL_CATALOG>>", "tempdb");

Upvotes: 0

KeithS
KeithS

Reputation: 71565

This is frankly trivial; you have 90% of the code you need:

string rawConnString = Properties.Settings.Default.ConnectionString;

string finalConnString = rawConnString
                            .Replace("<<DATA_SOURCE>>", server)
                            .Replace("<<INITIAL_CATALOG>>", "tempdb");

Upvotes: 1

David
David

Reputation: 73564

string finalString = Properties.Settings.Default.ConnectionString.Replace("<<DATA_SOURCE>>", server).Replace("<<INITIAL_CATALOG>>", "tempdb"); 

will do it all in one line of code. But it's uglier IMO because you'll have to scroll. The code in your question seems a LOT cleaner and more readable to me.

And doing it in one line of code won't help your performance at all. It should all compile down to the same MSIL either way. I'd leave it as you had it.

Upvotes: 2

McGarnagle
McGarnagle

Reputation: 102743

Not sure if this is what you're after, but you can chain them:

var finalConnString = rawConnString.Replace("<<DATA_SOURCE>>", server)
    .Replace("<<INITIAL_CATALOG>>", "tempdb");

If you're looking to do it with a single method call, I don't think there's anything native to .NET. You can always create an extension method though. Here's a performance-conscious ReplaceMany implementation (signature .ReplaceMany(string[] oldValues, string[] newValues)).

Upvotes: 1

Related Questions