String大全

来源:互联网 发布:javascript 爬虫 编辑:程序博客网 时间:2024/04/29 04:27

JavaString类和常用方法

实例化String对象

第一直接赋值

package com.qn.array;

public class Test4 {
 public static void main(String[] args) {
 String name="zhangsan";
 System.out.println("姓名:"+name);

 }
}

结果

姓名:zhangsan

第二

String str=new String();

package com.qn.array;

public class Test4 {
 public static void main(String[] args) {
 String name=new String("zhangsan");
 System.out.println("姓名:"+name);
 }
}

结果

结果

姓名:zhangsan

String的内容比较

因为基本类型是使用“==”进行数据比较
下面首先使用“==”进行比较

package com.qn.array;

public class Test4 {
 public static void main(String[] args) {
 int x,y;
 x=32;
 y=32;
 System.out.println("基本数据类型进行比较:"+(x==y));
 String str1="zhangsan";
 String str2=new String("zhangsan");
 String str3=str2;
 System.out.println("str1==str2-->"+(str1==str2));
 System.out.println("str2==str3-->"+(str2==str3));
 }
}

结果

基本数据类型进行比较:true
str1==str2-->false
str2==str3-->true


这里使用的判断相等,是判断的地址空间是否相等,判断的是地址值

如果要判断内容是否相等,则必须使用String类中提供的equals()方法完成

package com.qn.array;

public class Test4 {
 public static void main(String[] args) {
 String str1="zhangsan";
 String str2=new String("zhangsan");
 String str3=str2;
 System.out.println("str1==str2-->"+str1.equals(str2));
 System.out.println("str1==str3-->"+str1.equals(str2));
 System.out.println("str2==str3-->"+str2.equals(str3));
 }
}

结果

str1==str2-->true
str1==str3-->true
str2==str3-->true

结论
一种使用“==”比较的是地址值
另一种使用“equals”方法完成:比较的是具体的内容

两种实例化方式的区别

String中可以直接赋值,和new进行实例化,哪一种更合适?

一个字符串就是String类的匿名对象

比如 "hello".equals("hello")

Stmbvnnring str1="zhangsan";

就表示将一个堆内存空间的指向给了栈内存

事例

package com.qn.array;

public class Test4 {
 public static void main(String[] args) {
 String str1="zhangsan";
 String str2="zhangsan";
 String str3="zhangsan";
 System.out.println("str1==str2-->"+(str1==str2));
 System.out.println("str1==str3-->"+(str1==str2));
 System.out.println("str2==str3-->"+(str2==str3));

 }
}

结果

str1==str2-->true
str1==str3-->true
str2==str3-->true

这个时候他们的地址是一样的


使用直接赋值的方式可以节约内存

使用new的方式

public class Test4 {
 public static void main(String[] args) {
 String str1=new String("zhangsan");

 }
}


使用new的方式开辟了两次空间,因为String str=new String("hello");

这个("hello")本身就是一个匿名对象,此时已经开辟了堆内存空间,而此时再次尽心new的时候new String("hello");又一次开辟了内存空间,所以一共开辟了两次内存空间

使用直接赋值的方式只需要一个实例化对象即可,而使用new String()的方式则意味着开辟两个内存对象,最好使用直接赋值的方式完成

下面:字符串的内容不可改变

package com.qn.array;

public class Test4 {
 public static void main(String[] args) {
 String str="hello";
 str=str+",world";
 System.out.println(str);
 }
}

结果

hello,world
此时字符串对象是改变了,但是字符串并没有改变


实际上字符串的改变实际上改变的是内存地址的引用关系

在开发中要避免下面的操作

package com.qn.array;

public class Test4 {
 public static void main(String[] args) {
 String str="hello";
 for (int i = 0; i <10; i++) {//循环改变内容
 str=str+i;//字符串的引用不断改变
 }
 }
}

下面String类的常用方法



将字符串变为字符数组

char[]()
 将此字符串转换为一个新的字符数组。

package com.qn.array;

public class Test4 {
 public static void main(String[] args) {
 String str="hello";
 char c[]= str.toCharArray();
 for (int i = 0; i < c.length; i++) {
 System.out.print(c[i]+"");
 }
 }
}

结果

hello

将字符数组变为字符串

(char[] value)
 分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。

package com.qn.array;

public class Test4 {
 public static void main(String[] args) {
 String str="hello";
 char c[]= str.toCharArray();
 for (int i = 0; i < c.length; i++) {
 System.out.print(c[i]+"");
 }
 String str1=new String(c);
 System.out.println("\n"+str1);
 }
}

