Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi i have no idea on how to remove characters from a string i.e. Ex: i have string like
Raghava(24) i want to remove (24) how to do this please give me solution.
Posted
Comments
Sergey Alexandrovich Kryukov 5-Mar-12 21:55pm    
Not really a question. "I have no idea" is not informative.

What's your real problem? Broken Google? Bing? MSDN?
--SA

Strictly speaking, you can never remove anything from a string. You cannot add anything to a string. Strings are immutable.

Look at the answers you already had. In all cases, the string is never modified. Instead, a brand-new string is created based on old string; and the variable on left side is assigned a new value. In this operation, the reference pointed by a variable is changed. It has a serious impact on the performance: all the data is moved left to write, with modification, and then assigned right to left.

That's why such operations should be used with care. For example, multiple concatenation (+) is string is really bad as (ever growing) data goes back and forth. The method string.Format does not present such problem. In general case, the mutable class System.Text.StringBuilder should be used.

Please see:
http://msdn.microsoft.com/en-us/library/system.string.aspx[^],
http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx[^].

—SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Mar-12 0:56am    
A vote of 1? Ha-ha, someone is way too naive...
--SA
Varun Sareen 8-Mar-12 22:54pm    
my 5: good explanation :)
Sergey Alexandrovich Kryukov 8-Mar-12 23:13pm    
Thank you, Sareen.
--SA
Try this:
C#
string str = "ht12rt";
string newstr= string.Empty;
int index = 0;
foreach(char c in str)
{
 try
  {
    int i = (int)c;
    if((i>48)&&(i<57))
    {
      continue;
    }
  }
 catch(Exception ex)
 {

 }
 newstr += c.ToString();
}
 
Share this answer
 
Comments
Varun Sareen 8-Mar-12 22:54pm    
my 5: nice alternative
Prasad_Kulkarni 9-Mar-12 0:00am    
Thank you Varun
string s1 = "Raghava(24)";
string s2 = s1.Replace("24", String.Empty);

Hope this helps,
Fredrik
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Mar-12 21:53pm    
Not so simple: you fail to explain that the strings are immutable, so you never remove anything from a string, and it's important, in particular, from the performance standpoint.

Please see my answer.
--SA
Fredrik Bornander 6-Mar-12 5:35am    
You assume that by "string" OP means the type string, I assume that he means the concept of a string, from which characters can be removed.
Sergey Alexandrovich Kryukov 7-Mar-12 0:56am    
Yes, correct, I assume the same.
Now, listen carefully: those strings are immutable. It is impossible to modify a string a string, add or remove anything. Instead, you create a brand new string (actually, implementation can take in in the intern pool, did you hear about it?) based on original string. None of your example modified the string object. Even if you used

string s1 = s1.Replace("24", String.Empty);

(without string s2), it would remain true. You create a brand new object and then assign it to a variable. The difference: the object looses its referential identity: it becomes a new reference. This is easy to check up using object.ReferenceEquals. Actually, System.String is a very complex class, it uses interning mechanism, intern pool and implements value-like semantic being in fact a real reference type, a class.

Unfortunately, too few .NET developers truly understand the nature of this class. And this is one of the purposes of it.

Now, did you finally get it, or I need to explain it more?

--SA
Fredrik Bornander 7-Mar-12 2:30am    
I know perfectly well what an immutable string is. What I meant was that "string" does not have to refer to the type System.String. It can mean the concept of a string. And strings can be concatenated, truncated, split or even have their content replaced, it has nothing to do with the .NET details of how System.String is implemented. I think it was clear from OPs question that he had one value and wanted another one based on the first, I don't think he was asking how to mutate a System.String objects internal representation.

Now, did you finally get it, or I need to explain it more?

Sergey Alexandrovich Kryukov 7-Mar-12 19:06pm    
Oh yes, I finally get it, thank you for your patience and the explanations. And I'm grad to hear that you know how it works. I think you mean that conceptually it looks like strings are mutable. But who would argue with that. Nevertheless, my answer is more precise. I explain that you finally get a modified string the way you and other show, but I also explain what happens and that the mutation does not happen. The lack of mutation perfectly observable, not just affects performance. So, the knowledge on the string behavior is not just implementation detail or internal representation. An attempt to proof that it does not matter does not look productive to me...

Thank you.
--SA
--SA
You can use below provided code for removing a string from a string

static void Main(string[] args)
{
    string str = "Raghava(24)";
    string strToRemove = "(24)";
    str = str.Replace(strToRemove, String.Empty);
    Console.Write(str);
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Mar-12 21:53pm    
Not so simple: you fail to explain that the strings are immutable, so you never remove anything from a string, and it's important, in particular, from the performance standpoint.

Please see my answer.
--SA
Dear Friend,

This link will give you clear insight into your problem:-

http://www.dotnetperls.com/remove

Please don't forget to mark this as your answer if it helps you out.

Thanks
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Mar-12 21:52pm    
This is not enough, in a very essential way: both you and the referenced article fail to explain that the strings are immutable, so you never remove anything from a string, and it's important, in particular, from the performance standpoint.

Please see my answer.
--SA
Varun Sareen 8-Mar-12 22:53pm    
dear SAKryukov, the person has asked just that how to remove some of the characters from a string. So we gave him that solution. We know that strings are immutable and also the question is not about the performance factor here, its about to solve the problem OP has asked. If it would have been about the performance then i would have gone with your answer :).

I am not trying to say that you are wrong or something else as you are senior to me but i am saying that i am also not wrong :)

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