涉及知识: StringBuffer类: append() insert() substring() toCharArray() toString() startwith() endwith() indexof() equals() charAt()
实现:字符串追加、字符串插入、字符串切割、字符串转字符串数组、string Buffer转string
代码:
public class test1 {
public static void main(String[] args) {
String ID="610101199204150102";
System.out.println(ID.substring(0, 2).equals("61"));
System.out.println(ID.startsWith("61"));
System.out.println(ID.substring(6).startsWith("1992"));
System.out.println(ID.substring(0,10).endsWith("1992"));
System.out.println(ID.indexOf("61")==0);
System.out.println(ID.indexOf("1992")==6);
//字符串转字符串数组
char[] chs=ID.toCharArray();
String str=new String(chs,0,2);
String str1=new String(chs,6,4);
System.out.println(str.equals("61"));
System.out.println(str1.equals("1992"));
System.out.println(contactInfo1(ID));
}
public static String contactInfo1(String ID)
{
StringBuffer result=new StringBuffer("出生于");
result.append(ID.substring(6,10)+"年");
if(ID.charAt(10)=='0')
{
result.append(ID.charAt(11)+"月");
}
else
{
result.append(ID.substring(10, 12)+"月");
}
if(ID.charAt(12)=='0')
{
result.append(ID.charAt(13)+"日");
}
else
{
result.append(ID.substring(12, 14)+"日");
}
result.insert(0, "大叔");
return result.toString();
}
}
运行结果: