Number of Methods in the String Class
March 13th, 2008
Python, Java, Ruby.

I just counted methods on these pages:
- http://docs.python.org/lib/string-methods.html
- http://www.ruby-doc.org/core/classes/String.html
- http://java.sun.com/javase/6/docs/api/java/lang/String.html
Bonus challenge: write a program in each language that counts the number of methods in the String class.
Assuming you’re only looking at public methods, this does it in Ruby:
puts String.public_methods.length
impressive! does that include overloaded methods in Java? Ruby and Python aren’t typed, right? I only speak Java and Assembly…
In [1]: len([m for m in dir('') if not m.startswith('__')])
Out[1]: 37
In Java:
public Class StringCount {
public static void main(String[] args) {
System.out.println(String.class.getDeclaredMethods().lenght);
}
}
To be more accurate, you should probably include the special __len__, __getitem__, etc. methods in Python:
>>> print(len(dir(”)))
65
It’s a tie!
irb(main):004:0> String.instance_methods(false).grep(/[a-z]/).length
=> 74
(false meaning no inherited methods)
4 iterators there (each etc aren’t methods in other languages), and 5 “cast” methods … sooo: 65
groovy> println String.class.declaredMethods.length
68
Might also be interesting to look at the number of constructors. java.lang.String has acquired a truly daunting number. Although in all fairness, much of the complexity of those constructors is due to Unicode and character encoding support. java.lang.String is actually hiding a mountain of code in non-public classes like StringCoding.
I’m sure hordes of angry Rubistas will correct me if I’m wrong, but I believe Ruby is the only one above without Unicode support.
We need a comic comparing ASCII and Unicode I think…
A bit of Ruby
String.new.public_methods.size # => 143
(String.new.public_methods - Object.new.public_methods).size # => 102
String.new.public_methods(false).size # => 83
(String.new.public_methods(false) - Object.new.public_methods).size # => 77
Alex: The Python string type isn’t a Unicode type, either. The corresponding Python “unicode” type adds isdecimal() and isnumeric() methods:
>>> print(len(dir(u”)))
67
I believe the Ruby string is also the only mutable string, which accounts for some of the extra methods.
C# 3.5
35 public instance methods
See:
System.Reflection.MethodInfo[] m = typeof(string).GetMethods(BindingFlags.Instance | BindingFlags.Public);
var a=
from mt in m
group mt by mt.Name into mtg
select mtg.Key;
a.Count().Dump();
Okay, now compare the number of classes that ship with each language. I think you’ll the opposite of the relationship for number of methods in the String class. My guess is that Python (batteries included) has the most, then Java, then Ruby. Is more or less better? I guess it depends on your perspective.
About the former ruby example.. public_instance_methods is your friend
(String.new.public_methods(false) - Object.new.public_methods).size # => 77
But one should consider aliasing (ie. size and length are one single method albeit shown as two) and side-effecting behaviour (sub and sub!) and variations based on arguments(i.e 6 different behaviours of String#[], kind-of-overloading behavour)
I have a strong desire to hear crickets chirping after Mark’s comment (at least until one of you with enough time on your hands goes and calculates it).
NSString is one serious batteries-included string class. I count 138 methods.
how about in the ’shell’ , well almost, /I/ didn’t have to write any Java to do it
javap java.lang.String | grep public | wc -l
79
javap java.lang.String | grep -v ‘public’ | wc -l
8
javap java.lang.String | grep -v ‘[{}]‘ | wc -l
84
To bring another (unnamed) language into it…
String methodDictionary size
54
On the other hand…
Object allSubclasses size
5403
Just saying…
s = String.methods(1).grep(/\w/) - String.methods(1).grep(/^(instance|included|private|public|protected|singleton|class|const|__)/); puts s.length => 37
Maybe you should re-evaluate your hoakie chart … even with my shitty regex && method listing.
@ Patrick Hurley, try the easy way:
String.methods(1).length => 77
From http://www.ruby-doc.org/core/classes/String.html#M000861
class String
In:
string.c
lib/jcode.rb
lib/mkmf.rb
lib/scanf.rb
lib/yaml/rubytypes.rb
ext/nkf/lib/kconv.rb
so the 119 figure includes all the methods defined by those 6 modules listed after string.c
# ruby -v
ruby 1.8.6 (2007-09-24 patchlevel 111) [universal-darwin9.0]
# irb -f
String.instance_methods(false).reject { |m| m =~ /!$/ }.size
=> 64
actually 62, not counting the aliases
LOL. Yeah, but that large number makes Ruby damn nice for lazy programmers
I just came by your blog today. Pretty nice. Thanks. Just rework the captcha ;/
Yes, Ruby has str.size, which is an alias to str.length,
while Python seems to require len(str), and your chart
seems to indicate that fewer methods is better.
If you’re lucky, that chart will be tattooed to your forehead… in hell.
I think this bar does not say much about a language especially not while not counting the overall number of classes and their relationship. Last but not least I also dont think its good to compared Java to Python/ruby at all, simply because both python and ruby are in a different niche.