String
- 创建对象:String name=“lucy” (String是一个类,不需要new一个对象,直接这样就可以创建与一个对象了)
String str=new String(); //空字符串 不是null,它是一个字符串,null是什么都没有
String str1=new String(hello);
// byte[]bs={}; //->编码 Unicode GBK UTF-8
// String str2=new String(bs);
常量池
- JVM中有一块中间String 常量池
如:
String s1=“hello”;
String s2=“hello”;
String s3=new String(hello);
String s4=“he”+“llo”;
String s5=“he”;
String s6=s5+“llo”;
//s1==s2 JDK中为了优化String,直接赋值的都在常量值中创建一个hello 对象
s2先在常量池中寻找是否有hello,有直接指向hello,无则在常量池中创建一个hello
//s1!=s3 s3中new出来的对象和s1,s2的对象不一样
//s1==s4 字符串直接量的拼接,也是在常量池中操作
//s1!=s6
- 但是字符串的比较不能用“==”比较,——>地址
应该用equals方法比较 如 s1.equals(s3) 是true
-
字符串是不可变的字符序列
如s6中,he,llo,hello三个对象,即字符串的拼接效率是很低的
-
学习字符串常用API Class String
-
学习方法: 对String:了解其常用的方法——>用代码写出来(给什么参数等看一下,参数是什么意思,····,用一下,运行一下,打印出来是什么,知道效果如何。)(知道方法的调用,参数的传递,结果的返回,能知道这个方法是什么就可以了)
String常用API
charAt
public char charAt(int index)
//方法charAt()用于返回字符串指定位置的字符。参数index表示指定的位置。该序列的第一个char值在索引0,下一个为1.
/**遍历一个字符串中的字符序列*/
public static void testCharAt(){
String name="Whatisjava?";
for(int i=0;i<name.length();i++){
char c=name.charAt(i);
System.out.print(c+" ");
}//W h a t i s j a v a ?
}
如果index参数为负数或不小于此字符串的长度时,会抛出异常indexOutOfBoundsException
indexOf/lastIndexOf
- indexOf方法用于实现在字符串中检索一个字符或者另一个字符串
- lastIndexOf方法用于实现在字符串中检索最后一次出现在字符串中的一个字符或者另一个字符串
- String提供几个重载的indexOf/lastIndexOf方法
int indexOf (int ch) | 返回指定字符第一次出现的字符串内的索引,此字符串无此类字符,返回-1 |
---|---|
int indexOf(int ch,int fromIndex) | 返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索 |
int indexOf(String str) | 返回指定子字符串第一次出现的字符串内的索引,无,返-1 |
int indexOf(String str,int fromIndex) | 返回指定字符串第一次出现的字符串内的索引,以指定的索引开始搜索 |
int lastIndexOf(int ch,int fromIndex) | 最后一次的字符 |
int lastIndexOf(String str,int fromIndex) | 最后一次的字符串 |
public static void testIndexOf()
{
String str="12345678909876543210";
int index=str.indexOf('2');//字符
System.out.println(index);
}//1(从0开始)
public static void testIndexOf()
{
String str="12345678909876543210";
int index=str.indexOf('2',6);
System.out.println(index);
}//17(从第6个开始索引,寻找下一个该指定字符)
public static void testIndexOf()
{
String str="12345678909876543210";
int index=str.indexOf("23");
System.out.println(index);
}//1
public static void testIndexOf()
{
String str="12345678909876543210";
int index=str.indexOf("23",6);
System.out.println(index);
}//-1
public static void testLastIndexOf()
{
String str="12345678909876543210";
int lastIndex=str.lastIndexOf('2');
System.out.println(lastIndex);
}//17
public static void testLastIndexOf()
{
String str="12345678909876543210";
int lastIndex=str.lastIndexOf("23",6);
System.out.println(lastIndex);
}//1
substring
-
返回一个子字符串,beginIndex开始,endIndex-1结束索引,
-
substring常用的重载方法定义如下:
public String substring(int beginIndex,int endIndex) | 返回字符串中从下标beginIndex(包括)开始到endIndex(不包括)结束的子字符串 |
---|---|
public String substring(int beginIndex) | 返回字符串中从下标beginIndex(包括)开始到字符串结尾的子字符串 |
- 如果beginIndex为负数,或endIndex大于该String对象的长度,或beginIndex大于endIndex时会抛出异常IndexOutOfBoundsException
public static void testSubstring(){
String str="http://www.oracle.com";
String subStr=str.substring(11,17);
System.out.println(subStr);//oracle
subStr=str.substring(7);
System.out.println(subStr);//www.oracle.com
}
String str="hamburger".substring(4,8);
System.out.println(str);//urge
trim
- trim方法用于返回一个字符串,其值为此字符串,并删除任何前导和尾随空格
public static void testTrim(){
String userName=" good girl ";
userName=userName.trim();
System.out.println(userName.length());//9
System.out.println(userName);//good girl
}
startsWith/endsWith
- startsWith方法用于测试此字符串是否以指定的前缀开头,是返回true,否false
- endsWith方法用于测试此字符串是否以指定的后缀结尾,是返回true,否false
public static void testStartWithAndEndWith(){
String str="I am a student";
System.out.println(str.endsWith("student"));//true
System.out.println(str.startsWith("I"));//true
System.out.println(str.startsWith("am"));//false
}
toUpperCase/toLowerCase
-
用于使用默认语言环境的规则将在此字符String中的所有字符转换为大写/小写
-
也有重写的方法toUpperCase(Locale.Root), toLowerCase(Locale.Root)可以将所有在此字符String使用给定的规则,大写Locale
public static void testToUpperCaseAndToLowerCase(){
String str ="我喜欢java";
str=str.toUpperCase();
System.out.println(str);//我喜欢JAVA
str=str.toLowerCase();
System.out.println(str);//我喜欢Java
}
valueOf
- valueOf方法用于将其他类型转换为字符串类型
public static void testValueOf(){
double pi=3.1415926;
int value=123;
boolean flag=true;
char[]charArr={'a','b','c','d','e','f','g'};
String str=String.valueOf(pi);
System.out.println(str);//3.1415926
str=String.valueOf(value);
System.out.println(str);//123
str=String.valueOf(flag);
System.out.println(str);//ture
str=String.valueOf(charArr);
System.out.println(str);//abcdefg
}