Remove commas from right side of string in c# [duplicate] - c#

This question already has answers here:
Trim last character from a string
(15 answers)
Closed 8 years ago.
I have string like below.
string s="this is item1,item2,item3,,, ,, ,";
now i want to remove (,) from right side of string.
Thanks in advance
i have tried
string.Replace(",", "");
and
string.TrimEnd("'");
but not working

Try this:
s = s.TrimEnd(',', ' ');
I think the problem in your code is that you do not assign the result of your replacement to any variable. And also your solution would remove all , from the string, not only those on the right side.

Try string.TrimEnd():
s= s.TrimEnd(',',' ');

Related

How to delete everything from string after character "/u"? [duplicate]

This question already has answers here:
Removing carriage return and linefeed from the end of a string in C#
(13 answers)
Closed 2 years ago.
Hi i have strange problem. Here is my code:
string tmp = "20_29\u0013";
How can I get rid of that "\u0013"?
Normally I will do it with substring or something but I have some issues with that character ---> \
Could someone help me?
You can use tmp.TrimEnd('\u0013') if it is single char or tmp.Replace("\\u0013", string.Empty) if it is sequence of chars to get "20_29" part:

How to parse this particular string? [duplicate]

This question already has answers here:
split strings into many strings by newline?
(4 answers)
Closed 2 years ago.
I want to parse this string: "2.8\r\n2.52\r\n" to just get 2.8 and 2.52. How can I go about doing something like this? I tried to using .Split() but I'm not sure what to pass as parameters to get what I am looking for.
String.Split should work fine:
string[] res = "2.8\r\n2.52\r\n".Split("\r\n", StringSplitOptions.RemoveEmptyEntries); // array with {"2.8", "2.52"}
You need to split by Environment.NewLine which in here would be \r\n
var result = "2.8\r\n2.52\r\n".Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
Edit:
Environment.NewLine depends on your environment as you can guess so to be on the safe side you can also use the exact string (\r\n) as a delimiter.

Remove empty line from string C# [duplicate]

This question already has answers here:
How to remove empty lines from a formatted string
(11 answers)
Closed 6 years ago.
How can I remove empty line from a string for example:
Turn this:
to This:
I used this code text.Replace("\n",""); but it turns it to:
testtesttes
You should replace two newline characters(if any). Following code will work on Unix as well as non unix platforms.
var stringWithoutEmptyLines = yourString.Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
string data = stringData.Replace("\r\n\r\n", "\r\n");

Replace (,) with (',') [duplicate]

This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 7 years ago.
I want to replace a single comma (,) with (',').
For example:
"text,text,text" with "text','text','text".
I tried
MyText.Replace(',',"','");
but can't get anything working properly.
Any help would be appreciated.
Try:
MyText = MyText.Replace(",","','");
There are two .Replace methods on strings. One for single characters and one for strings.
When you ',' that defines a single character so it goes to the single character version of the method. If you use double quotes then it defines a string so that version of the method is selected.
Docs on the string replace method: https://msdn.microsoft.com/en-us/library/system.string.replace(v=vs.110).aspx

string.replace is not working for quotes [duplicate]

This question already has answers here:
C# string replacement , not working
(6 answers)
Closed 8 years ago.
I have a string like
mukesh "salaria" engineer
how do i replace " with blank
like i want output as
mukesh salaria engineer
I already tried, str.replace("\"",string.empty);
but it doesn't working for me.
It works but must type this: str = str.replace(...)
string.Replace is a pure method(no side-effects). If you do not assign str.replace("\"",string.empty); to anything, then that statement does not make any change in the state of your object(in other words writing that line of code is equal to not writing it at all.).
Use str = str.Replace(...);
You must use the Replace method and get the string back from it. Your code must be:
str = str.Replace("\"", string.Empty);
msdn :
Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
try this
str.Replace('\"',' ');

Categories

Resources