Getting Started with Adobe After Effects - Part 6: Motion Blur
A collection of quick technology learning tips from what people around you learn every day

Different ways of Removing spaces,dots(.),hyphens(-),braces '(' and ')' from string

Sep 16 2011 12:02AM by Niladri Biswas   

Recently a colleague of mine asked me to give him a code for replacing spaces,dots(.),hyphens(-),braces '(' and ')'.

Here is my solution

string str= "This is a string having - and - - - - It also has dot (.) Many dots(.....)"; 
Approach 1: Using lambda and extension methods
var way1 = str
                .ToCharArray()
                .Where(i => 
                            i != ' ' && 
                             i != '-' && 
                            i != '.' && 
                           i != '(' &&
                            i != ')'
                           )
                        .Aggregate(" ", (a, b) => a + b);

Approach 2: Using string concat function and lambda and extension method

 var way2= string.Concat(str.Where(i => !new[] { '.', ' ', '-', '(' ,')'}.Contains(i))); 

Approach 3: Using regular expression

  var way3 = Regex.Replace(str, "[ ().-]+", ""); 

OUTPUT

ThisisastringhavingandItalsohasdotManydots 

Thanks

Read More..   [32134 clicks]

Published under: Microsoft .NET Tips ·  ·  ·  · 


Niladri Biswas
7 · 21% · 6710
18
 
1
 
16
 
0
Incorrect
 
0
Interesting
 



Submit

2  Comments  

  • nice post

    commented on Sep 17 2011 2:27AM
    Srikanth Reddy
    413 · 0% · 99
  • Nice post. In math we were very particular about the naming conventions of our symbols. In particular they were parenthesis "()", brackets "[]" and braces "{}". Since we were primarily working with the verbal translation of symbols this made a huge difference.

    commented on Nov 18 2013 11:56AM
    Dave Vroman
    128 · 1% · 399

Your Comment


Sign Up or Login to post a comment.

"Different ways of Removing spaces,dots(.),hyphens(-),braces '(' and ')' from string" rated 5 out of 5 by 18 readers
Different ways of Removing spaces,dots(.),hyphens(-),braces '(' and ')' from string , 5.0 out of 5 based on 18 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]