Best Way to Replace string character

Discussion in 'ASP .Net' started by =?Utf-8?B?UmFlZCBTYXdhbGhh?=, May 18, 2005.

  1. 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
     
    =?Utf-8?B?UmFlZCBTYXdhbGhh?=, May 18, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?UmFlZCBTYXdhbGhh?=

    Karl Seguin Guest

    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" <> wrote in message
    news:...
    >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
     
    Karl Seguin, May 18, 2005
    #2
    1. Advertising

  3. > 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" <> wrote in message
    news:...
    >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
     
    Kevin Spencer, May 18, 2005
    #3
  4. =?Utf-8?B?UmFlZCBTYXdhbGhh?=

    Karl Seguin Guest

    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:%...
    > 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" <> wrote in message
    > news:...
    >>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

    >
    >
     
    Karl Seguin, May 18, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    20
    Views:
    912
    Steven D'Aprano
    Oct 23, 2005
  2. Brand Bogard

    8 bit character string to 16 bit character string

    Brand Bogard, May 25, 2006, in forum: C Programming
    Replies:
    8
    Views:
    741
    those who know me have no need of my name
    May 28, 2006
  3. herman
    Replies:
    5
    Views:
    7,609
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 30, 2007
  4. Victor
    Replies:
    3
    Views:
    136
    Victor
    Sep 12, 2003
  5. Bart Vandewoestyne
    Replies:
    8
    Views:
    741
    Bart Vandewoestyne
    Sep 25, 2012
Loading...

Share This Page