473,394 Members | 2,063 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,394 software developers and data experts.

string copying

Is there a function that copies between string (or stringbuilder)
objects, while filtering out certain types or designated characters,
or leaving a certain typa of designates\d characters? For example, I
want to copy only the alphanumeric characters, or I want to copy a
string without the spaces.
Thanks

Mar 21 '07 #1
2 1706
"dani kotlar" <da*********@gmail.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
Is there a function that copies between string (or stringbuilder)
objects, while filtering out certain types or designated characters,
or leaving a certain typa of designates\d characters? For example, I
want to copy only the alphanumeric characters, or I want to copy a
string without the spaces.
To copy a string without the spaces:

string theOriginal = "String with spaces";
string theCopy = theOriginal.Replace(" ","");

With a StringBuilder:

StringBuilder theOriginal = new StringBuilder("Strign with spaces");
theOriginal.Replace(" ", "");
string theCopy = theOriginal.ToString();

To remove "a" and "b" characters:

string theCopy = theOriginal.Replace("a","").Replace("b","");

If the combination of characters that you want to remove gets more
complex than this, it's time to start studying Regular Expressions (RegEx
class).

Mar 21 '07 #2
dani kotlar wrote:
Is there a function that copies between string (or stringbuilder)
objects, while filtering out certain types or designated characters,
or leaving a certain typa of designates\d characters? For example, I
want to copy only the alphanumeric characters, or I want to copy a
string without the spaces.
Thanks
You can use a regular expression to remove characters from a string.

This will remove everything but alphanumeric characters:

string filtered = Regex.Replace(original, "[^\dA-Za-z]+", string.Empty);

[] creates a set
^ makes it a negative set
\d matches a digit
A-Za-z matches a letter
+ repeats one or more times

So the pattern matches every group of characters (one or more) that is
not an alphanumeric, and replaces it with an empty string.

--
Göran Andersson
_____
http://www.guffa.com
Mar 21 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: CoolPint | last post by:
Is there any way I can reduce the size of internal buffer to store characters by std::string? After having used a string object to store large strings, the object seems to retain the large...
20
by: hagai26 | last post by:
I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
11
by: akarui.tomodachi | last post by:
I have two structure definitions as below: typedef struct _sourceString { char firstSourceString; char secondSourceString; char thirdSourceString; }sourceString;
6
by: Erik | last post by:
Hello, For many years ago I implemented my own string buffer class, which works fine except assignments - it copies the char* buffer instead of the pointer. Therefore in function calls I pass...
12
by: Vincent RICHOMME | last post by:
Hi, I am currently implementing some basic classes from .NET into modern C++. And I would like to know if someone would know a non mutable string class.
26
by: Hardy Wang | last post by:
Hi all, I know it is better to handle large string with a StringBuilder, but how does StringBuilder class improve the performance in the background? Thanks! -- WWW:...
2
by: Mike Cain | last post by:
Two quick questions please: 1) How do I declare a STL string variable that I know in advance I want to hold 5,000 bytes? I'm using SGI STL with MS VS 6.0 C++ in case that matters. For example,...
9
by: barcaroller | last post by:
1. If I pass pointers (char*) as iterators to an STL algorithm and the return value is an iterator, can I convert that iterator to a pointer? If yes, how? 2. What is the internal...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.