Remove the value from string

Posted by hareeshkumarkr-16190 under ASP.NET on 9/27/2013 | Points: 10 | Views : 3423 | Status : [Member] | Replies : 8
Hi All,

How to remove the value from string?

e.g : s1=2,3,4,5;

s2=4;

result = 2,3,5;


Regards,
Hareesh




Responses

Posted by: Bandi on: 9/27/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
string myString = sourceString.Remove(sourceString.IndexOf(removeString),removeString.Length+1);

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

hareeshkumarkr-16190, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/27/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
As per your s1, s2 strings
string myString = s1.Remove(s1.IndexOf(s2),s2.Length+1);

It will remove only one occurrence..

Refer
http://www.dotnetperls.com/remove
http://msdn.microsoft.com/en-us/library/ms228599.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

hareeshkumarkr-16190, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/27/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
You can use the below code:-

string s1 = "2,3,4,5";
string s2 = "4";
string newone = str.Replace(s2 + ",", "");


Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

hareeshkumarkr-16190, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/27/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
You can also use the below code:-

string s1 = "2,3,4,5";

string s2 = "4";
List<string> result = s1.Split(new char[] { ',' }).ToList();
result.Remove(s2);
string output = string.Join(",", result.ToArray());


Happy Coding,
If it helps you or directs U towards the solution, MARK IT AS ANSWER

hareeshkumarkr-16190, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/27/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Two approaches:
1) string myString = s1.Remove(s1.IndexOf(s2),s2.Length+1); 

2) string myString = s1.Replace(s2 + ",", "");

the above are easy approaches....
The approach using List and Remove is the lengthy process

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

hareeshkumarkr-16190, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/27/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
"Mark As Answer"

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

hareeshkumarkr-16190, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nismeh on: 9/27/2013 [Member] Starter | Points: 25

Up
0
Down
You can simply use
myString == string.Empty

IT KNOWLEDGE IS APPLIED KNOWLEDGE
So Just Do It

hareeshkumarkr-16190, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 10/24/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
"Mark it as Answer" if you solved the issue...

It might help others who have similar issue

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

hareeshkumarkr-16190, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response