Java 基础知识(2)


[TOC]

面向对象基础知识(二)

string基本概念

为string类对象直接赋值

public class StringDemo {
    public static void main(String args[]) {
        String str = "www.baidu.com"; // 直接赋值
        System.out.println(str); // 输出字符串数据
    }
}

利用构造方法实例化

public class StringDemo {
    public static void main(String args[]) {
        String str = new String("www.baidu.com"); // 直接赋值
        System.out.println(str); // 输出字符串数据
    }
}

在string对象上使用等号比较

  • 在string对象上使用”==”比较
public class StringDemo {
    public static void main(String args[]) {
        String stra = "hello"; // 直接赋值定义字符串
        String strb = new String("hello"); // 构造方法定义字符串
        String strc = strb; // 引用传递
        System.out.println(stra == strb); // 比较结果:false
        System.out.println(stra == strc); // 比较结果:false
        System.out.println(strb == strc); // 比较结果:true
    }
}

实现字符串内容比较

public class StringDemo {
    public static void main(String args[]) {
        String stra = "hello"; // 直接赋值定义字符串
        String strb = new String("hello"); // 构造方法定义字符串
        String strc = strb; // 引用传递
        System.out.println(stra.equals(strb)) ; // 比较结果:true
        System.out.println(stra.equals(strc)) ; // 比较结果:true
        System.out.println(strb.equals(strc)) ; // 比较结果:true
    }
}

直接赋值时的堆内存自动引用

public class StringDemo {
    public static void main(String args[]) {
        String stra = "hello"; // 直接赋值实例化
        String strb = "hello"; // 直接赋值实例化
        String strc = "hello"; // 直接赋值实例化
        String strd = "yootk" ; // 直接赋值实例化,内容不相同
        System.out.println(stra == strb); // 判断结果:true
        System.out.println(stra == strc); // 判断结果:true
        System.out.println(strb == strc); // 判断结果:true
        System.out.println(stra == strd); // 判断结果:false
    }
}

不自动保存对象池操作

public class StringDemo {
    public static void main(String args[]) {
        String stra = new String("hello"); // 使用构造方法定义了新的内存空间,不会自动入池
        String strb = "hello"; // 直接赋值
        System.out.println(stra == strb); // 判断结果:false
    }
}

手工入池

public class StringDemo {
    public static void main(String args[]) {
        String stra = new String("hello").intern(); // 使用构造方法定义新的内存空间,手工入池
        String strb = "hello"; // 直接赋值
        System.out.println(stra == strb); // 判断结果:true
    }ja
}

修改字符串对象引用

public class StringDemo {
    public static void main(String args[]) {
        String str = "Hello "; // 直接赋值实例化String类对象
        str = str + "World "; // 字符串连接,同时修改String类对象的引用关系
        str += "!!!"; // 字符串连接,同时修改String类对象的引用关系
        System.out.println(str); // 输出当前的String类对象内容
        //Hello World !!!
    }
}

string类的常用方法

取出指定索引的字符

public class StringDemo {
    public static void main(String args[]) {
        String str = "hello"; // 定义字符串对象
        char c = str.charAt(0); // 截取第一个字符
        System.out.println(c); // 输出字符
        // h
    }
}

字符数组与字符串的转换

public class StringDemo {
    public static void main(String args[]) {
        String str = "hello"; // 定义字符串
        char[] data = str.toCharArray(); // 将字符串变为字符数组
        for (int x = 0; x < data.length; x++) { // 循环输出每一个字符
            System.out.print(data[x] + "、");
        }
        // h、e、l、l、o、
    }
}

将字符串转为大写

public class StringDemo {
    public static void main(String args[]) {
        String str = "hello"; // 字符串由小写字母组成
        char[] data = str.toCharArray(); // 将字符串变为字符数组
        for (int x = 0; x < data.length; x++) { // 改变每一个字符的编码值
            data[x] -= 32;
        }
        System.out.println(new String(data)); // 将全部字符数组变为String
        System.out.println(new String(data, 1, 2)); // 将部分字符数组变为String
        // HELLO
        // EL
    }
}

判断字符串是否由数字组成

