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. The Lounge
  3. Fine, I'll jump on the "I hate Microsoft" bandwagon

Fine, I'll jump on the "I hate Microsoft" bandwagon

Scheduled Pinned Locked Moved The Lounge
67 Posts 26 Posters 2 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.
  • S Scott Serl

    Here you go:

        public static string Left(this string theString, int length)
        {
            int sz = length > theString.Length + 1 ? theString.Length:length;
    
            return theString.Substring(0, sz);
        }
    
    Richard DeemingR Offline
    Richard DeemingR Offline
    Richard Deeming
    wrote on last edited by
    #29

    No need to call Substring if the length is greater than (or equal to) the string's length. And you'll get a NullReferenceException if the string is null, whereas VB would return an empty string instead. Seems like an odd choice, but when you're pandering to these VB devs... :rolleyes:

    public static string Left(this string theString, int length)
    {
    if (length < 0) throw new ArgumentOutOfRangeException(nameof(length));
    if (theString == null || length == 0) return string.Empty;
    if (theString.Length <= length) return theString;
    return theString.Substring(0, length);
    }

    Now, let's hope he doesn't ask for a C# version of this VB6 abomination:

    Dim s As String = "He11o"
    Mid$(s, 3, 2) = "ll"


    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

    1 Reply Last reply
    0
    • 9 9082365

      RyanDev wrote:

      Errors if you give it a length that is longer than the string.

      As one would jolly well hope it did. I don't see why MS should take stick for your faulty programming logic.

      I am not a number. I am a ... no, wait!

      D Offline
      D Offline
      dandy72
      wrote on last edited by
      #30

      Well, he did mention VB. Which teaches everyone bad habits.

      S 1 Reply Last reply
      0
      • Z ZurdoDev

        So, C# has no left method on a string? :omg: :omg: It would be so easy to add one, lazy Microsoft C# developers. So, use substring. Whoops. Nope. Errors if you give it a length that is longer than the string. I miss VB. :sigh:

        There are only 10 types of people in the world, those who understand binary and those who don't.

        S Offline
        S Offline
        Super Lloyd
        wrote on last edited by
        #31

        Oh my, you are the quick to hate type, are you not? One tiny method missing and *boom* I hate you MS! That escalated rather quickly! :O At any rate I give you here, free of charge, a solution to use for your own coding pleasure! No, no, no, there is no need to thank me!

        public static class StringExtensions {
        public static string Left(this string s, int n) {
        if (n >= s.Length)
        return "";
        return s.Substring(s.Length - n);
        }
        }

        All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

        J D Z 3 Replies Last reply
        0
        • Z ZurdoDev

          So, C# has no left method on a string? :omg: :omg: It would be so easy to add one, lazy Microsoft C# developers. So, use substring. Whoops. Nope. Errors if you give it a length that is longer than the string. I miss VB. :sigh:

          There are only 10 types of people in the world, those who understand binary and those who don't.

          B Offline
          B Offline
          BillWoodruff
          wrote on last edited by
          #32

          While I cherish the myriad weirdnesses I am aware of in my long-term relationship with C#, the absence of 'Left would not qualify. Welcome to Linq: [^]. Unless there's something you just can't stand about the word: "Take" ? :) cheers, Bill

          «There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008

          1 Reply Last reply
          0
          • Z ZurdoDev

            So, C# has no left method on a string? :omg: :omg: It would be so easy to add one, lazy Microsoft C# developers. So, use substring. Whoops. Nope. Errors if you give it a length that is longer than the string. I miss VB. :sigh:

            There are only 10 types of people in the world, those who understand binary and those who don't.

            M Offline
            M Offline
            Marc Clifton
            wrote on last edited by
            #33

            These are the string extension methods I wrote ages ago (well, since extension methods have been around): (Note that some of this isn't very elegant, could be LINQ'ified, could be optimized, etc.)

            	/// /// Returns a new string surrounded by single quotes.
            	/// 
            	public static string SingleQuote(this String src)
            	{
            		return "'" + src + "'";
            	}
            
            	/// /// Returns a new string surrounded by quotes.
            	/// 
            	public static string Quote(this String src)
            	{
            		return "\\"" + src + "\\"";
            	}
            
            	/// /// Exchanges ' for " and " for '
            	/// Javascript JSON support, which must be formatted like '{"foo":"bar"}'
            	/// 
            	public static string ExchangeQuoteSingleQuote(this String src)
            	{
            		string ret = src.Replace("'", "\\0xFF");
            		ret = ret.Replace("\\"", "'");
            		ret = ret.Replace("\\0xFF", "\\"");
            
            		return ret;
            	}
            
            	/// /// Returns the source string surrounded by a single whitespace.
            	/// 
            	public static string Spaced(this String src)
            	{
            		return " " + src + " ";
            	}
            
            	/// /// Returns a new string surrounded by brackets.
            	/// 
            	public static string Parens(this String src)
            	{
            		return "(" + src + ")";
            	}
            
            	/// /// Returns a new string surrounded by brackets.
            	/// 
            	public static string Brackets(this String src)
            	{
            		return "\[" + src + "\]";
            	}
            
            	/// /// Returns a new string surrounded by brackets.
            	/// 
            	public static string CurlyBraces(this String src)
            	{
            		return "{" + src + "}";
            	}
            
            	/// /// Returns everything between the start and end chars, exclusive.
            	/// 
            	/// The source string.
            	/// The first char to find.
            	/// The end char to find.
            	/// The string between the start and stop chars, or an empty string if not found.
            	public static string Between(this string src, char start, char end)
            	{
            		string ret = String.Empty;
            		int idxStart = src.IndexOf(start);
            
            		if (idxStart != -1)
            		{
            			++idxStart;
            			int idxEnd = src.IndexOf(end, idxStart);
            
            			if (idxEnd != -1)
            			{
            				ret = src.Substring(idxStart, idxEnd - idxStart);
            			}
            		}
            
            		return ret;
            	}
            
            	public static string Between(this string src, string start, string end)
            	{
            		string ret = String.Empty;
            		int idxStart = src.IndexOf(start);
            
            		if (idxSta
            
            1 Reply Last reply
            0
            • M Mark_Wallace

              Oh, there is, but the definition of the word "music" has changed to be something to do with money for nothing.

              I wanna be a eunuchs developer! Pass me a bread knife!

              M Offline
              M Offline
              Marco Bertschi
              wrote on last edited by
              #34

              Mark_Wallace wrote:

              money for nothing

              And chicks for free.

              D 1 Reply Last reply
              0
              • S Scott Serl

                Here you go:

                    public static string Left(this string theString, int length)
                    {
                        int sz = length > theString.Length + 1 ? theString.Length:length;
                
                        return theString.Substring(0, sz);
                    }
                
                J Offline
                J Offline
                Jono Stewart
                wrote on last edited by
                #35

                Don't forget the null check! And a small tweak because I like doing it this way :P It's called Truncate in my library, but if you're from VB, I guess Left is ok... Or you could just reference the Microsoft.VisualBasic assembly!

                public static string Left(this string target, int length)
                {
                if(string.IsNullOrEmpty(target) //could just do == null; condition below caters to empty (which is faster I wonder)
                return target;

                return target.SubString(0, Math.Min(target.Length, length));
                

                }

                1 Reply Last reply
                0
                • S Super Lloyd

                  Oh my, you are the quick to hate type, are you not? One tiny method missing and *boom* I hate you MS! That escalated rather quickly! :O At any rate I give you here, free of charge, a solution to use for your own coding pleasure! No, no, no, there is no need to thank me!

                  public static class StringExtensions {
                  public static string Left(this string s, int n) {
                  if (n >= s.Length)
                  return "";
                  return s.Substring(s.Length - n);
                  }
                  }

                  All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                  J Offline
                  J Offline
                  Jono Stewart
                  wrote on last edited by
                  #36

                  Looks like a 'Right' to me, rather than a 'Left' :)

                  S 1 Reply Last reply
                  0
                  • Z ZurdoDev

                    So, C# has no left method on a string? :omg: :omg: It would be so easy to add one, lazy Microsoft C# developers. So, use substring. Whoops. Nope. Errors if you give it a length that is longer than the string. I miss VB. :sigh:

                    There are only 10 types of people in the world, those who understand binary and those who don't.

                    Sander RosselS Offline
                    Sander RosselS Offline
                    Sander Rossel
                    wrote on last edited by
                    #37

                    Yesterday I noticed one of our projects has a reference to Microsoft.VisualBasic. What the...? Upon closer inspection I found it was needed for new My.Computer.Devices.ComputerInfo().TotalPhysicalMemory(). Looked around on the web, and there really isn't an easy C# variant :sigh:

                    Read my (free) ebook Object-Oriented Programming in C# Succinctly. Visit my blog at Sander's bits - Writing the code you need. Or read my articles here on CodeProject.

                    Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                    Regards, Sander

                    1 Reply Last reply
                    0
                    • L Lost User

                      Quote:

                      The seventies are back, for certain.

                      Except for the music! There is no music anymore! X|

                      Get me coffee and no one gets hurt!

                      W Offline
                      W Offline
                      Wastedtalent
                      wrote on last edited by
                      #38

                      I found music. I raided my parents Vinyl collection. Love that warm crackly sound of old LP records.

                      1 Reply Last reply
                      0
                      • M Marco Bertschi

                        Mark_Wallace wrote:

                        money for nothing

                        And chicks for free.

                        D Offline
                        D Offline
                        den2k88
                        wrote on last edited by
                        #39

                        We've got to install microwave ovens!

                        GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver When I was six, there were no ones and zeroes - only zeroes. And not all of them worked. -- Ravi Bhavnani

                        1 Reply Last reply
                        0
                        • J Jono Stewart

                          Looks like a 'Right' to me, rather than a 'Left' :)

                          S Offline
                          S Offline
                          Super Lloyd
                          wrote on last edited by
                          #40

                          Ahem.. you know what? You could even be right! :rolleyes: :laugh:

                          All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                          1 Reply Last reply
                          0
                          • S Super Lloyd

                            Oh my, you are the quick to hate type, are you not? One tiny method missing and *boom* I hate you MS! That escalated rather quickly! :O At any rate I give you here, free of charge, a solution to use for your own coding pleasure! No, no, no, there is no need to thank me!

                            public static class StringExtensions {
                            public static string Left(this string s, int n) {
                            if (n >= s.Length)
                            return "";
                            return s.Substring(s.Length - n);
                            }
                            }

                            All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                            D Offline
                            D Offline
                            den2k88
                            wrote on last edited by
                            #41

                            Excuse me but: Several GB worth of Framework, poorly documented, with an inextricable Hell of dependencies and Assembly idiosyncrasies and seemingly duplicated functionality which should live in the same ecosystem but are as incompatible as coffe and salt... and I have to write my farking own version of a function that existed 20 years ago to do the easiest and stupidest thing on Earth? So much for The Framework™.

                            GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver When I was six, there were no ones and zeroes - only zeroes. And not all of them worked. -- Ravi Bhavnani

                            S 1 Reply Last reply
                            0
                            • D den2k88

                              Excuse me but: Several GB worth of Framework, poorly documented, with an inextricable Hell of dependencies and Assembly idiosyncrasies and seemingly duplicated functionality which should live in the same ecosystem but are as incompatible as coffe and salt... and I have to write my farking own version of a function that existed 20 years ago to do the easiest and stupidest thing on Earth? So much for The Framework™.

                              GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver When I was six, there were no ones and zeroes - only zeroes. And not all of them worked. -- Ravi Bhavnani

                              S Offline
                              S Offline
                              Super Lloyd
                              wrote on last edited by
                              #42

                              I am still wondering if you are really throwing a tantrum for a "missing 2 lines function" or it is in fact a joke!? Haha! :laugh: Your seemingly deep anger make it all the more laughable! haha! :wtf: :rolleyes: :laugh:

                              All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                              D 1 Reply Last reply
                              0
                              • S Super Lloyd

                                I am still wondering if you are really throwing a tantrum for a "missing 2 lines function" or it is in fact a joke!? Haha! :laugh: Your seemingly deep anger make it all the more laughable! haha! :wtf: :rolleyes: :laugh:

                                All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                                D Offline
                                D Offline
                                den2k88
                                wrote on last edited by
                                #43

                                My tantrum is directeed to the pile of sunshine that is the .NET framework, in fact I usually make my own libraries anyway. Still for a "framework" so bloated not having a simple and fundamental function is basically an indication of utter failure.

                                GCS d--- s-/++ a- C++++ U+++ P- L- E-- W++ N++ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t++ 5? X R++ tv-- b+ DI+++ D++ G e++>+++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X If you think 'goto' is evil, try writing an Assembly program without JMP. -- TNCaver When I was six, there were no ones and zeroes - only zeroes. And not all of them worked. -- Ravi Bhavnani

                                S 1 Reply Last reply
                                0
                                • S Super Lloyd

                                  Oh my, you are the quick to hate type, are you not? One tiny method missing and *boom* I hate you MS! That escalated rather quickly! :O At any rate I give you here, free of charge, a solution to use for your own coding pleasure! No, no, no, there is no need to thank me!

                                  public static class StringExtensions {
                                  public static string Left(this string s, int n) {
                                  if (n >= s.Length)
                                  return "";
                                  return s.Substring(s.Length - n);
                                  }
                                  }

                                  All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!

                                  Z Offline
                                  Z Offline
                                  ZurdoDev
                                  wrote on last edited by
                                  #44

                                  Super Lloyd wrote:

                                  you are the quick to hate type, are you not?

                                  No.

                                  Super Lloyd wrote:

                                  One tiny method missing and boom I hate you MS!

                                  I'm trying to fit in with the Lounge crowd. :-\

                                  Super Lloyd wrote:

                                  public static class StringExtensions { public static string Left(this string s, int n) { if (n >= s.Length) return ""; return s.Substring(s.Length - n); } }

                                  You tried to sneak that one there. return "". How dare you. :mad:

                                  There are only 10 types of people in the world, those who understand binary and those who don't.

                                  1 Reply Last reply
                                  0
                                  • Z ZurdoDev

                                    So, C# has no left method on a string? :omg: :omg: It would be so easy to add one, lazy Microsoft C# developers. So, use substring. Whoops. Nope. Errors if you give it a length that is longer than the string. I miss VB. :sigh:

                                    There are only 10 types of people in the world, those who understand binary and those who don't.

                                    M Offline
                                    M Offline
                                    Member 10952144
                                    wrote on last edited by
                                    #45

                                    While MS gave you the power of Linq in C#....this gives you an infinite amount of possibilities. That combined with extensions, and you are so much better off than with VB! It's true, you'll have to spend 2 minutes of your personal precious time to write this code once, and to remember to add this extension 'lib' to all your projects where you might need them....boohoo. Sth like this... one line of code...

                                    public static string Left(this string inputString, int length) { return new string((from ch in inputString select ch).Take(length).ToArray()); }

                                    Now you can just Left a string by doing string.Left(5)....so easy but 1000 times more powerful (and prob as many times more performant) than VB....

                                    Z 1 Reply Last reply
                                    0
                                    • M Member 10952144

                                      While MS gave you the power of Linq in C#....this gives you an infinite amount of possibilities. That combined with extensions, and you are so much better off than with VB! It's true, you'll have to spend 2 minutes of your personal precious time to write this code once, and to remember to add this extension 'lib' to all your projects where you might need them....boohoo. Sth like this... one line of code...

                                      public static string Left(this string inputString, int length) { return new string((from ch in inputString select ch).Take(length).ToArray()); }

                                      Now you can just Left a string by doing string.Left(5)....so easy but 1000 times more powerful (and prob as many times more performant) than VB....

                                      Z Offline
                                      Z Offline
                                      ZurdoDev
                                      wrote on last edited by
                                      #46

                                      Member 10952144 wrote:

                                      you'll have to spend 2 minutes of your personal precious time to write this code

                                      Exactly!

                                      There are only 10 types of people in the world, those who understand binary and those who don't.

                                      M 1 Reply Last reply
                                      0
                                      • D dandy72

                                        Well, he did mention VB. Which teaches everyone bad habits.

                                        S Offline
                                        S Offline
                                        Slow Eddie
                                        wrote on last edited by
                                        #47

                                        Since when is increasing programmer/developer (Whatever the term hipsters are using these days...) "teaching bad habits" ?:confused:

                                        D 1 Reply Last reply
                                        0
                                        • Z ZurdoDev

                                          Member 10952144 wrote:

                                          you'll have to spend 2 minutes of your personal precious time to write this code

                                          Exactly!

                                          There are only 10 types of people in the world, those who understand binary and those who don't.

                                          M Offline
                                          M Offline
                                          Member 10952144
                                          wrote on last edited by
                                          #48

                                          Well it'll be less now if you just copy/paste the code....I think 30 secs will do now. Is prob just about as much as VB would need to show you its 'intellisense' info on the Left function ;-)

                                          Z 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