字符串

package com.hanqi;

import java.util.Random;

public class 字符串 {

    public static void main(String[] args) {
    
        String str1="字符串常量";
                String str2=null;
                str2=new String();
                str2=new String("实例化字符串");
                char[] c = new char[]{'a','b','b'};
                str2=new String(c);
                //字符集
                str2="afdsghjkl";
                
                //str2=new String(bytes);
                
    System.out.println("str2.length="+str2.length());
    System.out.println("str2="+str2);
    
    
    //查找字符或字符串
    int in=str2.indexOf("b");
    
    System.out.println("b="+in);
    
    int xo = str2.lastIndexOf("d");
    System.out.println("d="+xo);
    
    String newStr = str2.substring(5);
    
    newStr=str2.substring(5, 7);
    
    System.out.println("substring()="+newStr);
    
    str2="   a  b c d  d  s  ";
    
    //去除前后空格
    System.out.println("去空格="+str2.trim()+"后面");
    
    
    
    //查找替换
    
    System.out.println("查找和替换="+str2.replace("d", "$")+"后面");
    
    
    str2="abc,你好,cnmd";
    //只替换第一个
    System.out.println("查找和替换="+str2.replaceFirst("abc", "成功"));
    
    
    //判断字符串的开始和结束
    
    
    str2="abcdefgh";
    
    str2.startsWith("a");
    
    System.out.println("判断起始="+str2.startsWith("b"));
    
    
    System.out.println("判断起始="+(str2.indexOf("a")==0));
    
    
    
    System.out.println("判断结束="+str2.endsWith("gh"));
    
    
    str1="abc";//new String("abc");//
    str2="abc";//new String("abc");//
    
    str2="ABC";
    
    //无法判断 字符串,只判断指针指向内容
    System.out.println("判断字符串是否相等="+(str1==str2)+"   str1="+str1.toUpperCase()+"   str2="+str2.toLowerCase());//变大小写
    
    
    System.out.println("判断字符串是否相等="+str1.equals(str2));
    
    
    
    str2="abc#def#ghi#jkl";
    
    //字符串分割
    String[]array = str2.split("#");
    for (int i=0;i<array.length ;i++)
    {
        System.out.println(""+array[i]);
    }
    }
}

 

posted @ 2015-12-17 08:39  琦仔  阅读(152)  评论(0编辑  收藏  举报