public class StringDemo {
    public static void main(String args[]) {
        String str = "123423432";
        if (isNumber(str)) {
          System.out.println("字符串由数字组成!");
        } else {
          System.out.println("字符串由非数字组成!");
        }
    }
    /**
     * 判断字符串是否由数字所组成
     * @param temp 要判断的字符串数据
     * @return 如果字符串由数字组成返回true,否则返回false
     */
    public static boolean isNumber(String temp) {
        char[] data = temp.toCharArray(); // 将字符串变为字符数组,可以取出每一位字符进行判断
        for (int x = 0; x < data.length; x++) { // 循环判断
            if (data[x] > '9' || data[x] < '0') { // 不是数字字符范围
              return false; // 后续不再判断
            }
        }
        return true; // 如果全部验证通过返回true
    }
}

字符串与字节数组的转换

public class StringDemo {
    public static void main(String args[]) {
        String str = "helloworld"; // 定义字符串
        byte[] data = str.getBytes(); // 将字符串变为字节数组
        for (int x = 0; x < data.length; x++) {
            data[x] -= 32; // 将小写字母变为大写形式
        }
        System.out.println(new String(data)); // 全部转换
        System.out.println(new String(data, 5, 5)); // 部分转换
        /*
        HELLOWORLD
        WORLD
        */
    }
}

字符串比较的方法

  • compareTo()方法
public class StringDemo {
    public static void main(String args[]) {
        String stra = "Hello"; // 定义字符串对象
        String strb = "HEllo"; // 定义字符串对象
        System.out.println(stra.compareTo(strb)); // 32,大于0
        if (stra.compareTo(strb) > 0) { // 可以利用大小等于0的方式来判断大小
           System.out.println("大于");
        }
        /*
        32
        大于
        */
    }
}

字符串查找的方法

  • 使用indexOf()等功能查找
public class StringDemo {
    public static void main(String args[]) {
        String str = "helloworld"; // 实例化字符串对象
        System.out.println(str.indexOf("world")); // 返回满足条件单词的第一个字母的索引
        // 5
        System.out.println(str.indexOf("l")); // 返回的是第一个查找到的子字符串位置
        // 2 
        System.out.println(str.indexOf("l", 5)); // 从第6个元素开始查找子字符串位置
        // 8
        System.out.println(str.lastIndexOf("l")); // 从后向前开始查找指定字符串的位置
        // 8
    }
}

判断子字符串是否存在的第一种string方法

  • 利用indexOf()方法判断子字符串是否存在
public class StringDemo {
    public static void main(String args[]) {
        String str = "helloworld"; // 字符串对象
        if (str.indexOf("world") != -1) { // 能找到子字符串
          System.out.println("可以查询到数据");
        }
        // 可以查询到数据
    }
}

判断子字符串是否存在的第二种string方法

  • 使用contains()方法判断子字符串是否存在
public class StringDemo {
    public static void main(String args[]) {
        String str = "helloworld"; // 字符串对象
        if (str.contains("world")) { // 子字符串存在
            System.out.println("可以查询到数据。");
        }
        // 可以查询到数据
    }
}

开头或结尾判断

public class StringDemo {
    public static void main(String args[]) {
        String str = "##@@hello**"; // 字符串对象
        System.out.println(str.startsWith("##")); // 是否以“##”开头
        /// ture
        System.out.println(str.startsWith("@@", 2)); // 从第2个索引开始是否以“@@”开头
        // ture
        System.out.println(str.startsWith("**")); // 是否以“**”结尾
        // false
    }
}

观察替换的结果

public class StringDemo {
    public static void main(String args[]) {
        String str = "helloworld" ; // 定义字符串
        String resultA = str.replaceAll("l","_") ; // 全部替换
        String resultB = str.replaceFirst("l","_") ; // 替换首个
        System.out.println(resultA) ;
        // he__owor_d
        System.out.println(resultB) ;
        // he_loworld
    }
}

截取数据

public class StringDemo {
    public static void main(String args[]) {
        String str = "helloworld"; // 定义字符串
        String resultA = str.substring(5); // 从指定索引截取到结尾
        String resultB = str.substring(0, 5); // 截取部分子字符串
        System.out.println(resultA);
        // world
        System.out.println(resultB);
        // hello
    }
}

字符串拆分单词

