This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 6 years ago.
I am attempting to replace characters in a string data type. Below is my code, but the ' is not being replaced with ''. Am I mis-understanding how the replace() function operates? What should I do in order to change the string to High Flying Picture''s?
This is the syntax I attempted.
public static void Test()
{
string strVar = "High Flying Picture's";
strVar.Replace("'", "''");
Console.WriteLine(strVar);
}
strVar = strVar.Replace("'", "''");
You need to reassign it.
Related
This question already has answers here:
string.Replace (or other string modification) not working
(4 answers)
Closed 12 months ago.
I'm trying to replace a special character "½" with ".5"
cell.InnerText="o208½-105u208½-109o208-110u209-110";
string tempStr=cell.InnerText;
if (cell.InnerText.Contains("½"))
{
cell.InnerText.Replace("½", ".5");
}
string tempStr1 = cell.InnerText;
but my C# .Replace isn't working , I get the same result.
String is an immutable type. Compiler creates a new string after replacing. So try just this
var innerText = "o208½-105u208½-109o208-110u209-110";
innerText= innerText.Replace("½", ".5");
result
before - o208½-105u208½-109o208-110u209-110
after - o208.5-105u208.5-109o208-110u209-110
This question already has answers here:
C# string replace
(9 answers)
Closed 3 years ago.
I want to dynamically replace an exact sub string from a string. For ex I want to replace "dis.[Test Table]" to "dbo.[Test Table]" in the following string:
SELECT Name FROM dis.[Test Table].
Note that I it could be any text and any sub string. It will replace only exactly matched sub string as many times as it will occur.
use String.Replace() Method
fullString = fullString.Replace("oldSubString", "newSubString");
Just to elaborate further, your example can be written as:
var newString = "SELECT Name FROM dis.[Test Table]".Replace("dis.[Test Table]","dbo.[Test Table]");
// newString will become "SELECT Name FROM dbo.[Test Table]"
More details are here
This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 5 years ago.
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
tmpBSCID = tmpBSCID.Replace("../BSC/wf_BSC_Reporte.aspx?BSCID=", "");
Methods on .NET strings do not change the string, they return a new string.
This question already has answers here:
How can I check if a string exists in another string
(10 answers)
Closed 5 years ago.
I'm working on a Existing Class file(.cs) which fetches a string with some data in it.
I need to check if the string contains a word. String has no blank spaces in it.
The string-
"<t>StartTxn</t><l>0</l><s>0</s><u>1</u><r>0</r><g>1</g><t>ReleaseUserAuthPending</t>"
I need to check if the string contains 'ReleaseUserAuthPending' in it.
You can try this:
var strValue = "<t>StartTxn</t><l>0</l><s>0</s><u>1</u><r>0</r><g>1</g><t>ReleaseUserAuthPending</t>";
if (strValue.Contains("ReleaseUserAuthPending"))
{
//Do stuff
}
Refer About String - Contains function
For your information: Contains function is case-sensitive. If you want to make this Contains function as case-insensitive. Do the following step from this link.
bool containsString = mystring.Contains("ReleaseUserAuthPending");
Try
String yourString = "<t>StartTxn</t><l>0</l><s>0</s><u>1</u><r>0</r><g>1</g><t>ReleaseUserAuthPending</t>";
if(yourString.Contains("ReleaseUserAuthPending")){
//contains ReleaseUserAuthPending
}else{
//does not contain ReleaseUserAuthPending
}
This question already has answers here:
Why is \ character being doubled in my # string?
(3 answers)
Closed 8 years ago.
I have a small piece of code:
public static void Write(string filename){
string time = DateTime.Now.ToString("hh:mm tt");
int date = int.Parse(DateTime.Now.ToString("yyyyMMdd"));
string path = #"C:\Users\Public\" + filename;
}
If I debug and stop just after path is set it looks like “C:\\\Users\\\Public\\\filename.txt”.
Can anyone tell me why it has the double slashes? Is the '#' sign actually messing it up in this case?
How I may get it as “C:\Users\Public\filename.txt”
The double slashes are only there for the debugger, not your actual application. Your code is fine.