-2

I have a simple code where I receive data from database:

foreach (DataRow row in tmpDatosModulos.Rows)
{
    tmpBSCID += row["ModuloURL"].ToString();
    tmpBSCID.Replace("../BSC/wf_BSC_Reporte.aspx?BSCID=", "");

}
Convert.ToInt32(tmpBSCID);

First tmpBSCID receive value like: ../BSC/wf_BSC_Reporte.aspx?BSCID=21 now I want to replace it to drop all this part: ../BSC/wf_BSC_Reporte.aspx?BSCID= and get only last digits after =, but when I debug and it pass Replace instrucion it return all value: ../BSC/wf_BSC_Reporte.aspx?BSCID=21 instead of 21. Why it occurs? Regards

Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
Pepe
  • 403
  • 1
  • 5
  • 10
  • 5
    Strings are immutable and hence all string methods return a new string instead of modifying the passed. So you have to asssign the returned string to the original variable. `tmpBSCID = tmpBSCID.Replace(...) ` – Tim Schmelter Nov 23 '17 at 16:10
  • 1
    Off Topic: I think that a better approach is instead of saying "don't work correctly" say "I don't understand how to use the Replace function" – hardkoded Nov 23 '17 at 16:13
  • He doesn't know what he doesn't know. At a guess, if he knew his understanding of strings was flawed, he could have just looked that up. – Paul Michaels Nov 23 '17 at 16:15

1 Answers1

2
tmpBSCID = tmpBSCID.Replace("../BSC/wf_BSC_Reporte.aspx?BSCID=", "");

Methods on .NET strings do not change the string, they return a new string.

nvoigt
  • 75,013
  • 26
  • 93
  • 142