您的位置:首页 > 编程语言 > Java开发

Java基础学习笔记(二)常用类String

2010-12-26 11:15 573 查看
java.lang.String类代表不可变的字符序列

初始化方法(具体初始化方法api文档里有详细的)

String s1 = "hello"

String s2 = "hello"

s1 == s2;//true

s1 = new String("hello");

s2 = new String("hello");

s1 == s2;//false

s1.equals(s2);//true

char c[] = {'a','s','e','w','e','t','f','r'};

String s3 = new String(c); //s3 = "asewetfr"

String s4 = new String(c,4,4);s4 = "etfr"

常用方法介绍

public char charAt(int indes)

返回字符串中第index个字符

public int length()

返回字符串长度

public int indexOf(String str)

返回字符串中出现str的第一个位置

public int indexOf(String str,int fromIndex)

返回字符串中从fromIndex开始出现str的第一个位置

public boolean equalsIgnoreCase(String another)

比较字符串与another是否一样(忽略大小写)

public String replace(char oldChar,char newChar)

在字符串中用newChar字符替换oldChar字符

public String substring(int beginIndex)

返回该字符串从beginIndex开始到结尾的子字符串

public String substring(int beginIndex,int endIndex)

返回该字符串从beginIndex开始到endIndex结尾的子字符串

public String trim()

返回将该字符串去掉开头和结尾空格后的字符串

public String toLowerCase()

返回一个字符串为该字符串的小写形式

public String toUpperCase()

返回一个字符串为该字符串的大写形式
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: