Best Way to Replace string character

G

Guest

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(';'), "1");
}

is there better way to do that
 
K

Karl Seguin

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
 
K

Kevin Spencer

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.
 
K

Karl Seguin

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 said:
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 said:
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(';'), "1");
}

is there better way to do that

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top