码迷,mamicode.com
首页 > 编程语言 > 详细

Java学习----字符串函数

时间:2016-01-13 01:59:11      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

1.String

string类是最终类,不可以有子类

package com.demo.pkg1;

public class TestString {
    public static void main(String[] agrs) {
        // 构造字符串
        String name = "  hello world  ";
        String s1 = new String("aaa");
        
        // 长度
        int x = name.length();
        // 字符位置
        char c = name.charAt(3);
        // 子串
        String s = name.substring(5,8);
        // 去掉空格
        String ss = name.trim();
        
        System.out.println(x);
        System.out.println(c);
        System.out.println(s);
        System.out.println(ss);
    }
}

 15 e lo hello world 

 

2.StringBuffer(可变)

public class TestString {
    public static void main(String[] agrs) {
        StringBuffer s = new StringBuffer("hello");
        s.append(" world"); // string类型没有append
        System.out.println(s);
        System.out.println(s.toString()); // 转成string
    }
}
hello world

 

3.StringBuilder

StringBuilder线程不安全,StringBuffer线程安全。单线程使用StringBuilder,可以更快一点。

Java学习----字符串函数

标签:

原文地址:http://www.cnblogs.com/dragon1013/p/5126076.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!