Number of Methods in the String Class

Python, Java, Ruby.

String Methods

I just counted methods on these pages:

Bonus challenge: write a program in each language that counts the number of methods in the String class.


22 Responses to “Number of Methods in the String Class”

Chris Says:

Assuming you’re only looking at public methods, this does it in Ruby:

puts String.public_methods.length

DanB Says:

impressive! does that include overloaded methods in Java? Ruby and Python aren’t typed, right? I only speak Java and Assembly…

Danny Says:

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);
}
}

Carey Says:

To be more accurate, you should probably include the special __len__, __getitem__, etc. methods in Python:

>>> print(len(dir(”)))
65

It’s a tie!

et Says:

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

Alex Miller Says:

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

Carey Says:

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.

Vladekk Says:

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();

Mark Volkmann Says:

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.

riffraff Says:

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)

Michael Says:

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). ;)

Andre Pang Says:

NSString is one serious batteries-included string class. I count 138 methods.

Wayne Keenan Says:

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

Marco Qualizza Says:

To bring another (unnamed) language into it…

String methodDictionary size
54

On the other hand…
Object allSubclasses size
5403

Just saying…

Alan Says:

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

ste Says:

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

Bashar Says:

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 ;/

Woodie Says:

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.

she Says:

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.

Leave a Reply