public class StringDemo {
    public static void main(String args[]) {
        String str = "hello i like you"; // 定义字符串,中间使用空格作为间隔
        String result[] = str.split(" "); // 字符串拆分
        for (int x = 0; x < result.length; x++) { // 循环输出
            System.out.print(result[x] + "、");
        }
        // hello、i、like、you、
    }
}

字符串拆分字母

public class StringDemo {
    public static void main(String args[]) {
        String str = "hello i like you"; // 定义字符串
        String result[] = str.split(""); // 字符串全部拆分
        for (int x = 0; x < result.length; x++) { // 循环输出
            System.out.print(result[x] + "、");
        }
        // h、e、l、l、o、 、i、 、l、i、k、e、 、y、o、u、
    }
}

拆分为指定的个数

public class StringDemo {
    public static void main(String args[]) {
         String str = "hello i like you"; // 定义字符串,中间使用空格作为间隔
        String result[] = str.split(" ",2); // 字符串拆分
        for (int x = 0; x < result.length; x++) { // 循环输出
            System.out.println(result[x]);
        }
        /*
        hello
        i like you
        */
    }
}

拆分例子

public class StringDemo {
    public static void main(String args[]) {
        String str = "张三:20|李四:21|王五:22"; // 定义字符串
        String result[] = str.split("\\|"); // 第一次拆分
        for (int x = 0; x < result.length; x++) {
           String temp[] = result[x].split(":"); // 第二次拆分
           System.out.println("姓名:" + temp[0] + ",年龄:" + temp[1]);
        }
        /*
            姓名:张三,年龄:20
            姓名:李四,年龄:21
            姓名:王五,年龄:22
        */
    }
}

字符串连接

public class StringDemo {
    public static void main(String args[]) {
        String str = "hello".concat("world") ; // 等价于“+”
        System.out.println(str) ;
    }
}
/*
hello world
*/

转小写与转大写操作

public class StringDemo {
    public static void main(String args[]) {
        String str = "(*(*Hello(*(*" ; // 定义字符串
        System.out.println(str.toUpperCase()) ; // 转大写后输出
        System.out.println(str.toLowerCase()) ; // 转小写后输出
        /*
            (*(*HELLO(*(*
            (*(*hello(*(*
        */
    }
}

去掉左右空格

public class StringDemo {
    public static void main(String args[]) {
        String str = "   hello   world  "; // 定义字符串,包含空格
        System.out.println("【" + str + "】"); // 原始字符串
        System.out.println("【" + str.trim() + "】"); // 去掉空格后的字符串
        /*
            【   hello   world  】
            【hello   world】
        */
    }
}

取消掉全部空格

public class StringDemo {
    public static void main(String args[]) {
        String str = "   hello   world  "; // 定义字符串,包含空格
        System.out.println(str.replaceAll(" ","")); // 去掉空格后的字符串
        // helloworld
    }
}

取得字符串长度

public class StringDemo {
    public static void main(String args[]) {
        String str = "helloworld"; // 定义字符串
        System.out.println(str.length()); // 取得字符串长度
        // 10
        System.out.println(str.isEmpty()); // 判断字符串对象的内容是否为空字符串(不是null)
        // false
        System.out.println("".isEmpty()); // 判断字符串常量的内容是否为空字符串(不是null)
        // true
    }
}

实现首字母大写的操作

public class StringDemo {
    public static void main(String args[]) {
        String str = "wanghao"; // 定义字符串
        System.out.println(initcap(str)); // 调用initcap()方法
        // Wanghao
    }
    /**
     * 实现首字母大写的操作
     * @param temp 要转换的字符串数据
     * @return 将首字母大写后返回
     */
    public static String initcap(String temp) {
        // 利用substring(0,1)取出字符串的第一位后将其变为大写,再连接剩余的字符串
        return temp.substring(0, 1).toUpperCase() + temp.substring(1);
    }
}

文章作者: Optimist
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Optimist !
评论
 上一篇
懂点十八 懂点十八
马上就要进入大三了,在大三来临之际,想写点什么勉励自己加油努力。
2020-06-20 Optimist
下一篇 
Java 基础知识(1) Java 基础知识(1)
Java是一门面向对象的语言,学好Java受益终身。
2020-06-19 Optimist
  目录