Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Best Way to Replace string character

Reply
Thread Tools

Best Way to Replace string character

 
 
=?Utf-8?B?UmFlZCBTYXdhbGhh?=
Guest
Posts: n/a
 
      05-18-2005
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
 
Reply With Quote
 
 
 
 
Karl Seguin
Guest
Posts: n/a
 
      05-18-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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



 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      05-18-2005
> 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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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



 
Reply With Quote
 
Karl Seguin
Guest
Posts: n/a
 
      05-18-2005
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:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>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

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert string with control character in caret notation to realcontrol character string. Bart Vandewoestyne C Programming 8 09-25-2012 12:41 PM
How can I replace all occurrences of a character with another character in std string? herman C++ 5 08-30-2007 09:05 AM
8 bit character string to 16 bit character string Brand Bogard C Programming 8 05-28-2006 05:05 PM
best way to replace first word in string? hagai26@gmail.com Python 20 10-23-2005 09:56 AM
How to replace a variable string within /* variable_string */ with x for each character in string? Victor Perl Misc 3 09-12-2003 04:19 PM



Advertisments