结果

hello
hello

取出指定位置的字符

char(int index)
 返回指定索引处的 char值。

package com.qn.array;

public class Test4 {
 public static void main(String[] args) {
 String str="hello";
 char c= str.charAt(3);
 System.out.println(c);
 }
}

结果

l

字符串与byte数组的转换

byte数组(字节数组),在一般的io操作中经常用到
String类中提供了下面的方法

字符串变为字节数组

byte[]()
 使用平台的默认字符集将此 String编码为 byte 序列,并将结果存储到一个新的 byte 数组中。

将一个字节数组变为字符串

(byte[] bytes)
 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String

将部分字节数组变为字符串

(byte[] bytes, int offset, int length)
 通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的 String

package com.qn.array;

public class Test4 {
 public static void main(String[] args) {
 String str="hello";
 byte [] bytes=str.getBytes();
 System.out.println(new String(bytes));
 System.out.println(new String(bytes,1,3));
 }
}

结果

hello
ell

取得字符串的长度

package com.qn.array;

public class Test4 {
 public static void main(String[] args) {
 String str="hellofdsafdsafdsadfs";
 System.out.println("长度:"+str.length());

 }
}

结果

长度:20
注意:在操作数组的时候可以使用“数组名字.length”,那么这里习惯性的在字符串操作的地方也使用“字符串.length”的格式,后面没有括号,str是一个对象这里的length是一个方法。

查找指定的字符串是否存在

int(int ch)
 返回指定字符在此字符串中第一次出现处的索引。

int(int ch, int fromIndex)
 返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。

查找的时候方法的返回值是 一个int类型的数据,此数据表示的是一个字符串的位置,如果没有则返回-1

package com.qn.array;

public class Test4 {
 public static void main(String[] args) {
 String str="abcdefghijklmnccc";
 System.out.println("c:"+str.indexOf("c"));
 System.out.println("c:"+str.indexOf("c",4));
 System.out.println("c:"+str.indexOf("c",15));
 }
}

结果

c:2
c:14
c:15

取得空格

 trim ()
 返回字符串的副本,忽略前导空白和尾部空白。


如果假设一些信息是由用户输入的话,则就可能出现多余的空格,在这种操作中可以使用trim()取得字符串的左右空格,但是字符串中间的空格是不能去掉的

package com.qn.array;

public class Test4 {
 public static void main(String[] args) {
 String str=" hello aa ";
 System.out.println("没有取得的时候:"+str);
 System.out.println("取得左右空格后:"+str.trim());
 }
}

结果

没有取得的时候: hello aa
取得左右空格后:hello aa

字符串截取

取出字符串中的部分内容

 (int beginIndex)
 返回一个新的字符串,它是此字符串的一个子字符串。从截取处到尾

 (int beginIndex, int endIndex)
 返回一个新字符串,它是此字符串的一个子字符串。从指定的范围中截取

package com.qn.array;

public class Test4 {
 public static void main(String[] args) {
 String str="hello world";
 System.out.println(str.substring(5));//6到最后
 System.out.println(str.substring(5, 9));
 }
}

结果

world
 wor

拆分字符串

[]( regex)
 根据给定的匹配拆分此字符串。

[]( regex, int limit)
 根据匹配给定的来拆分此字符串。

需要正则支持的

package com.qn.array;

public class Test4 {
 public static void main(String[] args) {
 String str="hello world";
 String s[]=str.split(" ");//按空格进行拆分
 for (int i = 0; i < s.length; i++) {
 System.out.print(s[i]+"");
 }
 }
}

结果

helloworld

大小写转换

小写变成大写

package com.qn.array;

public class Test4 {
 public static void main(String[] args) {
 String str="hello";
 String str1="ABC";
 System.out.println(str.toUpperCase());
 System.out.println(str1.toLowerCase());
 }
}

结果

HELLO
abc

不区分大小写 equalsIgnoreCase();

package com.qn.array;

public class Test4 {
 public static void main(String[] args) {
 String str="hello";
 String str1="HELLO";
 System.out.println(str.equalsIgnoreCase(str1));
 System.out.println(str1.equals(str));
 }
}

结果

true
false
字符串替换功能

package com.qn.array;

public class Test4 {
 public static void main(String[] args) {
 String str="hello";
 String newStr=str.replaceAll("l", "x");
 System.out.println("替换之后的内容:"+newStr);
 }
}

结果

替换之后的内容:hexxo

 

0 0
原创粉丝点击