468,134 Members | 1,261 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,134 developers. It's quick & easy.

Best Way to Replace string character

I have the following letters;

string letters = "a;b;c....to z";

the I need to replace the incoming string which containing letters above
with integer 1 i did following

for(int u=0;u<letters.Split(';').Length;u++) {
FilesName = FilesName.Trim().Replace(letters.Split(';')[u], "1");
}

is there better way to do that
Nov 19 '05 #1
3 2355
Well,
You could use a regular expression. Failing that, you could write your code
to be a lot more efficient.

StringBuilder sb = new StringBuilder(letters.Length);
char[] letters = FilesName.Trim().ToCharArray();
for each (char letter in letters){
sb.Append( letter == ';' ? ';' : '1' ) ;
}
if (letter != ';'){
sb.Append("1");
}
}

or something similar...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Raed Sawalha" <Ra*********@discussions.microsoft.com> wrote in message
news:98**********************************@microsof t.com...
I have the following letters;

string letters = "a;b;c....to z";

the I need to replace the incoming string which containing letters above
with integer 1 i did following

for(int u=0;u<letters.Split(';').Length;u++) {
FilesName = FilesName.Trim().Replace(letters.Split(';')[u], "1");
}

is there better way to do that

Nov 19 '05 #2
> is there better way to do that

Sure is. You only need to split the string once, so create a string array
variable, and use that array in your loop. It prevents the recurring split,
which will consume a lot of resources creating arrays of strings that are
constantly being thrown away.

In fact, assumming that you want to use the lettters of the English alphabet
(or any alphabet, for that matter), as you do in your example, you don't
need an array of strings at all. You can create each letter in the alphabet
using its numerical value. As each letter has a numerical value that is
sequential, you can apply math to a single numerical value to get each
letter. This can be done by casting an integer to a char.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.

"Raed Sawalha" <Ra*********@discussions.microsoft.com> wrote in message
news:98**********************************@microsof t.com...
I have the following letters;

string letters = "a;b;c....to z";

the I need to replace the incoming string which containing letters above
with integer 1 i did following

for(int u=0;u<letters.Split(';').Length;u++) {
FilesName = FilesName.Trim().Replace(letters.Split(';')[u], "1");
}

is there better way to do that

Nov 19 '05 #3
egads..just got to work and saw that i had some leftovers in there...should
be:

StringBuilder sb = new StringBuilder(letters.Length);
char[] letters = FilesName.Trim().ToCharArray();
for each (char letter in letters){
sb.Append( letter == ';' ? ';' : '1' ) ;
}
Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
Well,
You could use a regular expression. Failing that, you could write your
code to be a lot more efficient.

StringBuilder sb = new StringBuilder(letters.Length);
char[] letters = FilesName.Trim().ToCharArray();
for each (char letter in letters){
sb.Append( letter == ';' ? ';' : '1' ) ;
}
if (letter != ';'){
sb.Append("1");
}
}

or something similar...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Raed Sawalha" <Ra*********@discussions.microsoft.com> wrote in message
news:98**********************************@microsof t.com...
I have the following letters;

string letters = "a;b;c....to z";

the I need to replace the incoming string which containing letters above
with integer 1 i did following

for(int u=0;u<letters.Split(';').Length;u++) {
FilesName = FilesName.Trim().Replace(letters.Split(';')[u], "1");
}

is there better way to do that


Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Billy N. Patton | last post: by
14 posts views Thread by Etu | last post: by
5 posts views Thread by djc | last post: by
3 posts views Thread by Pascal | last post: by
29 posts views Thread by gs | last post: by
8 posts views Thread by Warren Moxley | last post: by
6 posts views Thread by Gabriel | last post: by
5 posts views Thread by shapper | last post: by
27 posts views Thread by didacticone | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.