C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Thus: The call to right will extract the specified number of characters from the end of the string, and return it as a new string.
Result: With an argument of 4, right() returns the last four chars of a string. With 7, it returns the last seven chars.
Java program that uses right method, gets rightmost chars
public class Program {
public static String right(String value, int length) {
// To get right characters from a string, change the begin index.
return value.substring(value.length() - length);
}
public static void main(String[] args) {
// Test the right method.
String value = "website";
String result = right(value, 4);
System.out.println(result);
value = "Java Virtual Machine";
result = right(value, 7);
System.out.println(result);
}
}
Output
site
Machine