473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

String Replace

rsrinivasan
221 100+
All,

I am using c#. I want to replace any special character with space character.

For eg,
str = "hello world*& c shar#p"

should be like this,

"hello world c shar p"

thanks,
Sep 24 '08 #1
8 1691
PRR
750 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. string s = "Aaa@@@###";
  2.             string[] s2 = new string[] { "*","@","#"};//DEfine special char to remove...
  3.  
  4.             foreach (string s1 in s2)
  5.             {
  6.                 if (s.Contains(s1))
  7.                 {
  8.                   s=  s.Replace(s1, "");
  9.                 }
  10.             }
  11.  
  12.  
Sep 24 '08 #2
Curtis Rutland
3,256 Expert 2GB
Interestingly enough, you don't even have to do the .Contains(). .Replace will replace all instances of the first parameter with the second, and won't throw exceptions if there are none.
Sep 24 '08 #3
PRR
750 Expert 512MB
Interestingly enough, you don't even have to do the .Contains(). .Replace will replace all instances of the first parameter with the second, and won't throw exceptions if there are none.
lol.. you are right... .Contains is not required.... its just make it more readable....
Expand|Select|Wrap|Line Numbers
  1. string s = "@@@";
  2.             string ss = "AAA@@@";
  3.  
  4.             ss = ss.Replace("@", "");
  5.             Console.WriteLine(ss);
  6.  
Sep 25 '08 #4
PRR
750 Expert 512MB
really stupid of me...a lot of times i catch my self doin this... writing code that is not required....
Sep 25 '08 #5
balabaster
797 Expert 512MB
really stupid of me...a lot of times i catch my self doin this... writing code that is not required....
Alas, a straight replace isn't as handy as you might think as you have to do each individual character separately. It's far easier to use a regular expression.

VB
Expand|Select|Wrap|Line Numbers
  1. Dim OutString As String = Regex.Replace("Hello W\orl#d", "[\#@\*\\/]", " ")
C#
Expand|Select|Wrap|Line Numbers
  1. String OutString = Regex.Replace("Hello World", "[\#@\*\\/]", " ")
It's not quite as straightforward as a straight replace, but it's far more powerful.

Lookup Regex Cheat Sheets on Google...
Sep 25 '08 #6
PRR
750 Expert 512MB
Thats rights... regular expression is the way to go...even for large log files... use regular expression......using strings is usually performance hit.. specially if you are changing the string frequently...
Theres also "Aho-Corasick algorithm" for even larger files ...

Peter Bromberg's Article

For searchin through log files i normally use Regular Expression... havent tried this one(Aho-Corasick algorithm) as yet...
Sep 25 '08 #7
PRR
750 Expert 512MB
Alas, a straight replace isn't as handy as you might think as you have to do each individual character separately. It's far easier to use a regular expression.
VB
Expand|Select|Wrap|Line Numbers
  1. Dim OutString As String = Regex.Replace("Hello W\orl#d", "[\#@\*\\/]", " ")
C#
Expand|Select|Wrap|Line Numbers
  1. String OutString = Regex.Replace("Hello World", "[\#@\*\\/]", " ")
It's not quite as straightforward as a straight replace, but it's far more powerful.

Lookup Regex Cheat Sheets on Google...
ok lets consider a case where string has to be matched n replaced...
which is the best way
a. If the input string is very small (a few lines)
b. Its a log file ..few kb
c. its a large file... very large .. running into mbs...
Sep 25 '08 #8
balabaster
797 Expert 512MB
ok lets consider a case where string has to be matched n replaced...
which is the best way
a. If the input string is very small (a few lines)
b. Its a log file ..few kb
c. its a large file... very large .. running into mbs...
I've used regex to extract and/or replace strings in files amounting to many hundreds of megabytes... I'd love to know of a more efficient way.

The easiest way I could think of is to break the file into the same amount of chunks as there are processors in your machine and have a thread parse each. Make sure there is some overlap so you don't miss any that are broken due to the chunk separation.

I guess efficiency comes down to some comp-sci theory about efficient parsing mechanisms.
Sep 25 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
13
by: dimitris67 | last post by:
How can I replace an occurence of p(a string) in an other string(s) with np(new string).. char* replace _pattern(char *s,char *p,char *np) PLEASE HELP ME!!!!!
7
by: VMI | last post by:
If I have the string "Héllo", how can I replace char (é) with an 'e'? I cannot use the String.Replace() fuction. It has to be by replacing one char with another. Thanks.
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
9
by: Crirus | last post by:
dim pp as string pp="{X=356, Y=256}{X=356, Y=311.2285}{X=311.2285, Y=356}{X=256, Y=356}{X=200.7715, Y=356}{X=156, Y=311.2285}{X=156, Y=256}{X=156, Y=200.7715}{X=200.7715, Y=156}{X=256,...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
3
by: kronus | last post by:
I'm receiving an xml file that has a child called modified and it represents a date value in the form of a string -- Nov 14, 2008 -- and in my app, I have items associated with each object and I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.