import java.util.Date;
public class JavaString {
public static void main(String[] args) {
//char to string
char a[] = {'g','o','o','d','.'};
String s = new String (a); //全部字符
System.out.println(s);
String ss = new String (a,2,2);//部分字符
System.out.println(ss);
//多个字符串连接
String s1 = new String ("Hello");
String s2 = new String ("World");
String sss = s1 + " "+ s2;
System.out.println(sss);
//连接其它数据类型,int 和float
int booktime = 4;
float practice = 2.5f;
System.out.println("我每天花费"+booktime +"小时;"+practice+"小时上机练习.");
//String str = String.format("我每天花费{1}小时看书;{2}小时上机练 习.",booktime,practice);
//System.out.println(str);
//获得字符串长度:
String str = "We are studentsa";
int size = str.length();
System.out.println("str.length()="+size);
//字符串查找:
int size1 = str.indexOf("a");
System.out.println("字符串查找indexOf(a):" + size1);
int size2 = str.lastIndexOf("a");
System.out.println("字符串查找:lastIndexOf(a)" + size2);
//查找空字符串:
int size3 = str.indexOf(" ");//int size3 = str.indexOf("");
System.out.println("空字符串的位置:"+ size3);
//获得子字符串&#xff1a;//beginIndex <&#61; endIndex
String substr &#61; str.substring(3,4);//String substr &#61; str.substring(3);
System.out.println("获得子串:"&#43;substr);
//去掉空格&#xff1a;
String javaStr &#61; " Java Class";
System.out.println("字符串原来的长度&#xff1a;"&#43; javaStr.length());
System.out.println("去掉空格后字符串的长度&#xff1a;"&#43;javaStr.trim().length());
//字符串替换
String address &#61; "address";
String newstr &#61; address.replace("a", "A");
System.out.println(newstr);
//判断字符串的开始与结尾&#xff1a;
String num1 &#61; "22045612";
String num2 &#61; "21304578";
boolean b &#61; num1.startsWith("22");
boolean b2 &#61; num1.endsWith("78");
boolean b3 &#61; num2.startsWith("22");
boolean b4 &#61; num2.endsWith("78");
System.out.println("字符串num1是以&#39;22&#39;开始的吗&#xff1f;"&#43;b);
System.out.println("字符串num1是以&#39;78&#39;结尾的吗&#xff1f;"&#43;b2);
System.out.println("字符串num1是以&#39;22&#39;开始的吗&#xff1f;"&#43;b3);
System.out.println("字符串num1是以&#39;78&#39;结尾的吗&#xff1f;"&#43;b4);
//判断字符串是否相等
String tom &#61; new String("I am");
String jerry &#61; new String("I am");
boolean bb &#61;(tom&#61;&#61;jerry);
boolean bbb &#61; tom.equals(jerry); //equalsIgnoreCase
System.out.println("tom&#61;&#61;jerry:"&#43;bb);
System.out.println("tom.equals(jerry):"&#43;bbb);
//字母大小写转换&#xff1a;
String mystr &#61; new String("abc DEF");
String newstrs &#61; mystr.toLowerCase();
String newstrs2 &#61; mystr.toUpperCase();
System.out.println(newstrs);
System.out.println(newstrs2);
//字符串分割&#xff1a;
String splitStr &#61; new String("abc,def,ghi,gkl");
String [] newsplitstr &#61; splitStr.split(",");
for(int i&#61;0;i
{
System.out.println(newsplitstr[i]);
}
String [] newsplitstr2 &#61; splitStr.split(",",2);//限定拆分次数
for(int i&#61;0;i
{
System.out.println(newsplitstr2[i]);
}
//格式化字符串&#xff1a;
//日期格式化
//%te 一个月中的某一天(1-31)
//%tb 月份
//%tB 月份的全称
//%tA 星期几全称
//%ta 星期几
//%tc
//%tY 4位年份(2009)
//%tj 一年中的第几天(001-366)
//%tm 月份
//%td 一个月中的第几天(01-31)
//%ty 2位年份
Date date &#61; new Date();
String dates &#61; String.format("%te", date);
String datedays &#61; String.format("%tj", date);
System.out.println("日期&#xff1a;"&#43;dates);
System.out.println("一年中的第几天&#xff1a;"&#43;datedays);
//时间格式化&#xff1a;
//%tH 2位24小时数&#xff1a;(00-23)
//%tI 2位12小时数&#xff1a;(01-12)
//%tk 2位24小时数&#xff1a;(0-23)
//%tl 2位12小时数&#xff1a;(1-12)
//%tM 2位数字的分钟(00-59)
//%tS 2位数字的秒(00-60)
//%tL 8位毫秒(000-999)
//%tN 9位毫秒()
//%tp 上下午&#xff0c;
//%tz
//%tZ
//%ts 1970-01-01 00:00:00至今经过的秒
//%tQ 1970-01-01 00:00:00至今经过的毫秒
String hour &#61; String.format("%tH", date);
String minute &#61; String.format("%tM", date);
String second &#61; String.format("%tS", date);
String msecond &#61; String.format("%tQ", date);
System.out.println("现在是"&#43;hour&#43;"时"&#43;minute&#43;"分"&#43;second&#43;"秒");
System.out.println("现在是毫秒&#xff1a;"&#43;msecond);
//日期时间组合&#xff1a;
//%tF 年-月-日
//%tD 月/日/年
//%tc 全部日期和时间信息
//%tr 时分秒上下午
//%tT 时分秒
//%tR 时分
String time &#61; String.format("%tc", date);
String form &#61; String.format("%tF", date);
String sfm &#61; String.format("%tT", date);
System.out.println("全部时间信息&#xff1a;"&#43;time);
System.out.println("年-月-日格式&#xff1a;"&#43;form);
System.out.println("时分秒&#xff1a;"&#43; sfm);
System.out.println("date.toString():" &#43; date.toString());//date.toLocaleString()
System.out.println("date.toLocaleString():" &#43; date.toLocaleString());
//使用正则表达式
//字符串生成器&#xff1a;StringBuilder
//builder.append();insert(int offset arg);delete(int start,int end);
StringBuilder bf &#61; new StringBuilder("Hello");
bf.insert(5, "word");
System.out.println(bf.toString());
}
}