c# replace character by an empty string [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'm getting a string from a Json :
var value = JsonObject["price"]; //value = "1,560";
i'm trying to replace the ',' with an empty string :
value.Replace(",",string.Empty);
but i'm still getting the value with "," that's so strange and i'm stuck at it
thanks in advance

value = value.Replace( ", ", string.Empty);
strings in .net are immutable.

Per the documentation for String.Replace:
Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
It gives you a new string; it doesn't modify the existing one. So you need to assign the result to a variable:
value = value.Replace(",", string.Empty);

Related

Remove strings till a char is recognized [duplicate]

This question already has answers here:
How to remove a defined part of a string?
(8 answers)
Closed 1 year ago.
I am trying to delete parts of the string (first 10 chars) so that I get the serial code of the string without any extra chars. Now, the serial code will always begin after the ":" colon char. So is there a way to specify to delete strings from ":" and before that so that only remaining string would be the serial key?
for example;
string is "MySerials:12e42-23w6z-23w-a23"
final string must be "12e42-23w6z-23w-a23"
I am deleting the strings manually;
public string myStr;
public void Start () {
myStr = myStr.Substring (10, myStr.Length - 10);
Debug.Log (myStr);
}
I would use the string split function like so:
var teststring = "MySerials:12e42-23w6z-23w-a23";
var split = teststring.Split(':');
Console.WriteLine(split[1]);
Instead of splitting the string you could look for the first occurrence of ':' and get your result directly:
var input = "MySerials:12e42-23w6z-23w-a23";
var result = input.Substring(input.IndexOf(':') + 1);

Error when trying to do string var = "\" [duplicate]

This question already has answers here:
New Line in Constant error when trying to insert escape character in string
(3 answers)
Closed 4 years ago.
I am trying to do a Path.Combine. One string is the path and the other is just a slash.
string ok = browser.SelectedPath;
string okie = "\";
string pathy = Path.Combine(ok, okie);
Settings.Default["Path"] = pathy;
for
string okie = "\";
I get two errors NewLine in constant. Anyone know how to fix this?
Thank you!
Get rid of okie and pathy, and assign Settings.Default["Path"] the browser.SelectedPath, eg
Settings.Default["Path"] = browser.SelectedPath;

c# - How can i format a string in c# [duplicate]

This question already has answers here:
Add zero-padding to a string
(6 answers)
Closed 4 years ago.
I have a very simple Question to ask.
I have a string like:
string str="89";
I want to format my string as follow :
str="000089";
How can i achieve this?
Assuming the 89 is actually coming from another variable, then simply:
int i = 89;
var str = i.ToString("000000");
Here the 0 in the ToString() is a "zero placeholder" as a custom format specifier; see https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings
If you have a string (not int) as the initial value and thus you want to pad it up to length 6, try PadLeft:
string str = "89";
str = str.PadLeft(6, '0');
If you want the input to be a string you'll have to parse it before you output it
int.Parse(str).ToString("000000")

c# How can i replace backslash in a string? [duplicate]

This question already has answers here:
How do I replace a backslash-double quote with just a double quote?
(3 answers)
Closed 5 years ago.
I am working on a c# project and have some strings like this
first string
"[\"2018\\/02\\/12\",[\"Test1\",\"Test2\",\"Test3\",\"Test4\"]]"
But this string format is not not suitable for my application. I want to change first string to this :
second string
2018-02-12,"Test1","Test2","Test3","Test4"
I've done some of it, but I'm having trouble getting a backslash. Actually backslash did not changed.
my code :
string MyString = "[\"2018\\/02\\/12\",[\"Test1\",\"Test2\",\"Test3\",\"Test4\"]]";
MyString = MyString.Replace("[", "").Replace("]", "").Replace("\\", "");
How can I get the second string?
Use the following code:
MyString.Replace("[", string.Empty).Replace("]", string.Empty).Replace("\\", string.Empty).Replace(#"\", string.Empty).Replace("/", "-");
And view the result in Text Visualizer.

string capturing before / symboal [duplicate]

This question already has answers here:
Get Substring - everything before certain char
(9 answers)
Closed 8 years ago.
Lets say I have a string:
string a = "abc&dcg / foo / oiu";
now i would like the output to be
"abc&dcg"
i have tried:
string output= a.Substring(a.IndexOf('/'));
but it returns the last part not the first part
I have tried trim() as well, but doesn't provide me with the results.
Try this:
string result = a.Split('/')[0].Trim();
The split operation will give you the 3 substrings separated by '/' and you can choose whichever ones you want by specifying the index.
Try this one
string a = "abc&dcg / foo / oiu";
string output = a.Substring(0, a.IndexOf("/"));
Console.WriteLine(output);
It will show
abc&dcg
Try
string output;
if (a.IndexOf('/')>=0) { output = a.Split('/')[0].Trim() };
This wil prevents error case a doesn't contains any /

Categories