Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Java equivalent to Python's Easy String Splicing?

Ok, what I want to know is is there a way with Java to do what Python can do below...

string_sample = "hello world"  string_sample[:-1] >>> "hello world"  string_sample[-1] >>> "d"  string_sample[3] >>> "l" 

Because it seems to me that Java make you work for the same result (I'm particularly getting at having to use 2 numbers every time and the lack of a -1 to indicate last character)

String string_sample = "hello world";  string_sample.substring(0,string_sample.length()-1); >>> "hello world"  string_sample.substringstring_sample.length()]; >>> "d"  string_sample.(3,4); >>> "l" 

I haven't gotten on to arrays/lists yet in Java so really hoping Java has something easier than this

Edit: Ammended 'i' to 'l' for string_sample[3]. Well spotted Maroun!

like image 618
rory Avatar asked Jun 25 '13 21:06

rory


People also ask

Is there string slicing in Java?

Java - String substring() Method This method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string or up to endIndex – 1, if the second argument is given.

Can you index a string in Java?

You can get the character at a particular index within a string by invoking the charAt() accessor method. The index of the first character is 0, while the index of the last character is length()-1 . For example, the following code gets the character at index 9 in a string: String anotherPalindrome = "Niagara.

Can you splice a string python?

You can't do this since strings in Python are immutable.


1 Answers

Sorry, Java's substring is not as flexible as Python's slice notation.

In particular:

  • You can give it just a begin, or a begin and end, but not just an end. (Also, no step, but you don't miss that as much.)
  • Negative indices are an error, not a count from the end.

You can see the docs here.

However, it's not hard at all to write this on your own:

public String slice_start(String s, int startIndex) {     if (startIndex < 0) startIndex = s.length() + startIndex;     return s.substring(startIndex); }  public String slice_end(String s, int endIndex) {     if (endIndex < 0) endIndex = s.length() + endIndex;     return s.substring(0, endIndex); }  public String slice_range(String s, int startIndex, int endIndex) {     if (startIndex < 0) startIndex = s.length() + startIndex;     if (endIndex < 0) endIndex = s.length() + endIndex;     return s.substring(startIndex, endIndex); } 

Put those as static methods of some utility class.

Obviously this isn't exactly the same as Python, but it probably handles all the cases you want, and very simple. If you want to handle other edge cases (including things like step and passing slices around and so on), you can add whatever additional code you want; none of it is particularly tricky.


Other sequences are basically the same, but there you're going to want subSequence instead of substring. (You can also use subSequence on strings, because a String is a CharSequence.)

Arrays aren't actually a type of sequence at all; you'll need to write code that explicitly creates a new Array and copies the sub-array. But it's still not much more complicated.


Note that you may want to look for a library that's already done this for you. There are at least three linked in other answers on this page, which should make your search easy. :) (You may still want to do it for yourself once, just to understand how those libraries work—but for production code, I'd rather use a library where someone else has figured out and tested all of the edge cases than to reimplement the wheel and deal with them as they get caught in unit tests, or errors in the field…)

like image 119
abarnert Avatar answered Sep 21 '22 05:09

abarnert