How to get the last characters in a String in Java, regardless of String size

get last character of string java
java substring last 4 characters
java string substring
replace last character in string java
find last character of a string
get last 3 characters of a string in java
get second last character in string java
apex get last character from string

I'm looking for a way to pull the last characters from a String, regardless of size. Lets take these strings into example:

"abcd: efg: 1006746"
"bhddy: nshhf36: 1006754"
"hfquv: nd: 5894254"

As you can see, completely random strings, but they have 7 numbers at the end. How would I be able to take those 7 numbers?

Edit:

I just realized that String[] string = s.split(": "); would work great here, as long as I call string[2] for the numbers and string[1] for anything in the middle.

Lots of things you could do.

s.substring(s.lastIndexOf(':') + 1);

will get everything after the last colon.

s.substring(s.lastIndexOf(' ') + 1);

everything after the last space.

String numbers[] = s.split("[^0-9]+");

splits off all sequences of digits; the last element of the numbers array is probably what you want.

Get last 4 characters of String in Java, Learn how to get last 4 characters of a String or simply any number of last characters of a string in Java using string substring() method. Get the string and the index; Create an empty char array of size 1; Copy the element at specific index from String into the char[] using String.getChars() method. Get the specific character at the index 0 of the character array. Return the specific character. Below is the implementation of the above approach:

How about:

String numbers = text.substring(text.length() - 7);

That assumes that there are 7 characters at the end, of course. It will throw an exception if you pass it "12345". You could address that this way:

String numbers = text.substring(Math.max(0, text.length() - 7));

or

String numbers = text.length() <= 7 ? text : text.substring(text.length() - 7);

Note that this still isn't doing any validation that the resulting string contains numbers - and it will still throw an exception if text is null.

Comparing Strings and Portions of Strings (The Java™ Tutorials , The Java Tutorials have been written for JDK 8. The String class has a number of methods for comparing strings and portions of strings. is a String object that represents the same sequence of characters as this object. Region is of length len and begins at the index toffset for this string and ooffset for the other string. Java String lastIndexOf() The java string lastIndexOf() method returns last index of the given character value or substring. If it is not found, it returns -1. The index counter starts from zero. There are 4 types of lastIndexOf method in java.

This question is the top Google result for "Java String Right".

Surprisingly, no-one has yet mentioned Apache Commons StringUtils.right():

String numbers = org.apache.commons.lang.StringUtils.right( text, 7 );

This also handles the case where text is null, where many of the other answers would throw a NullPointerException.

Extract substring from end of a Java String – zParacha.com, Today I'll show you how to extract last few characters (a substring) from a string in Java. Here is the code to do this. public class SubStringEx{. /**. Method to get  The easiest way is to use the built-in substring() method of the String class. In order to remove the last character of a given String, we have to use two parameters: 0 as the starting index, and index of the penultimate character. We can achieve that by calling String‘s length() method and subtracting 1 from the result.

This code works for me perfectly:

String numbers = text.substring(Math.max(0, text.length() - 7));

wordwrap - Manual, wordwrap — Wraps a string to a given number of characters So if you have a word that is larger than the given width, it is broken apart. it calculates wrapping based on font and point-size, rather than character count. the wordwrap() output before using wordwrap, otherwise you will get line breaks inserted regardless  The idea is to match the whole string from ^ to $, capture the last sequence of \w+ in a capturing group 1, and replace the whole sentence with it using $1. Demo. You can do that with StringUtils (from Apache Commons Lang). It avoids index-magic, so it's easier to understand.

You can achieve it using this single line code :

String numbers = text.substring(text.length() - 7, text.length());

But be sure to catch Exception if the input string length is less than 7.

You can replace 7 with any number say N, if you want to get last 'N' characters.

Strings and Drawing Text \ Processing.org, The full documentation can be found on java's String page. If we didn't have the String class, we'd probably have to write some code like this: see if two String objects contain the exact same sequence of characters, regardless textFont() takes one or two arguments, the font variable and the font size, which is optional. letter in string java, find character in string java, check character in string java, character in string java, character in a string java, find a character in string

Arrays and Strings, Let's say that you have an array: A[ 6 ] = {2, 5, 6, 4, 7, 9} and you have to find the sum of In spite of only 5 characters, the size of the string is 6, because the null  I have a string: /abc/def/ghfj.doc I would like to extract ghfj.doc from this, i.e. the substring after the last /, or first / from right. Could someone please provide some help?

4. Pattern Matching with Regular Expressions, Pattern Matching with Regular Expressions Introduction Suppose you have been on the Internet for Consult Table 4-1 for a list of the regular expression characters. End of entire string (except allowable final line terminator) READ_ONLY , 0 , fc . size ()); // Decode ByteBuffer into CharBuffer CharBuffer cbuf = Charset . Java - String length() Method - This method returns the length of this string. The length is equal to the number of 16-bit Unicode characters in the string.

[PDF] Regular Expressions: The Complete Tutorial, your own regular expressions like you have never done anything else. regular expression package included with version 1.4 and later of the Java Similarly, «​$» matches right after the last character in the string. longest match be returned​, regardless if the regex engine is implemented using an NFA or DFA algorithm. Re: Last 5 characters from string (irrespective of the length) Posted 07-17-2014 (145045 views) | In reply to sas_lak Depends if NUMBER is a number or a character string.

Comments
  • Both your question and several answers mention String.split(), but it is worth noting that s.split(": ") is going to compile a new java.uitl.regex.Pattern every time you call it, then match your string with that pattern, creating a regex Matcher and an ArrayList before the String[] that is returned. It will be relatively slow and will allocate far more than necessary to solve this problem. Whether this matters depends on the nature of your application. I generally avoid split() unless I really need it. (Note that split() does not use regex if you split on a single character.)
  • One serendipitous aspect of the lastIndexOf approach is that if there aren't any spaces, lastIndexOf will return -1, so you'll end up with the whole string (s.substring(0)).
  • s.substring(s.lastIndexOf(':') + 1); I would like to use that, but how would I be able to get the letters after the first colon? Wouldn't it be s.indexOf(":", 1)?
  • I wonder too. This way is much more safe than handling all possible problems.
  • This is also the only answer that works if you want/need a single expression to safely convert something to a string and get up to the last n characters. For example, you could do StringUtils.right(Objects.toString(someObj), 7).
  • It's better to check in advance whether the input length is 7 or longer, and then do the substring.
  • Regex should be [0-9]{7}$ to make sure it matches the last 7 digits.
  • @ Willi Schönborn: Agreed. The OP's case suggested that the other groups in the string would always contain letters. I was assuming that it was unnecessary to specify a boundary.