java基础总结

http://c.biancheng.net/view/5663.html
java基础

1、字符串使用
使用 concat() 方法
列子:

 public static void main(String[] args) {
        String a = "凌青";
        a = a.concat("大");
        a = a.concat("渣男");
        System.out.println(a);
    }
    凌青大渣男

2、Java字符串大小写转换(toLowerCase()和toUpperCase())

列子:

String str="abcdef 我 ghijklmn";
System.out.println(str.toLowerCase());    // 输出:abcdef 我 ghijklmn
System.out.println(str.toUpperCase());    // 输出:ABCDEF 我 GHIJKLMN

3、Java分割字符串(spilt())

列子:

1)“.”和“|”都是转义字符,必须得加“\\”。
如果用“.”作为分隔的话,必须写成String.split("\\."),这样才能正确的分隔开,不能用String.split(".")。
如果用“|”作为分隔的话,必须写成String.split("\\|"),这样才能正确的分隔开,不能用String.split("|")。
 String Colors = "Red,Black,White,Yellow,Blue";
    String[] arr1 = Colors.split(","); // 不限制元素个数
    String[] arr2 = Colors.split(",", 3); // 限制元素个数为3
    System.out.println("所有颜色为:");
    for (int i = 0; i < arr1.length; i++) {
        System.out.println(arr1[i]);
    }
    System.out.println("前三个颜色为:");
    for (int j = 0; j < arr2.length; j++) {
        System.out.println(arr2[j]);
    }
}
所有颜色为:
Red
Black
White
Yellow Blue
前三个颜色为:
Red
Black
White,Yellow,Blue

2)如果在一个字符串中有多个分隔符,可以用“|”作为连字符,比如:“acount=? and uu =? or n=?”,把三个都分隔出来,可以用String.split(“and|or”)。

 public static void main(String[] args) {
        String Colors = "Red,Black,White/Yellow/Blue";
        String[] arr1 = Colors.split(",|/" ); // 不限制元素个数
        for (int i = 0; i < arr1.length; i++) {
            System.out.println(arr1[i]);
        }
    }
输出结果:
Red
Black
White
Yellow
Blue

4、Java字符串的替换(replace()、replaceFirst()和replaceAll())
1、replace() 方法用于将目标字符串中的指定字符(串)替换成新的字符(串)
2、replaceFirst() 方法用于将目标字符串中匹配某正则表达式的第一个子字符串替换成新的字符串
3、replaceAll() 方法用于将目标字符串中匹配某正则表达式的所有子字符串替换成新的字符串

 public static void main(String[] args) {
       String a = "凌青,hello";
       System.out.println(a.replace("凌青","玲青"));
    }
输出结果:玲青,hello

5、Java字符串查找(3种方法)
1、indexOf() 方法用于返回字符(串)在指定字符串中首次出现的索引位置,如果能找到,则返回索引值,否则返回 -1。
2、lastIndexOf() 方法用于返回字符(串)在指定字符串中最后一次出现的索引位置,如果能找到则返回索引值,否则返回 -1。
3、charAt() 方法可以在字符串内根据指定的索引查找字符

String s = "Hello Java";
int size = s.indexOf('v');    // size的结果为8
输出结果: size的结果为8

String words = "today,monday,sunday";
System.out.println(words.charAt(0));    // 结果:t
System.out.println(words.charAt(1));    // 结果:o
System.out.println(words.charAt(8));    // 结果:n

6、Java StringBuffer类详解
StringBuffer 类提供了 3 个构造方法来创建一个字符串,如下所示:
StringBuffer() 构造一个空的字符串缓冲区,并且初始化为 16 个字符的容量。
StringBuffer(int length) 创建一个空的字符串缓冲区,并且初始化为指定长度 length 的容量。
StringBuffer(String str) 创建一个字符串缓冲区,并将其内容初始化为指定的字符串内容 str,字符串缓冲区的初始容量为 16 加上字符串 str 的长度。
更多介绍.

// 定义一个空的字符串缓冲区,含有16个字符的容量
StringBuffer str1 = new StringBuffer();
// 定义一个含有10个字符容量的字符串缓冲区
StringBuffer str2 = new StringBuffer(10);
// 定义一个含有(16+4)的字符串缓冲区,"青春无悔"为4个字符
StringBuffer str3 = new StringBuffer("青春无悔");
/*
*输出字符串的容量大小
*capacity()方法返回字符串的容量大小
*/
System.out.println(str1.capacity());    // 输出 16
System.out.println(str2.capacity());    // 输出 10
System.out.println(str3.capacity());    // 输出 20

6.1、追加字符串
StringBuffer 类的 append() 方法用于向原有 StringBuffer 对象中追加字符串。

  StringBuffer buffer = new StringBuffer("hello,");    // 创建一个 StringBuffer 对象
        String str = "World!";
        buffer.append(str);
        System.out.println(buffer);
输出结果: hello,World!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值