KBD

Keith Devens .com

Saturday, July 5, 2008 Flag waving
Linux is only free if your time has no value – Jamie Zawinski
← LethonKaliban's Class Loot Lists →

Daily link icon Thursday, July 27, 2006

Dumb substring behavior in C# (and Java)

Substring in C# (and Java) throw an exception if you take a substring and give it a length (or a starting position) that puts you after the end of the string. That's basically to ensure that you always get a string that's exactly the length you want? Rather than just being able to take a substring and not having to worry about it, you have to include code like the following around every single substring you ever take:

if (str.Length > 30){
    str = str.Substring(0, 30);
}

Typically when you take a substring you want to ensure that your string is no longer than a certain number of characters. I can't think of a situation that would it a good idea for the language to enforce that you can't get a string that's less than the maximum length you want instead of exactly the length you want.

On the other hand, most of the dynamic languages (PPPR) let you substring off the end of the string without worrying about it.

← LethonKaliban's Class Loot Lists →

Comments XML gif

Adam Vandenberg (http://adamv.com/) wrote:

Dumb indeed.

public static class Strings {
        /// <summary>
        /// Returns the substring with the first min(charcount, s.Length)
        /// characters from string s
        /// </summary>
        public static string First(string s, int charcount)
        {
            if (s == null) return String.Empty;
            return s.Substring(0, Math.Min(s.Length, charcount));
        }
}

(Editors note: code reformatted slightly to not bust my layout)

∴ Adam Vandenberg | 27-Jul-2006 1:38pm est | http://adamv.com/ | #9572

revence 27 wrote:

The problem is not the nitification of an error, which should happen in any self-respecting API. You are doing a bad thing -- and should be notified -- if you access out of bounds.

So, it is not the error that's the prob. It's the Exception. The problem is that those languages throw on errors, which is very ungood. That's why Py had str.index, and str.find. One throws, another whispers.

Down with Exceptions. Down with Exceptions! DOWN WITH EXCEPTIONS! (Join in, all of you! Join! Join the Revolution!) Down with EXCEPTIONS!

∴ revence 27 | 30-Jul-2006 6:32am est | #9574

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

July 2008
SunMonTueWedThuFriSat
 12345
6789101112
13141516171819
20212223242526
2728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 5 posts

Recent comments XML

Girls, please don't get breast implants

> And no, you will not be receiving​a picture.

:-(...

Keith: Jul 2, 6:05am

Javascript clone function

This is a clever way to clone an​object if you are using YAHOO UI.​Same tec...

Antonio: Jul 1, 12:47pm

I hate Norton Antivirus

Oh just one other thing norton is​great at keeping people out of your​compu...

kevin.sands: Jul 1, 12:50am

Terminator 3 was awful

I think the biggest reason why T3​totally blew was because Edward​Furlong g...

76.167.172.64: Jun 29, 3:06am

Generated in about 0.124s.

(Used 8 db queries)

mobile phone