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

整理Java基础知识--String类

时间:2018-01-10 23:50:09      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:other   单个字符   lower   单元   程序   51cto   rgs   连接字符串   先后   

Java String 类
创建字符串
最简单的写法:String i = "我是字符串";
String 类是不可改变的,一旦创建了String对象,就固定了

String i = "123";
System.out.println("i = " + i);
i = "abc";
System.out.println("i = " + i);
输出结果:
123
abc

等价于

String i = new String("123");
System.out.println("i = " + i);
String i = new String("abc");
System.out.println("i = " + i);
i & 123 & abc 需要分配3个单位的内存单元 程序自上而下执行,只是先后把123 & abc 赋值给i 并输出

技术分享图片
从输出结果上来看i 是改变了 实际上123 abc 还在内存中并未改变。而i只是引用对象 本身也未改变
连接字符串
1.concat方法联接:string1.concat(string2)
2.使用 + 联接

class TestA{
    public static void main(String[] args){

        String string1 = "123";
        String string2 = "abc";
        String string3 = "llll";
        String string4;
        String string5;
        string4 = string1.concat(string2).concat(string3);
        string5 = "123" + "abc" + "llll";
        System.out.println("string4 = " + string4);
        System.out.println("string5 = " + string5);
    }
}
输出结果:
string4 = 123abcllll
string5 = 123abcllll

String类常用方法
1.字符串的长度
public int length()

String str = new String("adzzbzz");
int len = str.length();//len = 7

2.字符串索引处的字符
public char charAt(int index)

String str = new String("adzzbzz");
char ch = str.charAt(5);//ch = b

3.字符串比较
int compareTo(Object o)

int compareTo(String anotherString)

String str1 = new String("abc");
String str2 = new String("ABC");
int a = str1.compareTo(str2);//a>0
int b = str1.compareTo(str2)//b=0

4.字符串连接
public String concat(String s)

String str = "111".concat("222").concat("333");
String str = "111"+"222"+"333";

5.提取子字符串
public String substring(int beginIndex)
public String substring(int beginIndex, int endIndex)

String str1 = new String("adzzbzz");
String str2 = str1.substring(2);//str2 = "zzbzz"
String str3 = str1.substring(2,5);//str3 = "zzb"

6.字符串中单个字符查找
public int indexOf(int ch/String str)
public int indexOf(int ch/String str, int fromIndex)
public int lastIndexOf(int ch/String str)
public int lastIndexOf(int ch/String str, int fromIndex)

String str = "I am a good student";
int i1 = str.indexOf(‘a‘);//i1= 2
int i2 = str.indexOf("good");//i2 = 7
int i3 = str.indexOf("w",2);//i3= -1
int i4 = str.lastIndexOf("a");//i4= 5
int i5 = str.lastIndexOf("a",3);//i5= 2

7.字符串中字符的大小写转换
public String toLowerCase()
public String toUpperCase()

String str = new String("abCD");
String str1 = str.toLowerCase();//str1 = "abcd"
String str2 = str.toUpperCase();//str2 = "ABCD"

8.字符串中字符的替换
public String replace(char oldChar, char newChar)
public String replaceFirst(String regex, String replacement)
public String replaceAll(String regex, String replacement)

String str = "adzzzad";
String str1 = str.replace(‘a‘,‘b‘);//str1 = "bdzzzbd"
String str2 = str.replace("ad","bp");//str2 ="bpzzzbp"
String str3 = str.replaceFirst("ad","bp");//str3 = bpzzzad"
String str4 = str.replaceAll("ad","bp");//str4 = "bpzzzbp"

整理Java基础知识--String类

标签:other   单个字符   lower   单元   程序   51cto   rgs   连接字符串   先后   

原文地址:http://blog.51cto.com/12431776/2059641

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