Replace don't works correctly [duplicate] - c#

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.

Related

How do I replace a special character [duplicate]

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

Replace exact matching string [duplicate]

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

Find a set of characters in a string C# class file(.cs) [duplicate]

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
}

How to check if string contains special part [duplicate]

This question already has answers here:
How can I check if a string contains a character in C#?
(8 answers)
Closed 5 years ago.
How I can check if my string has the value ".all" in. Example:
string myString = "Hello.all";
I need to check if myString has .all in order to call other method for this string, any ideas how I can do it?
you could use myString.Contains(".all")
More info here
Use IndexOf()
var s = "Hello.all";
var a = s.IndexOf(".all", StringComparison.OrdinalIgnoreCase);
If a = -1 then no occurrences found
If a = some number other than -1 you get the index (place in the string where it starts).
So a = 5 in this case
Simply call .Contains(".all") on the string object:
if (myString.Contains(".all")
{
// your code to call the other method goes here
}
There is no need for regex to do that.
Optionally, as mentioned by #ZarX in comments, you can check if the string ends with your keyword with .EndsWith(".all"), which will return true if the string ends with your keyword.

Remove string from a string [duplicate]

This question already has answers here:
How to remove a string from a string
(4 answers)
Closed 8 years ago.
I've this kind of string.
FullName:ae876ggfg777878848adgf877
And I want to remove "FullName:", so the output as follows:
ae876ggfg777878848adgf877
How can I do that?
I tried this:
var index = myText.IndexOf(":");
var result = myText.Remove(index);
But the output is like this:
FullName
Which I do not expect.
IndexOf returns the index of whatever string/character you give it, so in your case, the index of :.
Remove, according to the documentation:
Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted.
So what's happening here is you're removing everything after and including the :
You should be using String.Replace:
string removed = myText.Replace("FullName:", "");
Use String.Substring() and get the start index by using String.IndexOf('character') + 1.
string s = "FullName:ae876ggfg777878848adgf877";
Console.WriteLine(s.Substring(s.IndexOf(':')+1));

Categories

Resources