String字符串拼接,截取,查找及日期转换

转自:http://www.2cto.com/kf/201109/105006.html
import java.util.Date;
public class JavaString {
 
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
      //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);
 //获得子字符串://beginIndex <= endIndex
  String substr = str.substring(3,4);//String substr = str.substring(3);
  System.out.println("获得子串:"+substr);
 //去掉空格:
  String javaStr = " Java Class";
  System.out.println("字符串原来的长度:"+ javaStr.length());
  System.out.println("去掉空格后字符串的长度:"+javaStr.trim().length());
 //字符串替换
  String address = "address";
  String newstr = address.replace("a", "A");
  System.out.println(newstr);
 //判断字符串的开始与结尾:
  String num1 = "22045612";
  String num2 = "21304578";
  boolean b = num1.startsWith("22");
  boolean b2 = num1.endsWith("78");
  boolean b3 = num2.startsWith("22");
  boolean b4 = num2.endsWith("78");
  System.out.println("字符串num1是以'22'开始的吗?"+b);
  System.out.println("字符串num1是以'78'结尾的吗?"+b2);
  System.out.println("字符串num1是以'22'开始的吗?"+b3);
  System.out.println("字符串num1是以'78'结尾的吗?"+b4);
 //判断字符串是否相等
  String tom = new String("I am");
  String jerry = new String("I am");
  boolean bb =(tom==jerry);
  boolean bbb = tom.equals(jerry); //equalsIgnoreCase
  System.out.println("tom==jerry:"+bb);
  System.out.println("tom.equals(jerry):"+bbb);
 //字母大小写转换:
  String mystr = new String("abc DEF");
  String newstrs = mystr.toLowerCase();
  String newstrs2 = mystr.toUpperCase();
  System.out.println(newstrs);
  System.out.println(newstrs2);
 //字符串分割:
     String splitStr = new String("abc,def,ghi,gkl");
     String [] newsplitstr = splitStr.split(",");
     for(int i=0;i<newsplitstr.length;i++)
     {
      System.out.println(newsplitstr[i]);
     }
     String [] newsplitstr2 = splitStr.split(",",2);//限定拆分次数
     for(int i=0;i<newsplitstr2.length;i++)
     {
      System.out.println(newsplitstr2[i]);
     }
 //格式化字符串:
     //日期格式化
     //%te   一个月中的某一天(1-31)
     //%tb  月份
     //%tB  月份的全称
     //%tA 星期几全称
     //%ta 星期几
     //%tc
     //%tY  4位年份(2009)
     //%tj 一年中的第几天(001-366)
     //%tm 月份
     //%td 一个月中的第几天(01-31)
     //%ty 2位年份
     Date date = new Date();
     String dates = String.format("%te", date);
     String datedays = String.format("%tj", date);
     System.out.println("日期:"+dates);
     System.out.println("一年中的第几天:"+datedays);
    //时间格式化:
   //%tH 2位24小时数:(00-23)
   //%tI 2位12小时数:(01-12)
   //%tk 2位24小时数:(0-23)
   //%tl 2位12小时数:(1-12)
   //%tM 2位数字的分钟(00-59)
   //%tS 2位数字的秒(00-60)
   //%tL 8位毫秒(000-999)
   //%tN 9位毫秒()
   //%tp  上下午,
   //%tz
   //%tZ
   //%ts 1970-01-01 00:00:00至今经过的秒
   //%tQ 1970-01-01 00:00:00至今经过的毫秒
     String hour = String.format("%tH", date);
     String minute = String.format("%tM", date);
     String second = String.format("%tS", date);
     String msecond = String.format("%tQ", date);
     System.out.println("现在是"+hour+"时"+minute+"分"+second+"秒");
     System.out.println("现在是毫秒:"+msecond);
     //日期时间组合:
     //%tF 年-月-日
     //%tD 月/日/年
     //%tc 全部日期和时间信息
     //%tr 时分秒上下午
     //%tT 时分秒
     //%tR 时分
     String time = String.format("%tc", date);
     String form = String.format("%tF", date);
     String sfm  = String.format("%tT", date);
     System.out.println("全部时间信息:"+time);
     System.out.println("年-月-日格式:"+form);
     System.out.println("时分秒:"+ sfm);
     System.out.println("date.toString():" + date.toString());//date.toLocaleString()
     System.out.println("date.toLocaleString():" + date.toLocaleString());
 //使用正则表达式
 //字符串生成器:StringBuilder
     //builder.append();insert(int offset arg);delete(int start,int end);
     StringBuilder bf = new StringBuilder("Hello");
     bf.insert(5, "word");
     System.out.println(bf.toString());
 }
 
}


你可能感兴趣的:(String)