Knowledge Walls
John Peter
Pune, Maharashtra, India
Custom Left and Right Method of String manipulation in Java Example
1314 Views
CustomLeftRightMethodExample
public class CustomLeftRightMethodExample {
    public static String left(String data,int no_of_chars){
        if (data.length() > no_of_chars)
            return data.substring(0, no_of_chars);
        else
            return data;
    }
    public static String right(String data,int no_of_chars){
        if (data.length() > no_of_chars)
            return data.substring(data.length()-no_of_chars, data.length());
        else
            return data;
    }
    
    public static void main(String args[]) throws Exception{    
        System.out.println(left("Parthasarathy",3));
        System.out.println(right("Parthasarathy",3));
    }
}
Output 
Par
thy
Hints 
String Class substring method is used to parse the string using staring index and end index.

substring(starting index,ending index)

Left: left string has to specify 0 as starting index. and number of characters should be ending index.
Right: right string has to specify (length - number of characters) as starting index and number of characters is the ending index.
Previous Topics
Previous lessons of current book.
Computer software engineer articles of One day One Thing to Know
Best Lessons of "One day One Thing to Know"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details