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.
|
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));
}
}
|