1

I have a string,

string ij = "/alwaysSame09102012/myThing.aspx?asdasd=99&Urasdl=scashdasdeasdmeasds/tasdigaesdr1/gasdoasdveasdasdrnaasdancasde/eamsdeasdetiasdasdnagsds/tasidgeasdr1masdeetasdasd11180,/reasdMeasdetMe2as0d1asd0/asrdganasdiseasdasdgeasdetasdiasdngaasdsd.aasdspafsxasdffas?asdsdlaieasdnedtfe=asdsafaser1meafswedfhfdget111ertert80"

Now i just need to change the first "alwaysSame09102012" with "always2013forever".

I know i can do something like this,

string ij = "/alwaysSame09102012/myThing.aspx?asdasd=99&Urasdl=scashdasdeasdmeasds/tasdigaesdr1/gasdoasdveasdasdrnaasdancasde/eamsdeasdetiasdasdnagsds/tasidgeasdr1masdeetasdasd11180,/reasdMeasdetMe2as0d1asd0/asrdganasdiseasdasdgeasdetasdiasdngaasdsd.aasdspafsxasdffas?asdsdlaieasdnedtfe=asdsafaser1meafswedfhfdget111ertert80"

string[] c = ij.split['/'];

string finalString =  ij.replace( "/" + c[0] + "/", "/" + "always2013forever" + "/");

This is my logic but no working, please help,

only constant in my string is "/alwaysSame09102012/" which i need to replace

Update

**

  • What if I got this "alwaysSame09102012" in at of my query string, that's why I don't want to use replace.

**

Mathematics
  • 7,314
  • 25
  • 77
  • 152
  • 2
    What about `string finalString = ij.Replace("alwaysSame09102012","always2013forever");` ? – Steve B May 31 '13 at 14:54
  • 1
    what's wrong with `string.Replace` or do you not know about that? – Chris May 31 '13 at 14:54
  • 2
    _but no working_ -- you should also include the expected _and_ actual output. – Austin Salonen May 31 '13 at 14:54
  • and why did you go to all the trouble of splitting the string to get c[0] if it is constant? – Chris May 31 '13 at 14:58
  • thanks but i can have "alwaysSame09102012" in my query string 2 times, how would i make sure it's first one i need to replace. also it's bank software if i don't program it properly ATM machine will not work properly and you will get less money. – Mathematics May 31 '13 at 15:11

4 Answers4

5

Use String.Replace.

Ex:

var goodStr = ij.Replace("alwaysSame09102012", "always2013forever");

The reason your answer does not work is because c[0] is going to be "". The value you are looking for (e.g. 'alwaysSame09102012') is going to be in c[1].

Mohammed Hossain
  • 1,319
  • 10
  • 19
  • thanks but what if i got alwaysSame09102012 twice in string – Mathematics May 31 '13 at 15:11
  • Check the accepted answer for this question (it is essentially what you need!): http://stackoverflow.com/questions/2201595/c-sharp-simplest-way-to-remove-first-occurance-of-a-substring-from-another-str – Mohammed Hossain May 31 '13 at 15:13
  • Here, this is what you want: http://stackoverflow.com/questions/141045/how-do-i-replace-the-first-instance-of-a-string-in-net – Mohammed Hossain May 31 '13 at 15:18
1
string ij = "/alwaysSame09102012/myThing.aspx?asdasd=99&Urasdl=scashdasdeasdmeasds/tasdigaesdr1/gasdoasdveasdasdrnaasdancasde/eamsdeasdetiasdasdnagsds/tasidgeasdr1masdeetasdasd11180,/reasdMeasdetMe2as0d1asd0/asrdganasdiseasdasdgeasdetasdiasdngaasdsd.aasdspafsxasdffas?asdsdlaieasdnedtfe=asdsafaser1meafswedfhfdget111ertert80"

string newString = ij.Replace("alwaysSame09102012","always2013forever");
Kurubaran
  • 8,696
  • 5
  • 43
  • 65
1
string ReplaceFirst (string source, string old_substring, string new_substring)
{
    var position = source.IndexOf(old_substring);
    return (position < 0)
        ? source
        : source.Substring(0, position) + new_substring + source.Substring(position + old_substring.Length);
}

Usage:

var new_string = ReplaceFirst("/alwaysSame09102012/myThing...", "alwaysSame09102012","always2013forever");
Vercetti
  • 437
  • 1
  • 6
  • 17
0

You should use the URI classes.

http://msdn.microsoft.com/en-us/library/system.uri.aspx http://msdn.microsoft.com/en-us/library/system.uribuilder.aspx

This will give you more flexibility, and prevent you from escaping problems etc.

feathj
  • 3,019
  • 2
  • 22
  • 22