George B
Posts: 24
Nickname: hmgeorge
Registered: Feb, 2008
|
|
Re: Substrings
|
Posted: Oct 18, 2010 6:19 AM
|
|
public class Test {
public static void main (String[] args) {
String first = "joe";
String last = "blogg";
String username = take(first, 1) + take(last, 6);
System.out.println("first: [" + first + "] last: [" + last + "] " +
"username: [" + username + "]");
last = "blog";
username = take(first, 1) + take(last, 6);
System.out.println("first: [" + first + "] last: [" + last + "] " +
"username: [" + username + "]");
}
/*
* Returns a substring consisting of the first n characters of
* a string, or else the whole string if it has less than n
* characters.
*/
static String take(String str, int n) {
String substr;
if (str.length() > n) {
substr = str.substring(0, n);
}
else {
substr = str;
}
return substr;
}
}
|
|