第十五章 String讲解

package ch15;    
    
import java.util.Scanner;    
    
public class Test    
{    
    public static void main(String[] args)    
    {    
        /* .         * byte short int long float double char boolean   
        */    
        // 我叫什么    
        char[] cs1 = new char[] { '我', '叫', '王', '亚', '杰' };    
        char[] cs2 = new char[] { '我', '叫', '王', '亚', '杰' };    
        char[] cs3 = { '我', '叫', '王', '亚', '杰' };    
        char[] cs4 = cs1;    
        System.out.println(cs4 == cs1);    
        System.out.println(cs2 == cs3);    
    
        // for (int i = 0; i < cs.length; i++)    
        // {    
        // System.out.print(cs[i]);    
        // }    
        // 第一个 要迭代数组的数据类型    
        // 是迭代的变量名    
        // 要迭代的数组名    
        // 好处,避免数组下标越界    
        // java.lang.ArrayIndexOutOfBoundsException    
        for (char c : cs1)    
        {    
            System.out.print(c);    
        }    
   
        boolean b = compar(cs1, cs3);    
        System.out.println(b);    
        System.out.println("******************");    
        String str1 = "我叫王亚杰";    
       String str2 = new String("我叫王亚杰");    
        String str3 = "我叫王亚杰";    
    
        // str3=str1;    
    
        // 获取到字符串长的长度    
        System.out.println(str1.length());    
        System.out.println();    
        // 这两个的值是否相等    
        // equals 比较两个值是否相等的    
        System.out.println(str1.equals(str2));    
        // str1==str2 地址是否相等    
       System.out.println(str1 == str2);    
       System.out.println(str1 == str3);    
       System.out.println("******************");    
        char c1 = 'A';// 65---91    
        char c2 = 'a'; // 97--123    
        System.out.println(c1 - c2);    
        System.out.println((char) (c1 + 32));    
        // 不区分大小写 比较    
        System.out.println(c1 == c2);    
        System.out.println('a' == 97);    
   
        b = compar2(c1, c2);    
        System.out.println(b);    
            
        // 验证码  不区分大小写    
        String str4="abc";    
        String str5="ABC";    
        // 忽略大小写比较    
        System.out.println(str4.equalsIgnoreCase(str5));    
        // 全部变成大写    
        System.out.println(str4.toUpperCase());    
        // 全部小写    
        System.out.println(str5.toLowerCase());    
        // abcABC    
      String str6=str4+str5;    
//       拼接字符串    
        String str6=str4.concat(str5);    
        System.out.println(str6);    
        System.out.println("******************************");    
             
        // 985860612@qq.com    
         // 在字符串中查找一个字符,如果找不到那么就返回-1  如果找到了返回字符所在的位置    
        String str7="985860612@qq.com";    
        int index=str7.indexOf('@');    
        System.out.println(index);    
        index=str7.indexOf('#');    
        System.out.println(index);    
            
        index= str7.indexOf("qq");// 返回找到开始位置    
        System.out.println(index);    
        System.out.println(8=='8');    
        index=str7.indexOf('8', 1);// 第一个参数要查找的字符   第二个参数 从第几位开始查找    
        System.out.println(index);    
        index=str7.lastIndexOf('8');// 最后出现的位置    
        System.out.println(index);//    
        // 截取字符串        从第几位开始截取(从0开始数,包含你数到的那一位)    
        String com=str7.substring(10);//     
        System.out.println(com);    
        String qq=str7.substring(0, 9);// 鸵鸟  顾头不顾尾    
        System.out.println(qq);    
            
            
        //     
        String str8="          985860612      ";    
      Scanner input = new Scanner(System.in);    
     System.out.println("请输入用户名");    
      String name=input.next();    
      if(name.equals(str8.trim())){    
          System.out.println("登录成功");    
      }else{    
          System.out.println("登录失败");    
      }    
       // 去除空格    
        // 去除空格    
        System.out.println(str8);    
        System.out.println(str8.trim());    
            
        // ***************欢*迎*光*临****************    
        String str9="欢迎光临";    
        int count=8;    
        char [] cs5=str9.toCharArray();//把字符转成字符数组    
            
        for (int i = 0; i < count; i++)    
        {    
            System.out.print("*");    
        }    
            
       for (int i = 0; i < cs5.length; i++)    
        {    
            System.out.print(cs5[i]+"*");    
       }    
            
        for (int i = 0; i < count-1; i++)    
        {    
            System.out.print("*");    
        }    
        System.out.println("/************************************");    
            
        String str10="欢 迎 光 临";    
        String[] strs=  str10.split(" ");// 拆分字符串    
        String str11="";    
            
        for (String s : strs)    
        {    
            System.out.println(s);    
                
            str11+=s;    
        }    
        System.out.println(str11);    
        String[] strs2=str7.split("6"); // 铁链    
        for (String s : strs2)    
        {    
            System.out.println(s);    
        }    
    }    
    
    // 不区分大小写比较字符    
    private static boolean compar2(char c1, char c2)    
    {    
       // 统一大写比较    
        if (c1 < 65 || c1 > 91)    
        {    
            if (c1 >= 97 && c1 <= 123)    
           {    
                c1 = (char) (c1 - 32);    
            }    
    
        }    
        if (c2 < 65 || c2 > 91)    
        {    
            if (c2 >= 97 && c2 <= 123)    
            {    
                c2 = (char) (c2 - 32);    
            }    
    
        }    
        return c1==c2;    
    }    
    
    private static boolean compar(char[] cs1, char[] cs2)    
    {    
        /*    
         * 数据类型 [] 数组名; 数组名=new 数据类型[长度]; 获取数组长度 数组名.length (这是一个属性)    
         */    
        // == 比较的是内存地址    
        if (cs1 != null && cs2 != null)    
       {    
            if (cs1 == cs2)    
            {    
                return true;    
            }    
            if (cs1.length == cs2.length)    
            {    
                for (int i = 0; i < cs2.length; i++)    
                {    
                    if (cs1[i] != cs2[i])    
                    {    
                        return false;    
                    }    
                }    
                return true;    
            }    
        }    
        return false;    
    
    }    
}    
  
  
  
  
[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片  
package ch15;    
    
public class Test02    
{    
    public static void main(String[] args)    
    {    
    
        String str="1";    
        str+=2;    
        System.out.println(str);    
        StringBuffer sb=new StringBuffer();    
        sb.append("1");    
        sb.append("2");// 拼接    
        sb.append("3");    
        sb.insert(1, 'a');// 插入    
        System.out.println(sb.toString());    
            
            
            
        // 记录开始时间592    
        long start = System.currentTimeMillis();    
    
      for (int i = 0; i < 1000; i++)    
     {    
         str+=i;    
      }    
   
       // 记录结束时间,=开始时间    
        System.out.println(System.currentTimeMillis() - start);    
    }    
}    


原文地址:https://www.cnblogs.com/fuhaots2009/p/3483566.html