zoukankan      html  css  js  c++  java
  • 3.字符串

    字符串(String类型看做是对象,引用类型)
    字符串相关基础知识
    1. 在java中字符串必须包含一对双引号之间  
    “abcd”
    2.声明方式     
     String str;
    3.使用字符串变量时要先初始化
    4.创建字符串   
    1.String s=new String(“good”);      
    开辟两块推内存空间,有一块成为垃圾,并且不能自动入池,需要使用intern()手工入池
    2. String str; str=”good”
    只开辟一块推内存空间,字符串内容可以自动入池,以供下次使用。
    5.String s=new String(“good”);  创建了几个String类的实例化对象?
    创建了两个实例化对象,一个是String类的匿名对象"good",另一个是关键字new实例化的String类对象
    6.字符串一旦声明,则不可更改,属于final类型
    每一次字符串进行字符串连接操作时都会产生垃圾,并且字符串的内容没有任何改变,改变的只是字符串的内存地址指向,所以对于字符串的修改操作是有垃圾产生的
    字符串常用的相关方法
    5.2连接字符串
    11.连接多个字符串
       String a=new String("hello");
        String b=new String("world");
        System.out.println(a+" "+b);
    2. 连接其他数据类型
      int booktime=4;
           float sleeptime=2.5f;//默认为double
        String a=new String("睡觉的时间为");
        String b=new String("看书的时间为");
        System.out.println(a+sleeptime+b+booktime);
    5.3获取字符串信息
    1. 获取字符串长度
     String a=new String("睡觉的时间为");
        int size=a.length();
        System.out.println(size);//6
    2. 字符串查找
    1. indexOf()
      String a=new String("睡觉的时间为");
        int size=a.indexOf("的");
        System.out.println(size);//从0开始
    2. lastIndexOf()
     String a=new String("睡觉的时间为的");
        int size=a.lastIndexOf("的");
        System.out.println(size);//6 最后一次出现
    3. charAt(int index) 取得索引位置的字符
     String b=new String("hello");
        char size=b.charAt(4);
        System.out.println(size);//o
    5.4字符串操作
    1. 获取子字符串
    1. substring(int beginindex)
     String b=new String("hello");
              String c=b.substring(1);
      System.out.println(c);//ello
     2.substring(int beginindex ,int endindex)
               String b=new String("hello");
               String c=b.substring(0,3);
     System.out.println(c);//hel   不包括结束字符
    2.去除空格
      String b=new String(" hello ");
         String c=b.trim();
         int e=c.length();
         int f=b.length();
         System.out.println(f);//7
         System.out.println(b);//“ hello “  最前和最后的空格
         System.out.println(c);//”hello”
         System.out.println(e);//5
    3.字符串替换
    Str.replace()  
     String a=new String("hello");
    String b=a.replace("h", "e");
    System.out.println(b); //eello
    4. 判断字符串的开始与结尾
    String a=new String("hello");
            boolean num1=a.endsWith("o");
        System.out.println(num1);//true
     
                     String a=new String("hello");
            boolean num1=a.startsWith("h");
        System.out.println(num1);//true
    5. 判断字符串是否相等
    String a=new String("hello");
              String b=new String("hello");
              boolean c=(a==b);
            System.out.println(c);//false 虽然内容相同,但是对象引用不同,分配的内存地址不同
    1. equals(String  otheString)
    String a=new String("hello");
             String b=new String("hello");
             boolean c=a.equals(b);
    System.out.println(c);//true
    2. equalsignoreCase(Sring otherstr)
        String a=new String("hello");
                String b=new String("Hello");
                boolean c=a.equalsIgnoreCase(b);
        System.out.println(c);//true
     
    6. 按字典顺序比较两个字符串
    String str=new String("b");
     String str2=new String("a");
     String str3=new String("c");
     System.out.println("abc") ;
             System.out.println(str+" compareto" +str2 +":"+str.compareTo(str2));
         System.out.println(str+" compareto" +str3 +":"+str.compareTo(str3));    
    //b a 返回1  str与str2对比
    //b c 返回-1  str与str3对比
    7. 字母大小写转换
    1. toLowerCase()
    String str=new String("B");
     String str2=new String("a");
     String str3=new String("c");
     String str4=str.toLowerCase();  
    System.out.println(str4);//b
    2. toUpperCase()
      String str=new String("baBa");
      String str4=str.toUpperCase();  
    System.out.println(str4);//BABA
    8. 字符串分割
    1. split(String sign)
    2. Split(String sign,int limit)
    String str=new String("192.168.1.1");
     String[] firstArray=str.split("\.");//全部分割,存储在字符型数组
     String[] secondArray=str.split("\.",2);//分割次数 分割后的数组数
     System.out.println(str);
     for(String a:firstArray) {
     System.out.print("["+a+"]");}//字符型数组的输出方式
     System.out.println();//换行
     for(String a:secondArray) {
     System.out.print("["+a+"]");
     }
     System.out.println();
    }
     
     
    字符串与字节
    public String(byte[] bytes)      将全部的字节数组变为字符串  
    public String(byte[] bytes,int offset,int length)    将部分的字节数组变为字符串
    public byte[] getBytes()    将字符串变为字节数组
    public byte[] getBytes(String charsetName) throws UnsupportedEncoingException 字符串转码操作

  • 相关阅读:
    poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)
    poj 2060 Taxi Cab Scheme (最小路径覆盖)
    poj 2728 Desert King (最小比例生成树)
    poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)
    poj 3463 Sightseeing( 最短路与次短路)
    研究生flag
    插入排序和堆排序
    根据二叉树的中序遍历和层次遍历还原二叉树
    关于AVL实现的代码记录
    回文数猜想(与6174问题很像)
  • 原文地址:https://www.cnblogs.com/cainame/p/10091895.html
Copyright © 2011-2022 走看看