I wish Python slicing was in Java

After using Python for four months now, I like a lot, especially with Django. But, honestly, I think I’ll always prefer Java.

There is one thing in Python that just kicks Java’s ass, though: slicing.

If only Java allowed this:

String s = "Hello there!";
String forward = s.substring(-5);
String backwards = s.substring(-1, -5);
String backEveryOther = s.substring(-1, -5, -2);
char lastChar = s.charAt(-1);
System.out.println(forward);
System.out.println(backwards);
System.out.println(backEveryOther);
System.out.println(lastChar);

Output:

here!
!ereh
!rh
!
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s