Click here to Skip to main content
15,911,139 members
Please Sign up or sign in to vote.
1.67/5 (2 votes)
See more:
My Code is:
string input = "8,16,9,18,19";
string output = input.Replace("8","");

I want the output like this:

output="16,9,18,19";

Any Help would be appreciated and please dont use the Regex method or function

Thanks

What I have tried:

I Tried
string input = "8,16,9,18,19";
string output = input.Replace("8","");

But My Ouput comes like this:
output="16,9,1,19";
Posted
Updated 3-Aug-17 18:39pm

String.Replace changes all matching values, so your "18," matches as well as the one you want.
If you always want to remove the first one, then use a substring with IndexOf:
C#
string input = "8,16,9,18,19";
string output = input.Substring(input.IndexOf(',') + 1);
 
Share this answer
 
Comments
Member 12693375 20-Jul-17 6:43am    
Wrong Answer Sir u can check it practically
string input1 = "8,16,9,18,19";
string output1 = input1.Substring(input1.IndexOf(',') + 2);

It gives me output like this: "6,9,18,19";
OriginalGriff 20-Jul-17 6:48am    
Why did you put "+ 2" when I put "+ 1"?
A regex is the easiest approach:


string input = "test, and test but not testing.  But yes to test";
string pattern = @"\btest\b";
string replace = "text";
string result = Regex.Replace(input, pattern, replace);
Console.WriteLine(result);


The important part of the pattern is the \b metacharacter, which matches on word boundaries. If you need it to be case-insensitive use RegexOptions.IgnoreCase:


Regex.Replace(input, pattern, replace, RegexOptions.IgnoreCase);
 
Share this answer
 
string input = "8,16,9,18,19";
           string[] items = input.Split(',');
           string replace = "8";
           List<string> lst = new List<string>();
           foreach (string item in items)
               if (item != replace)
                   lst.Add(item);
           string output = string.Join(",", lst);


           // using linq
           string output1 = string.Join(",", input.Split(',').Where(k => k != replace));
 
Share this answer
 
string input = "8,16,9,18,19";

input = string.Join(",", input.Split(new[] { ',' }).Select(i => i.Trim()).Where(i => i != "8"));


The above allows for spaces in the string also, like "8, 16, 9, ...". If you can guarantee there are no spaces you can omit the Trim

string input = "8,16,9,18,19";

input = string.Join(",", input.Split(new[] { ',' }).Where(i => i != "8"));
 
Share this answer
 
Quote:
How to replace numeric value in C#?

You are not working on numeric value, it is a string.

Your code removes all char '8' in the string.
The most simple way to get your output is to get a substring starting at char 2.
C#
string input = "8,16,9,18,19";
string output = input.Substring(2);


Other approaches are possible, including RegEx, as you can see on other solutions. But for that, we need to your exact criteria.
 
Share this answer
 
Use
string out1= obj.Substring(2);

to always remove from starting index. And if you want to remove from anywhere in string, needs a generic method to construct.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900