首页 > 编程语言 > 详细

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

时间:2016-01-13 01:58:00      阅读:110      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!