-3

I'm a beginner in C# and I have the following string,

string url = "svn1/dev";

along with,

string urlMod = "ato-svn3-sslv3.of.lan/svn/dev"

I want to replace svn1 in url with "ato-svn3-sslv3.of.lan"

Dmitry
  • 13,797
  • 6
  • 32
  • 48
Andre
  • 363
  • 3
  • 15
  • Is it always `svn1` you want to replace? Or all characters before the first `/`? – germi Apr 17 '14 at 13:50
  • What have you tried until now? Replacing strings is something google will help you a lot. Did you run into any problems? – kat0r Apr 17 '14 at 13:50
  • @germi yea its always svn1. Kindly note that I'm having http in first url, and https in the urlMod that is appended to both – Andre Apr 17 '14 at 13:51
  • It smells like a string is immutable understanding. THere are plenty of Replace methods on string but you have to assign the result to a variable to get the change string. – kenny Apr 17 '14 at 13:52
  • I agree with kat0r, I suggest, being new to C# you learn now how to google the answer to problems like this. It will help you much more than just giving you the answer – dferraro Apr 17 '14 at 13:52
  • @Kenny, there are a lot of answers that show a lot of people who has high rank, don't understand also that string is immutable – Andre Apr 17 '14 at 13:55
  • "don't understand also that string is immutable" @Andre take a look at the link in my answer – jltrem Apr 17 '14 at 13:58

4 Answers4

3

Although your question still has some inconsistent statements, I believe String.Replace is what you are looking for:

http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx

url = url.Replace("svn1","ato-svn3-sslv3.of.lan");
Andrew
  • 3,648
  • 1
  • 15
  • 29
1

You can use the string method replace.

url = url.Replace("svn1", urlMod)
Honorable Chow
  • 3,097
  • 3
  • 22
  • 22
  • 1
    You'll have to assign that to a variable - `Replace` returns a new `string`, it doesn't alter the one you call it from. – germi Apr 17 '14 at 13:52
1

Strings are immutable so you need to assign the return value to a variable:

string replacement = "ato-svn3-sslv3.of.lan";
url = url.Replace("svn1", replacement);
Community
  • 1
  • 1
jltrem
  • 12,124
  • 4
  • 40
  • 50
0

I think you need this:

        string url = "svn1/dev";
        string anotherUrl = "ato-svn3-sslv3.of.lan/svn/dev";
        string toBeReplaced = anotherUrl.Split('/')[0];
        url = url.Replace("svn1", toBeReplaced);

It uses split method and replace method.

Amit Joki
  • 58,320
  • 7
  • 77
  • 95