0

I have a java string containing n number of characters.

How do I extract the LAST few characters from that string?

I found this piece of code online that extracts the first few, but I dont understand it so I cant modify it.

Can anyone help me with this?

String upToNCharacters = s.substring(0, Math.min(s.length(), n));
Noob_Programmer
  • 111
  • 5
  • 14

5 Answers5

2

Try,

String upToNCharacters = s.substring(s.length()-lastCharNumber);
Masudul
  • 21,823
  • 5
  • 43
  • 58
2

That piece of code does exactly the opposite of what you want. Now let's see why and how we can modify it.

Quick solution

You can modify the code as follows to do what you want:

 String lastNchars = s.substring( Math.max(0, s.length()-n));

Explanation

According to the official documentation, Java String class has a special method called substring(). The signature of the method is the following (with overload):

 public String substring(int beginIndex, int endIndex))
 public String substring(int beginIndex)

The first method accepts 2 parameters as input:

  1. beginIndex: the begin index of the substring, inclusive.
  2. endIndex: the end index of the substring, exclusive.

The second overload will automatically consider as endIndex the length of the string, thus returning "the last part"

Both methods return a new String Object instance according to the input parameters just described.

How do you pick up the right sub-string from a string? The hint is to think at the strings as they are: an array of chars. So, if you have the string Hello world you can logically think of it as:

[H][e][l][l][o][ ][w][o][r][l][d]
[0]...............[6]......[9][10]

If you choose to extract only the string world you can thus call the substring method giving the right "array" indexes (remember the endIndex is exclusive!):

 String s = "Hello world";
 s.substring(6,11);

In the code snippet you provided, you give a special endIndex:

 Math.min(s.length(), n);

That is exactly up to the n th char index taking into account the length of the string (to avoid out of bound conditions).

What we did at the very beginning of this answer was just calling the method and providing it with the beginning index of the substring, taking into account the possible overflow condition if you choose a wrong index.

Please note that any String Object instance can take advantage of this method, take a look at this example, for instance:

 System.out.println("abc");
 String cde = "cde";
 System.out.println("abc" + cde);
 String c = "abc".substring(2,3);
 String d = cde.substring(1, 2);

As you see even "abc", of course, has the substring method!

Alex Gidan
  • 2,619
  • 17
  • 29
0

Have a look at the substring documentation, Basically what it does is, it returns a substring of the string on which it is called, where substring from the index specified by the first parameter and the ends at the second parameter.

So, to get the last few characters, modify the begin index and the end index to the values you need. There is also another version of this method which takes only one parameter, just the begin index, which might be useful for you.

anirudh
  • 4,116
  • 2
  • 20
  • 35
0
String lastNchars = s.substring(s.length()-n);
TulaGingerbread
  • 435
  • 5
  • 15
0

One of the String.substring() overloads takes one parameter - the index of the starting index. From that, you can easily implement your function :

String lastFew(String s, int number) {
    if (number > s.length()) {
        throw new IllegalArgumentException("The string is too short!");
    } else return s.substring(s.length()-number);
 }
Danstahr
  • 4,190
  • 22
  • 38