zoukankan      html  css  js  c++  java
  • java基础讲解06-----字符串

    1.

    package test;

    public class chb01 {

            public static void main(String[] ggs)
            {
                /**
                 * 1. 首先String不属于8种基本数据类型,String是一个对象。  因为对象的默认值是null,所以String的默认值也是null;但它又是一种特殊的对象,有其它对象没有的一些特性。
                      2. new String()和new String(“”)都是申明一个新的空字符串,是空串不是null;
                 */
                 String chb="hichb   loveme";
                 System.out.println("字符串的长度:"+chb.length());
                 
                 int a=chb.indexOf("c");
                 System.out.println("从0下标查找c的位置:"+a);
                 
                 char gg=chb.charAt(0);
                 System.out.println("按照下标获取字符串:"+gg);
                 
                 //截取字符串
                 String jieChb=chb.substring(2);
                 System.out.println("截取下标为2后面的字符串(包括下标为2):"+jieChb);
                 
                 
                 String jie2=chb.substring(0, 2);
                 System.out.println("截取下标0-2之间的字符串:"+jie2);
                 
                 //去除字符串中的空格   好像没有效果  哈哈
                 String chuqu=chb.trim();
                 System.out.println("去除字符串中的空格:"+chuqu.length());
                 
                 //字符串替换
                 String tihun=chb.replace("c","C");
                 System.out.println("修改后的样子:"+tihun);
                 
                 //判断字符串的开始,结尾、
                 boolean f=chb.startsWith("h");
                 boolean f1=chb.endsWith("0");
                 System.out.println("判断字符串的开始:"+f);
                 System.out.println("判断字符串的结尾:"+f1);
                 
                 //判断字符串是否相等   
                 //注意:字符串对象,不能简单的用比较运算符 “==” ,因为比较运算符,比较的是两个字符串的地址是否相等
                 //因为这里String是一个对象  c1 c2 是引用了这个对象
                 String c1=new String("chb big");
                 String c2=new String("chb big");
                 String cd1=new String("CHB BIG");
                 boolean c3=(c1==c2);
                 System.out.println("比较运算符  比较的是两个字符串的地址是否相等 :"+c3);
                 
                 boolean c7=(c1.equals(c2));
                 boolean c8=(c1.equals(cd1));
                 boolean c9=(c1.equalsIgnoreCase(cd1));  //equalsIgnoreCase 不区分大小写
                 System.out.println(c7);
                 System.out.println(c8);
                 System.out.println(c9);
                 
                 String  c4="aa";
                 String c5="aa";
                 boolean c6=(c4==c5);
                 System.out.println("比较运算符  :"+c6);
                 
                 //分割字符串
                 String ip="127.0.0.1";
                 String[] arry=ip.split("\.");//以.为分隔符,需要使用转义字符  \.
                 String[] arry2=ip.split("\.",2);
                 for(String aip:arry)
                 {
                     System.out.print("{"+aip+"}");
                 }
                 System.out.println();
                 for(String aip2:arry2)
                 {
                     System.out.print ("{"+aip2+"}");
                 }
                 
            }
    }

    2.

    package test;

    import java.util.Date;

    public class cg {
        
        public static void main(String[] args) {
            
            /**
             * 格式化字符串
             *
             * String 类的静态format()方法用于创建格式化的字符串,它有两种重载的形式
             *
             */
            
            //1.日期和时间字符串格式化
            
            Date date=new Date();
            System.out.println(date);//输出的是: Thu Apr 13 22:50:10 CST 2017
            String date2=String.format("%te", date);//%te  日期格式化转换符 具体百度上有的
            System.out.println(date2);// 输出  13  
            
            //时间转换符
            String hour=String.format("%tH", date);
            
            //日期和时间组合的格式
            
            String time=String.format("%tF", date);
            System.out.println(time);//2017-04-13

            
        
        }

    }

    //一般 我们格式化时间,都会专门写一个工具类的,后面完善吧,一般String常用的就是这些,这些基础,不过多说了

  • 相关阅读:
    GoldenGate HANDLECOLLISIONS参数使用说明
    GoldenGate for bigdata 12.3.2.1版本新特性
    使用GoldenGate EVENTACTIONS执行数据的实时触发和定制化
    基于Docker的GoldenGate部署
    Redis热点数据预写方案
    Redis缓存和数据库双写一致方案
    Redis主从复制原理和高可用方案
    Redis的并发竞争如何解决?如何保证并发写的有序?
    Redis 的单线程模型
    Nginx系列之6:重要模块
  • 原文地址:https://www.cnblogs.com/qq3111901846/p/6705498.html
Copyright © 2011-2022 走看看