-2

The below string is coming from a DIV tag. So I have enclosed the value below.

String cLocation = "'target="_blank'></a><img alt='testimage.jpg' src='/SPECIMAGE/testimage.jpg'"

I would like to replace in the above string by changing "src="/" with "src='xyz/files'".

I have tried the typical string.Replace("old","new") but it didn't work.

I tried the below,

cNewLocation ="xyz/files";
cNewString = cLocation.Replce("src='/'", "src='" + cNewLocation + "'/")

It didn't work.

Please suggest.

Anirudh
  • 581
  • 5
  • 14
  • 32
  • That is not a valid string literal in C#. Please post actual code. – Oded Nov 15 '11 at 22:06
  • 3
    Your question title contains a key to answer itself, have you tried google or look into the auto-suggested posts on StackOverflow whilst posting an answer? – sll Nov 15 '11 at 22:08
  • 4
    @sll - Googling would require effort, you know, clicking links and reading documentation. Much too difficult. – Oded Nov 15 '11 at 22:09
  • I could not complete the post and it saved and posted. Read above comments. – Anirudh Nov 15 '11 at 22:17
  • Well it did not work because you have no `"src='/'"` in your string. You have `src='/SPECIMAGE/testimage.jpg'`, and that's different. – Otiel Nov 15 '11 at 22:23
  • thanks for voting it down but whoever did that should read it fully and then do it. – Anirudh Nov 15 '11 at 22:24
  • Votes down have been made when your only question was "*how to replace a String*". Now *@CAbbott*'s solution might be the one you want if I understand right your mistake. – Otiel Nov 15 '11 at 22:25
  • @Otiel -> I enclosed / withink '/' after it gave errors. – Anirudh Nov 15 '11 at 22:26
  • 2
    Your question is not clear. Please write *exactly* the input string you have, and *exactly* the output string you want. – Otiel Nov 15 '11 at 22:28

4 Answers4

2

you might try looking at the Replace command in c#.

so mystring = srcstring.Replace("old", "New");

http://msdn.microsoft.com/en-us/library/system.string.replace%28v=vs.71%29.aspx

possible replace the / in the string with //?

griegs
  • 22,624
  • 33
  • 128
  • 205
2

If I'm understanding what you're asking, you could use Regex to replace the string like so:

var cNewString = Regex.Replace(cLocation, @"src='/.*/", "src='" + newLocation + "/");

EDIT : I modified the regular expression to replace src='/.../ with src='{newLocation}/

CAbbott
  • 8,078
  • 4
  • 31
  • 38
1

You can do the following:

string cLocation = "'target='_blank'></a><img alt='testimage.jpg' src='/SPECIMAGE/testimage.jpg'";
cLocation = cLocation.Replace("src='/'", "src='xyz/files'");
LarsTech
  • 80,625
  • 14
  • 153
  • 225
ahashki
  • 21
  • 2
  • There's a stray apostrophe in there, by the looks of it. Should probably be `cLocation = cLocation.Replace("src='/", "src='xyz/files/");` – Carson63000 Nov 16 '11 at 02:24
1

This fixes the problem:

int start = cLocation.IndexOf("src='") + 5;
int end = cLocation.LastIndexOf("'");
string xcLocation = cLocation.Remove(start, end - start);
string cLocation = xcLocation.Insert(start , "xyz/files");
kiritsuku
  • 52,967
  • 18
  • 114
  • 136
ahashki
  • 21
  • 2