JAVA015--常用类

String

import java.util.Scanner;

public class TestString {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    //字符串有三种类型--String、StringBuffer、StringBuilder
    //String类型
    //特点一:String常量也是对象,在加载期就被产生,放到数据段的字符串常量池当中

// String str = “hello”;
// String str1 = “hello”;
// String str2 = new String(“hello”);
// String str3 = new String(“hello”);
// System.out.println(str == str1);
// System.out.println(str2 == str3);
// System.out.println(str.equals(str2));

    //注意String的空与空串的区别

// String s1 = null;//空,没有String对象
// String s2 = “”;//空串,有String对象,对象里面没有字符

    //特点二:String对象一旦产生,内容不可更改;每次改变都是产生了一个新的对象
    //这个特点导致了String的效率不高(做字符串拼接的时候,每次拼接都要产生新的String对象)

// String str = new String(“hello”);
// String str1 = str;
// System.out.println(str == str1);
// str1 += “!”;
// System.out.println(str == str1);

    //String的常用方法
    //1、与字符数组相关的方法

// String str = “hello”;
//转换为char[]
// char[] charArray = str.toCharArray();
//访问某个位置的字符是多少?
// char c = str.charAt(4);
//得到某个字符在字符串当中第一次出现的位置;如果该字符不存在,返回-1
// int index = str.indexOf(‘a’);
//得到某个字符在字符串当中最后一次出现的位置;
// index = str.lastIndexOf(‘l’);
//得到字符串长度
// int length = str.length();

    //2、跟字母有关的方法

// String str = “woRlD”;
// String upperStr = str.toUpperCase();//转换为全大写
// String lowerStr = str.toLowerCase();//转换为全小写
//忽略大小写判断相等
// System.out.println(upperStr.equalsIgnoreCase(lowerStr));
//字符串比较大小(按字典顺序:先比较第一个不同的字符,如果所有字符都相同,再比较长度)
// System.out.println(“hellow”.compareTo(“Hellow”));
//字符串忽略大小写比较大小
// System.out.println(“hellow”.compareToIgnoreCase(“Hellow”));

    //3、跟字符串内容相关的
    //一个字符串是否包含在另一个字符串内

// System.out.println(“hello”.contains(“hl”));
//一个字符串是否以另一个字符串开头
// System.out.println(“hello”.startsWith(“he”));
//一个字符串是否以另一个字符串结尾
// System.out.println(“hello”.endsWith(“lo”));
//从一个字符串当中截取子串
// String str = “helloworld”.substring(4, 7);//前开后闭的区间
// str = “ILOVEYOU”.substring(3);
// System.out.println(str);
//在一个字符串中做内容替换—本方法支持正则表达式
// str = “ILOVEYOU”.replace(‘O’, ‘F’);
// str = “ILOVEYOU”.replace(“LOV[EO]”, “LIKE”);
// System.out.println(str);

    //4、字符串特殊方法
    //trim()---主要用途是去掉字符串前后的空格

// System.out.println(“请输入您的用户名:”);
// String inputName = new Scanner(System.in).next();
// if(inputName != null && !inputName.trim().equals(“”)){
// System.out.println(“执行业务操作”);
// }else{
// System.out.println(“输入不能为空!”);
// }

    //split()---字符串拆分(分隔符如果出现在最后面,不会分隔出空串;但是如果出现在最前面,会分割出空串)
    //本方法支持正则表达式

// String url = “username=zhang3&password=1234563&account=5000”;
// String[] values = url.split(“&”);
// System.out.println(values.length);
// for(String value : values){
// String[] subValue = value.split(“=”);
// System.out.println(subValue[1]);
// }
// String[] values = url.split(“[=&]”);
// for(int i = 1; i < values.length; i+=2){
// System.out.println(values[i]);
// }

    //正则表达式

// System.out.println(“请输入电话号码:”);
// String num = new Scanner(System.in).next();
// String regex = “8320([0-9]{4}|1[12]0|119)”;
// if(num.matches(regex)){
// System.out.println(“这是电子科技大学的电话号码”);
// }else{
// System.out.println(“输入有误…..”);
// }

}

StringBuffer类

//特点一:StringBuffer对象内容可以更改(如果在开发中,需要做大量的字符串拼接动作,选用它)
//特点二:线程安全、但效率低(多线程程序使用)
// StringBuffer sb = new StringBuffer(“hello”);
// StringBuffer newSb = sb;
// sb.append(“world”);//往末尾添加
// sb.insert(5, ” “);//往中间添加
// System.out.println(newSb);

StringBuilder类

//特点一:StringBuilder对象内容可以更改
//特点二:线程不安全,但效率高(单线程程序中使用)
StringBuilder sb = new StringBuilder(“hello”);
StringBuilder newSb = sb;
sb.append(“world”);//往末尾添加
sb.insert(5, ” “);//往中间添加
System.out.println(newSb);

三个字符串类的区别

    //结论:String是使用最多的字符串类型,方法丰富,语法简单!
    //如果出现大量的字符串拼接动作,请使用StringBuilder或StringBuffer替换
    //单线程应用以及没有线程安全性问题的多线程应用,使用StringBuilder
    //会出现线程安全性问题(多个线程访问同一个字符串对象),使用StringBuffer

properties类

定义:一个可以操作文件的具有KV对的集合类
//产生一个容器对象
Properties props = new Properties();
//使用setProperty()增加元素,要求Key不能重复
props.setProperty(“J121”, “周春艳”);
props.setProperty(“J122”, “何如伟”);
props.setProperty(“J123”, “刘弯弯”);
props.setProperty(“J124”, “何如伟”);
//如果key重复了,setProperty()相当于修改
props.setProperty(“J121”, “李言妮”);
//使用getProperty(key)取元素,如果key不存在得到null
System.out.println(props.getProperty(“J125”));

    //keySet()得到所有的key
    Set allKey = props.keySet();
    for(Object key : allKey){
        System.out.println(key);
    }

    System.out.println(props.size());//元素的个数


    //属性文件操作
    //存文件
    try {
        props.store(new FileOutputStream("data.properties"), null);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Properties readProps = new Properties();
    System.out.println(readProps.size());
    //读文件
    try {
        readProps.load(new FileInputStream("data.properties"));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println(readProps.getProperty("J124"));
    System.out.println(readProps.size());

结果:
null
J124
J123
J122
J121
4
0
何如伟
4

包装类

基本数据类型对应的8个包装类
int与Integer与String的转换
其他的都类似
//int —> Integer
// int num = 120;
// Integer in = new Integer(num);
// Integer in2 = num;//自动封箱

    //Integer ---> int

// Integer in = new Integer(125);
// int num = in.intValue();
// int num2 = in;//自动拆箱
// System.out.println(num);

    //Integer ---> String

// Integer in = 230;
// String str = in.toString();
// String str2 = in + “”;
// System.out.println(str);

    //String ---> Integer

// String str = “275”;
// Integer in = new Integer(str);
// System.out.println(in);

    //String ---> int (重要)

// String str = “312”;
// int num = Integer.parseInt(str);
// System.out.println(num);

    //int ---> String

// int num = 666;
// String str = Integer.toString(num);
// String str2 = num +”“;
// System.out.println(str);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值