Is it possible to get substring from right hand hand(Reverse) direction using `substring()` in JAVA.
Example.
 Suppose `String S="abcdef"`,
 Can I get Substring "fedc" using `S.substring(S.length()-1,3)` ?
If it is not correct, please suggest me how to get Substring from right hand end(Reverse direction)??
['https://Stackoverflow.com/questions/51500657', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/10128375/']
You could reverse the string and use `substring`. Unfortunately `String` does not have that, but `StringBuilder` has it e.g.

```
new StringBuilder("abcdef").reverse().toString().substring(0,4);

```
Java doesn't support extension methods like C# does, so I would build a function for this. This way you can control how much of the reverse substring you want with a parameter.

```
public class StackOverflow {
    public static void main(String[] args) {
        String data = "abcdef";
        for (int i = 0; i < data.length(); i++) {
            System.out.println(reverseSubstring(data, i+1));
        }
    } 

    public static String reverseSubstring(String data, int length) {
        return new StringBuilder(data).reverse().substring(0, length);
    }
}

```

Result:

```
f
fe
fed
fedc
fedcb
fedcba

```

UPDATE
------

Another approach is to create a wrapper class to String. This way you can call a class method like how you're asking in your question with the example `S.substring(S.length()-1,3)`. This will also allow you to still have all the `String` methods after using the wrapper's `get()` method.

String Wrapper
--------------

```
public class MyString {
    private String theString;

    public MyString(String s) {
        theString = s;
    }

    public String get() {
        return theString;
    }

    public String reverseSubstring(int length) {
        return new StringBuilder(theString).reverse().substring(0, length);
    }
}

```

Usage
-----

```
public class StackOverflow {
    public static void main(String[] args) {
        MyString data = new MyString("abcdef");
        for (int i = 0; i < data.get().length(); i++) {
            System.out.println(data.reverseSubstring(i+1));
        }
    } 
}

```

Results:

```
f
fe
fed
fedc
fedcb
fedcba

```