创建一个程序,它将MM/DD/YYYY格式 (例如4/23/2000) 这样的生日转换为3个单独的字符串。
class Birthday {
public static void main(String[] arguments) {
String birthday = "08/23/2002";
String month = birthday.substring(0, 2);
String day = birthday.substring(3, 5);
String year = birthday.substring(6, 10);
System.out.println("Birthday: " + birthday);
System.out.println("Month: " + month);
System.out.println("Day: " + day);
System.out.println("Year: " + year); }
}
为什么这个要这样写 birthday.substring(0, 2);
birthday.substring(3, 5);
birthday.substring(6, 10);
请具体解释一下 最好在具例说明! 非常谢谢!
[code="java"] /**
* Returns a new string that is a substring of this string. The
* substring begins at the specified beginIndex
and
* extends to the character at index endIndex - 1
.
* Thus the length of the substring is endIndex-beginIndex
.
*
* Examples:
*
* "hamburger".substring(4, 8) returns "urge"
* "smiles".substring(1, 5) returns "mile"
*
beginIndex
is negative, orendIndex
is larger than the length ofString
object, orbeginIndex
is larger thanendIndex
.