Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Best Way to Replace string character (http://www.velocityreviews.com/forums/t102858-best-way-to-replace-string-character.html)

=?Utf-8?B?UmFlZCBTYXdhbGhh?= 05-18-2005 09:55 AM

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

Karl Seguin 05-18-2005 11:26 AM

Re: Best Way to Replace string character
 
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" <RaedSawalha@discussions.microsoft.com> wrote in message
news:98AAE6BB-0132-4E22-8F7F-DB5118A16A6E@microsoft.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




Kevin Spencer 05-18-2005 12:06 PM

Re: Best Way to Replace string character
 
> 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" <RaedSawalha@discussions.microsoft.com> wrote in message
news:98AAE6BB-0132-4E22-8F7F-DB5118A16A6E@microsoft.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




Karl Seguin 05-18-2005 01:00 PM

Re: Best Way to Replace string character
 
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:%233Qhjx5WFHA.2700@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" <RaedSawalha@discussions.microsoft.com> wrote in message
> news:98AAE6BB-0132-4E22-8F7F-DB5118A16A6E@microsoft.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

>
>





All times are GMT. The time now is 11:41 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57