java中的String
Java String
1.声明字符串
声明字符串就好好比是去超市买衣服,进门的时候就拿一个菜篮子或者小推车,以此表明我要买衣服,然后就往菜篮子里转自己要买的东西。声明字符串的两种方式:
String hello=“helloworld”;
String hello=new String();
Sting hello=new String ("helloworld");
2.创建字符串
进入超市并已经推了小推车后,接下来就需要将购买的物品放置到小推车中,也是创建字符串。
String类字符串的创建方式有以下几种(简要介绍几种):
(1)String str=new String (); 初始化一个对象,表明这个对象是一个空字符
(2)String str=new String("hello"); 该对象表示参数所表示的对象
(3)String str=new String(char char[ ]); 初始化一个数组,该数组表示字符数组顺序组成的字符串
【创建二维字符串数组】
public class Main {
public static void main(String[] args) {
//如何创建字符串数组
String[] str=new String[4];
str[0]=new String("hello ,");
str[1]=new String("this ");
str[2]=new String("is ");
str[3]=new String("life ");
for(String s:str) {
System.out.print(s);
}
}
}
hello ,this is life
字符串指针值浅复制
public class MyFile {
public static void main(String[] args) {
String str1="hello";
String str2="hello";
String str3=new String("hello");
String str4=new String("hello");
System.out.println("测试浅复制地址问题");
if(str1==str2)
System.out.println("str1与的str2地址指向 是一样的");
else System.out.println("str1与str2的地址指向是不一样的");
if(str3==str4)
System.out.println("str3与str4的地址指向是一样的");
else System.out.println("str3与str4的地址指向是不一样的");
}
}
3.字符串的操作(便捷操作问题)
3.1字符串的比较
对两个字符串进行比较,比较其内容是否相同。
对于字符串的比较,主要有以下几种方法:
(1)equals( );比较两个字符串是否相等,返回boolean值类型。
(2)equalsIgnoreCase( ); 忽略大小写,返回boolean值类型。
(3)compareTo(string ); 按照字典的顺序进行比较,返回int 值类型。
public class t01 {
public static void main(String[] args){
String str1=new String("helloworld");
String str2=new String("HELLOWORLD");
String str3=new String("Helloworld");
System.out.println(str1+"和"+str2+"使用equals比较结果为:"+str1.equals(str2));
System.out.println(str1+"和"+str2+"使用equalIgnoreCase比较结果为:"+str1.equalsIgnoreCase(str2));
System.out.println(str1+"与"+str3+"使用compareTo比较"+str1.compareTo(str3));
}
}
helloworld和HELLOWORLD使用equals比较结果为:false
helloworld和HELLOWORLD使用equalIgnoreCase比较结果为:true
helloworld与Helloworld使用compareTo比较32
注意:在比较字符串时不能用“==”,因为用==比较时,实际上判断的是否是同一个对象,如果内容相同,但不是同一个对象,返回值仍然是false,因为其对应的不是同一个内存地址。
3.2连接字符串
当一个物体从中间折断或者合并为同一个时,我们会想办法把两者粘起来,将字符串连接起来的方法可以是concat(),该方法的参数为String类型的对象,返回值为String。
public class t01 {
public static void main(String[] args){
String str1="today is ";
String str2="very good";
String str3=str1.concat(str2);
System.out.println(str3);
}
}
today is very good
3.3复制字符串
String str1=str2; | 复制字符串(浅复制)--具体浅复制什么意思,就是两个字符串指向的地址是一样的 |
copyValueOf(char[ ],int offset,int count) | 部分复制offset---起始位置,count---个数 |
copyValueOf(char[]) | 全部复制 |
public class Main {
public static void main(String[] args) {
String str="helloworld";
char[] ch=new char[str.length()];
//转换格式
for(int i=0;i<str.length();i++) {
ch[i]=str.charAt(i);
}
//全部复制
String str1=String.copyValueOf(ch);
//部分复制,复制起始点,个数
String str2=String.copyValueOf(ch,5, 5);
System.out.print("全部复制的结果为:"+str1+"\n部分复制的结果为:"+str2);
}
}
运行结果为:
全部复制的结果为:helloworld
部分复制的结果为:world
代码解析:在程序中,首先创建一个字符串,内容为“helloworld”字符串名为str,然后创建一个char类型数组ch,并设置字符串的长度为,并将字符串的长度str转化为char数组并赋值给ch,使用copyValueOf的方法复制ch中的内容,进行全部复制和部分复制,最后输出结果。
3.4更改字符串中的内容
若发现写到纸上的字错了,如果使用的是铅笔可以用橡皮擦掉,对于字符串,也提供了修改的方法。
replace(char,char);该方法可以把字符串中与第一个参数形同的字符替换为第二个字符。
replaceAll(String,String);该方法可以把字符串中与第一个参数相同的字符串替换为第二个字符串。
replaceFirst(String,String);该方法可以将字符串中与第一个参数字符串相同的替换为方法中第二个字符串。
public class MyFile {
public static void main(String[] args) {
String str1="this is a question";
String str2=str1.replace('s', 'S');
System.out.println(str2);//替换字符char
String str3=str1.replaceAll("this", "it");
System.out.println(str3);//替换字符串str
//替换首字符串
System.out.println(str1.replaceFirst("s", "S"));
}
}
3.5获取字符串的长度
这个很简单,在这里简要一提,使用.length()方法,用于获取字符串长度。
public class Main {
public static void main(String[] args) {
String str="hello world i am java";
System.out.print(str.length());
}
}
3.6分割字符串
分割字符串使用的方法是split(),首先按照指定的划界表达式将字符串分割成几部分,每一部分是个字符串,方法的返回值是一个字符串数组
public static void main(String[] args) {
String str="hello world i am java";
String[] words=str.split(" ");
for(String s:words) {
System.out.print(s+",");
}
}
}
3.7tirm()去掉字符串首尾的空格符
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String str=in.nextLine();//接收一行字符串enter键结束
String message=str.trim();
System.out.println(message);
}
}
差点以为能够去掉中间的空格呢,总之在应用中也是比较方便
3.8其他类转化为字符串
使用String.valueOf( object进行转化),这种转化避免了object.toString()中的不足和(String)对象--强制转化的限制以及程序报错的问题,是一种比较好用的方法。
public static void main(String[] args) {
int a=1234;
String str=String.valueOf(a);
System.out.println(str);
}