Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. switch statemeent. Am I missing someting

switch statemeent. Am I missing someting

Scheduled Pinned Locked Moved C#
questioncsharphelp
19 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Judah Gabriel Himango

    Way easier way to do that using the latest version of C#:

    string input = "This is a c# program!";

    var newChars = from character in input
                   where !char.IsPunctuation(character) && !char.IsWhiteSpace(character)
                  select char.ToUpper(character);

    string result = new string(newChars.ToArray()); // results in "THISISACPROGRAM"

    Life, family, faith: Give me a visit. From my latest post: "A lot of Christians struggle, perhaps at a subconscious level, about the phrase "God of Israel". After all, Israel's God is the God of Judaism, is He not? And the God of Christianity is not the God of Judaism, right?" Judah Himango

    T Offline
    T Offline
    TheFoZ
    wrote on last edited by
    #5

    Thanks for that. As you said, the code is for the latest version of C# and I am using 2005. I came up with this that seems to do the job

    public static string CVNameSearch(string inputStr)
    {
    int x;
    string returnStr;

        returnStr = "";
    
        for (x = 0; x<= inputStr.Length -1; ++x)
        {
            if (char.IsLetter(inputStr, x))
            {
                returnStr += inputStr.Substring(x, 1).ToUpper();
            }
        }
    
        return returnStr;
    
     }
    

    Thanks for your help today

    The FoZ

    1 Reply Last reply
    0
    • T TheFoZ

      Thanks for the response. Do you have an example at all. What the function basically does is convert a string to an Upper Case string without any punctuation. e.g "This is a c# program!" converts to "THISISACPROGRAM" Many thanks

      The FoZ

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #6

      Why not something simple, like this? str = Regex.Replace(str, "[^A-Za-z]+", string.Empty).ToUpper();

      Despite everything, the person most likely to be fooling you next is yourself.

      T 1 Reply Last reply
      0
      • G Guffa

        Why not something simple, like this? str = Regex.Replace(str, "[^A-Za-z]+", string.Empty).ToUpper();

        Despite everything, the person most likely to be fooling you next is yourself.

        T Offline
        T Offline
        TheFoZ
        wrote on last edited by
        #7

        Nice cheers. Don't suppose you could explain the syntax for me. I gather from the help that str is the input string, "[^A-Za-z]+" is the match but I'm not sure how it works. What is the string.Empty for? Thanks for your help

        The FoZ

        E G 2 Replies Last reply
        0
        • T TheFoZ

          Nice cheers. Don't suppose you could explain the syntax for me. I gather from the help that str is the input string, "[^A-Za-z]+" is the match but I'm not sure how it works. What is the string.Empty for? Thanks for your help

          The FoZ

          E Offline
          E Offline
          Ed Poore
          wrote on last edited by
          #8

          [^A-Za-z]+ Basically this is a regular expression pattern which matches: [^]+ ==> any character that is not included inside the brackets A-Z ==> matches any character in the range A to Z a-z ==> matches any character in the range a to z So basically it replaces an non-alphabetic character with string.Empty.  string.Empty (or String.Empty or System.String.Empty) are essentially the same as "" however when you reference them it does not create a new object but utilises one that's already been created, thus more memory efficient.  A small difference but sometimes it matters.

          G P T 3 Replies Last reply
          0
          • T TheFoZ

            Nice cheers. Don't suppose you could explain the syntax for me. I gather from the help that str is the input string, "[^A-Za-z]+" is the match but I'm not sure how it works. What is the string.Empty for? Thanks for your help

            The FoZ

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #9

            The pattern "[^A-Za-z]+" matches any non-letter characters, which then are replaced by an empty string, so that any non-letter characters are removed from the string. The ^ in the set makes it a negative set, mathing any characters except A-Z and a-z. If you want to keep any other characters, you just add them in the pattern, like for example "[^A-Za-zÅåÄäÖöÉéÈèËëÑñ]+".

            Despite everything, the person most likely to be fooling you next is yourself.

            1 Reply Last reply
            0
            • E Ed Poore

              [^A-Za-z]+ Basically this is a regular expression pattern which matches: [^]+ ==> any character that is not included inside the brackets A-Z ==> matches any character in the range A to Z a-z ==> matches any character in the range a to z So basically it replaces an non-alphabetic character with string.Empty.  string.Empty (or String.Empty or System.String.Empty) are essentially the same as "" however when you reference them it does not create a new object but utilises one that's already been created, thus more memory efficient.  A small difference but sometimes it matters.

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #10

              Ed.Poore wrote:

              string.Empty (or String.Empty or System.String.Empty) are essentially the same as "" however when you reference them it does not create a new object but utilises one that's already been created, thus more memory efficient. A small difference but sometimes it matters.

              The performance difference is really minimal, but there is another difference. If you always use string.Empty when you want an empty string, the code gets clearer. If you happen to stumble upon a "" in the code, you know that there is supposed to be something between the quotation marks, that perhaps got erased or left out by mistake. :)

              Despite everything, the person most likely to be fooling you next is yourself.

              E S 2 Replies Last reply
              0
              • E Ed Poore

                [^A-Za-z]+ Basically this is a regular expression pattern which matches: [^]+ ==> any character that is not included inside the brackets A-Z ==> matches any character in the range A to Z a-z ==> matches any character in the range a to z So basically it replaces an non-alphabetic character with string.Empty.  string.Empty (or String.Empty or System.String.Empty) are essentially the same as "" however when you reference them it does not create a new object but utilises one that's already been created, thus more memory efficient.  A small difference but sometimes it matters.

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #11

                Ed.Poore wrote:

                does not create a new object but utilises one that's already been created

                But in either case it stores an empty string in the executable at that point, because it's a constant, right? If so, there's no difference.

                E G 2 Replies Last reply
                0
                • T TheFoZ

                  Thanks for the response. Do you have an example at all. What the function basically does is convert a string to an Upper Case string without any punctuation. e.g "This is a c# program!" converts to "THISISACPROGRAM" Many thanks

                  The FoZ

                  M Offline
                  M Offline
                  Mark Churchill
                  wrote on last edited by
                  #12

                  In C# you would use a bunch of if(c == foo) .... else if(30 < c && c < 100 && c == 10) ... etc. Of course with your particular issue doing it character by character probably isnt the best solution. I'd avoid using a Regex for something so simple. The framework has a bunch of stuff here thats probably useful (and culture invariant), eg: "foo bar".ToUpper().Replace(" ","") or similar. And who suggested LINQ? Just because LINQ is cool doesnt mean its appropriate to use it ;)

                  Mark Churchill Director Dunn & Churchill Free Download:
                  Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.

                  1 Reply Last reply
                  0
                  • G Guffa

                    Ed.Poore wrote:

                    string.Empty (or String.Empty or System.String.Empty) are essentially the same as "" however when you reference them it does not create a new object but utilises one that's already been created, thus more memory efficient. A small difference but sometimes it matters.

                    The performance difference is really minimal, but there is another difference. If you always use string.Empty when you want an empty string, the code gets clearer. If you happen to stumble upon a "" in the code, you know that there is supposed to be something between the quotation marks, that perhaps got erased or left out by mistake. :)

                    Despite everything, the person most likely to be fooling you next is yourself.

                    E Offline
                    E Offline
                    Ed Poore
                    wrote on last edited by
                    #13

                    That's the best justification I've heard of it, thanks!  I do use it but had always been slightly confused as to what the point was, now there's a valid reason.

                    S 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Ed.Poore wrote:

                      does not create a new object but utilises one that's already been created

                      But in either case it stores an empty string in the executable at that point, because it's a constant, right? If so, there's no difference.

                      E Offline
                      E Offline
                      Ed Poore
                      wrote on last edited by
                      #14

                      No it doesn't, I just compiled a sample and took a look at it under reflector and they definitely use two different methods, one compares a constant, the other compares the reference:

                      .entrypoint
                      .maxstack 2
                      .locals init (
                      [0] string toCompare,
                      [1] bool c1,
                      [2] bool c2)
                      L_0000: nop
                      L_0001: ldstr "Hello"
                      L_0006: stloc.0
                      L_0007: ldloc.0
                      L_0008: ldsfld string [mscorlib]System.String::Empty
                      L_000d: call bool [mscorlib]System.String::op_Equality(string, string)
                      L_0012: stloc.1
                      L_0013: ldloc.0
                      L_0014: ldstr ""
                      L_0019: call bool [mscorlib]System.String::op_Equality(string, string)
                      L_001e: stloc.2
                      L_001f: ret

                      1 Reply Last reply
                      0
                      • E Ed Poore

                        [^A-Za-z]+ Basically this is a regular expression pattern which matches: [^]+ ==> any character that is not included inside the brackets A-Z ==> matches any character in the range A to Z a-z ==> matches any character in the range a to z So basically it replaces an non-alphabetic character with string.Empty.  string.Empty (or String.Empty or System.String.Empty) are essentially the same as "" however when you reference them it does not create a new object but utilises one that's already been created, thus more memory efficient.  A small difference but sometimes it matters.

                        T Offline
                        T Offline
                        TheFoZ
                        wrote on last edited by
                        #15

                        Thanks for the info. 'Every little helps' when it all comes down to it. The Regex expression does exactly what the old VB code does in about 10 lines and lots of iterations through a loop.

                        The FoZ

                        E 1 Reply Last reply
                        0
                        • T TheFoZ

                          Thanks for the info. 'Every little helps' when it all comes down to it. The Regex expression does exactly what the old VB code does in about 10 lines and lots of iterations through a loop.

                          The FoZ

                          E Offline
                          E Offline
                          Ed Poore
                          wrote on last edited by
                          #16

                          It all depends on whether you understand (and like) regular expressions. Some people will do anything to avoid them.


                          I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder

                          1 Reply Last reply
                          0
                          • E Ed Poore

                            That's the best justification I've heard of it, thanks!  I do use it but had always been slightly confused as to what the point was, now there's a valid reason.

                            S Offline
                            S Offline
                            Scott Dorman
                            wrote on last edited by
                            #17

                            Another benefit is that it ensures the comparisons are always performed the same way...no more some tests looking for length == 0 while others looking for "".

                            Scott Dorman

                            Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai


                            [Forum Guidelines][Articles][Blog]

                            1 Reply Last reply
                            0
                            • G Guffa

                              Ed.Poore wrote:

                              string.Empty (or String.Empty or System.String.Empty) are essentially the same as "" however when you reference them it does not create a new object but utilises one that's already been created, thus more memory efficient. A small difference but sometimes it matters.

                              The performance difference is really minimal, but there is another difference. If you always use string.Empty when you want an empty string, the code gets clearer. If you happen to stumble upon a "" in the code, you know that there is supposed to be something between the quotation marks, that perhaps got erased or left out by mistake. :)

                              Despite everything, the person most likely to be fooling you next is yourself.

                              S Offline
                              S Offline
                              Scott Dorman
                              wrote on last edited by
                              #18

                              Excellent reasons. Another benefit is that it ensures the comparisons are always performed the same way...no more some tests looking for length == 0 while others looking for "".

                              Scott Dorman

                              Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai


                              [Forum Guidelines][Articles][Blog]

                              1 Reply Last reply
                              0
                              • P PIEBALDconsult

                                Ed.Poore wrote:

                                does not create a new object but utilises one that's already been created

                                But in either case it stores an empty string in the executable at that point, because it's a constant, right? If so, there's no difference.

                                G Offline
                                G Offline
                                Guffa
                                wrote on last edited by
                                #19

                                PIEBALDconsult wrote:

                                But in either case it stores an empty string in the executable at that point, because it's a constant, right? If so, there's no difference.

                                No, the string.Empty property returns a string that already exists in the mscorlib.dll. If you use "", that literal string will be added to your assembly. The difference is minimal, as it's only a few bytes of data, but there is a difference.

                                Despite everything, the person most likely to be fooling you next is yourself.

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups