Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to extract the last n characters from a string using Java?
To extract last n characters, simply print (length-n)th character to nth character using the charAt() method.
Example
public class ExtractingCharactersFromStrings {
public static void main(String args[]) {
String str = "Hi welcome to tutorialspoint";
int n = 5;
int initial = str.length()-5;
for(int i=initial; i<str.length(); i++) {
System.out.println(str.charAt(i));
}
}
}
Output
p o i n t
Advertisements