Java常用类之String

字符串相关类(String、StringBuffer)

String类

Java.lang.String代表不可变的字符序列
平常使用的“XXXXX”为该类的一个对象

String类常见的构造方法
1、String(String original)
创建一个String对象为original的拷贝
2、String(char[] value)
用一个字符数组创建一个String对象
3、String(char[] value,int offset,int count)
用一个字符数组从offset项开始的count个字符序列创建一个 String对象(下例中有体现)

public class TestString{
    public static void main(String[] args){
        String s1 = "123";
        String s2 = "123";
        //在data seg内存区"123"只存在一个,指向相同
        System.out.println(s1 == s2);

        String s3 = new String("Hello");
        String s4 = new String("Hello");
        System.out.println(s3 == s4);
        //String 类重写了equals方法,比较字符串序列是否相等
        System.out.println(s3.equals(s4));

        char[] c = {'s','u','n',' ','j','a','v','a'};
        String s5 = new String(c);
        String s6 = new String(c,4,4);
        System.out.println(s5);
        System.out.println(s6);
    }
}

运行结果:

true
false
true
sun java
java

String类常用方法
1、public char charAt(int index)返回字符串中第index个字符
2、public int length()返回字符串的长度
3、public int indexOf(String str)返回字符串中出现str的第一个位置
4、public int indexOf(String str,int fromIndex)返回字符串中从fromIndex开始出现str的第一个位置
5、public boolean equalsIgnoreCase(String another)比较字符串与another是否一样(忽略大小写)
6、public String replace(char oldChar,char newChar)在字符串中用newChar字符替换oldChar字符
7、public boolean startsWith(String prefix)判断字符串是否以prefix开头
8、public boolean endsWith(String suffix)判断字符串是否以suffix结尾
9、public String toUpperCase()返回一个字符串为该字符串的大写形式
10、public String toLowerCase()返回一个字符串为该字符串的小写形式
11、public String substring(int beginIndex)返回该字符串从beginIndex开始到结尾的子字符串
12、public String substring(int beginIndex,int endIndex)返回该字符串从beginIndex开始到endIndex结尾的字符串
13、public String trim()返回将该字符串去掉开头和结尾空格后的字符串

下面例子中各方法都有体现

public class TestString1 {

    public static void main(String[] args) {
        String s1 = "sun java",s2 = "Sun Java";
        System.out.println(s1.charAt(1));
        System.out.println(s2.length());
        System.out.println(s1.indexOf("java"));
        System.out.println(s1.indexOf("Java"));
        System.out.println(s1.equals(s2));
        System.out.println(s1.equalsIgnoreCase(s2));

        String s = "我是程序员,我在学Java";
        String sr = s.replace('我', '你');
        System.out.println(sr);

        String s3 = "Welcome to Java world!";

        System.out.println(s3.startsWith("Welcome"));
        System.out.println(s3.endsWith("world"));

        String sl = s3.toLowerCase();
        String su = s3.toUpperCase();
        System.out.println(sl);
        System.out.println(su);

        String subs = s3.substring(11);
        System.out.println(subs);

        String s4 = "   sun java   ";
        String sp = s4.trim();
        System.out.println(sp);
    }

}

输出结果

u
8
4
-1
false
true
你是程序员,你在学Java
true
false
welcome to java world!
WELCOME TO JAVA WORLD!
Java world!
sun java

另外还有一些String类的常用方法

静态重载方法
1、public static String valueOf(…)将基本数据类型、引用类型转换为String类型
(形参也可以是对象即(Objiect object),相当于调用对象的(重写的)toString()方法)有多态存在
2、public String[] split(String regex)可以将一个字符串按照指定的分隔符分割,返回分割后的字符串数组

public class TestString2 {
    public static void main(String[] args){
        int j = 1234567;
        String s = String.valueOf(j);
        System.out.println("j是"+s.length()+"位数");

        String s2 = "Kangkang,male,1997 ";
        String[] sp = s2.split(",");
        for(int i = 0;i<sp.length;i++){
            System.out.println(sp[i]);
        }
    }
}

运行结果

j7位数
Kangkang
male
1997 


输出一个字符串的大写英文字母数,小写英文字母数,非英文字母数

public class TestString3 {

    public static void main(String[] args) {
        String s = "asjhdaBJHbjbjbJjhb=-=8JHByHjbHygUBJhuyU-%$";
        int lc= 0 ,uc = 0,oc = 0;

        for(int i = 0;i<s.length();i++){
            char c = s.charAt(i);
            if(c > 'a' && c<'z'){
                lc++;
            }else if(c > 'A' && c < 'Z'){
                uc++;
            }else{
                oc++;
            }

        }
        System.out.println(lc+" "+uc+" "+oc);

    }
}

运行结果20 13 9
寻找一字符串中特定字符串出现的次数

public class TestString4 {
    public static void main(String[] args){
        String s = "sunjavahahahajavajavaaajava";

        String toFind = "java";
        int count = 0;
        int index = -1;

        while((index = s.indexOf(toFind)) != -1){
            s = s.substring(index + toFind.length());
            count++;
        }
        System.out.println(count);
    }
}

运行结果4

StringBuffer类

Java.lang.StringBuffer代表可变的字符序列

StringBuffer类的常见构造方法
StringBuffer() 创建一个不包含字符序列的空的StringBuffer对象
StringBuffer(String str) 创建一个StringBuffer对象,包含与String对象str相同的字符序列

StringBuffer常用方法
1、重载方法public StringBuffer append(…)可以为该StringBuffer对象添加字符序列,返回添加后的该StringBuffer对象引用
例如:public StringBuffer append(String str)
public StringBuffer append(char[] str)
public StringBuffer append(Object obj)
2、public StringBuffer insert(…)可以为该StringBuffer对象在指定位置插入字符序列,返回修改后的该StringBuffer对象引用
3、public StringBuffer delete(int start,int end) 可以删除从start开始到end-1为止的一段字符序列,返回修改后的该StringBuffer的对象引用
4、public StringBuffer reverse() 用于将字符序列逆序,返回修改后的该StringBuffer的对象引用
另外还有一些与String类含义类似的方法 indentOf()、subString()、length() 等

public class TestStringBuffer {

    public static void main(String[] args) {
        String s = "haha";
        char[] a ={'a','b','c'};

        StringBuffer sb = new StringBuffer(s);
        sb.append('/').append("heiehei").append('/').append("houhou");
        System.out.println(sb);

        StringBuffer sb2 = new StringBuffer("数字");
        for(int i = 0;i<5;i++){
            sb2.append(i);
        }
        System.out.println(sb2);
        sb2.delete(6, sb2.length()).insert(0, a);
        System.out.println(sb2);
        System.out.println(sb2.reverse());
    }

}

输出结果

haha/heiehei/houhou
数字01234
abc数字0123
3210字数cba
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值