【Java学习笔记】String相关语句

来源:互联网 发布:php 点击链接下载文件 编辑:程序博客网 时间:2024/06/05 17:54

.String概述

1.String 类代表字符串,java程序中的所有字符串字面值(如“a,b,c”)都可作为此类的实现。

2.字符串是常量,它们的值在创建之后不能更改(字符串一旦被赋值,其值不能再改变)

.常用构造方法

1.public String():空参构造方法

public String(byte[] bytes):将字节数组转换成字符串

public String(byte[] bytes,int index,int length):将部分字节数组转换成字符串

public String(char[] value):将字符数组转换成字符串

public String(char[] value,int offset,int count):将字符数组的一部分转换成字符串

public String(String original):将字符串常量转换成字符串

例题:

package practice;

 

public class P2 {

public static void main(String[] args) {

//空参构造方法

String s1=new String();

System.out.println("s1:"+s1);

System.out.println("s1.length():"+s1.length());

 

    //将字节数组转换为字符串

     byte [] bytes = {96,93,98};

     String s2 = new String (bytes);

     System.out.println("s2:"+s2);

    //将部分字节数组转换成字符串

     byte [] byt1 = {95,96,97,98};

     String s3= new String (byt1,1,3);

     System.out.println("s2:"+s3);

    //将字符数组转换成字符串

   char [] ch = {'a','b','c'};

   String s4 = new String (ch);

  System.out.println("s1:"+s4);

String s5 = new String (ch,0,1);

  System.out.println("s1:"+s5);

 

}

}

2.arr.length 是属性,s.length()是方法

3.部分转换包前不包后

String s3 = new String(bys, 0, 1) ;转换第0个字节

4.字符串变量相加:先开辟空间,再拼接

字符串常量相加:先拼接,再去字符串常量池找有没有这个变量,有直接返回地址值,没有则开辟新的空间。

例:public class StringDemo4 {

public static void main(String[] args) {

String s1 = "hello";

String s2 = "world";

String s3 = "helloworld";

System.out.println(s3 == s1 + s2);//false

System.out.println(s3.equals((s1 + s2)));//true

 

System.out.println(s3 == "hello" + "world");//true

System.out.println(s3.equals("hello" + "world"));//true

}

}

5. ==和equals()的区别

public class StringDemo3 {

public static void main(String[] args) {

String s1 = new String("hello");

String s2 = new String("hello");

System.out.println(s1 == s2);//false

System.out.println(s1.equals(s2));//true :默认比较是地址值,比较的还内容

 

String s3 = new String("hello");

String s4 = "hello";

System.out.println(s3 == s4);//false

System.out.println(s3.equals(s4));//true

 

String s5 = "hello";

String s6 = "hello";

System.out.println(s5 == s6);//true

System.out.println(s5.equals(s6));//true

}

}

==比较的是地址值,equal默认比较地址值,但实际比较的是内容

.判断功能

1.boolean equals(Object obj):将此字符串和指定的对象进行比较

boolean equalsIgnoreCase(String str):将此字符串和另一个字符串进行比较,忽略大小写

boolean contains(String str):此字符串中是否包含一个子字符串:str

boolean startsWith(String str):判断这个字符串是否以子字符(str)串开头

boolean endsWith(String str):判断这个字符串是否以子字符串(str)结尾

boolean isEmpty():判断该字符串是否为空

2.例题

package practice;

public class P3 {

public static void main(String[] args) {

String s1="helloworld";

String s2="helloworld";

String s3="Helloworld";

//将此字符串与指定的字符串比较

System.out.println(s1.equals(s2));

//忽略大小写的字符串比较

System.out.println(s1.equalsIgnoreCase(s3));

//此字符串是否包含另一个子字符串

System.out.println(s1.contains("owoe"));

//判断这个字符串是否以字符(str)开头

System.out.println(s1.startsWith("hello"));

//判断这个字符串是否以(str)结尾

System.out.println(s1.endsWith("rl"));

//判断该字符串 是否为空

System.out.println(s1.isEmpty());

}

}

.获取功能

1. int length():获取字符串的长度

char charAt(int index):获取指定索引处的字符

int indexOf(int ch):返回指定字符第一次在该字符串出现的索引!

参数中为什么是int?

97 和 'a'--->a

int indexOf(String str):返回指定str子字符串在大字符串中第一次出现的索引

int indexOf(int ch,int fromIndex):返回此字符从指定位置开始后第一次出现的索引

int indexOf(String str,int fromIndex):返回此支持从指定位置开始第一次出现的索引

String substring(int start):截取:从指定位置开始截取,默认截取截取到末尾

String substring(int start,int end):截取功能:从指定位置开始截取到指定位置,包前不包后

2.事例

package practice;

 

public class P4 {

public static void main(String[] args) {

String s = "helloworld";

//获取指定索引处的字符

System.out.println(s.charAt(2));

//返回指定字符第一次出现胡索引

System.out.println(s.indexOf('o'));

//指定字符串第一次出现的索引

System.out.println(s.indexOf("owo"));

    //返回指定位置开始后第一次出现的索引

System.out.println(s.indexOf('d',4));

//从指定位置开始截取,默认截取到末尾

System.out.println(s.substring(8));

//从指定位置截取到指定位置,包前不包后

System.out.println(s.substring(3,8));

}

}

3.例题

键盘录入一个字符串,统计该字符串中大写字符,小写字符,数字字符共有多少个?

public class StringTest2 {

public static void main(String[] args) {

//定义三个统计变量

int bigCount = 0 ;

int smallCount = 0 ;

int numberCount = 0;

//创建键盘录入对象

Scanner sc = new Scanner(System.in);

System.out.println("请输入一个字符串:");

String line = sc.nextLine() ;

//遍历字符串:使用:charAt()和字符串length()

for(int x = 0 ; x < line.length() ; x ++){

 char ch = line.charAt(x) ;

 

 if(ch>='a' && ch<='z'){

 smallCount++;

 }else if(ch>='A' && ch<='Z'){

 bigCount++;

 }else if(ch>='0' && ch<='9'){

 numberCount++ ;

 }

}

//输出结果:

System.out.println("该字符串大写字符有:"+bigCount+"个");

System.out.println("该字符串小写字符有:"+smallCount+"个");

System.out.println("该字符串数字字符有:"+numberCount+"个");

}

}

.转换功能

1. byte[] getBytes():将字符串转换成字节数组

char[] toCharArray():将字符串转换成字符数组

static String valueOf(char[] chs):将字符数组转换成字符串(直接使用String类名就可以用调用)

static String valueOf(int i):表示将一个int类型的数据转成字符串

注意:valueOf():可以将任何数据类型转换成字符串类型

String toLowerCase():将字符串转换成小写

String toUpperCase():将字符串转换成大写

String concat(String str):拼接:字符串拼接

2.事例

package practice;

 

public class P6 {

public static void main(String[] args) {

//定义一个字符串

String s = "Java SE";

//将字符串转换成字节数组

byte [ ] bys = s.getBytes();

for (int i=0;i<bys.length;i++){

System.out.print(bys[i]);

}

//将字符串转换成字符数组

char [] chs = s.toCharArray();

for(int i=0;i<chs.length;i++){

System.out.print(chs[i]);

}

//将字符数组转换成字符串

String s2= String.valueOf(chs);

System.out.println(s2);

//把int类型的数据转换成字符串

String s3=String.valueOf(100);

System.out.println(s3);

//把字符串转换成小写

System.out.println(s.toLowerCase());

//把字符串转换成大写

System.out.println(s.toUpperCase());

//字符串拼接

String s4=s.concat(s2);

System.out.println(s4);

}

}

.替换功能

1.String replace(char old,char new)

String replace(String old,String new)

去除字符串两空格

String trim()

按字典顺序比较两个字符串  

int compareTo(String str)

int compareToIgnoreCase(String str)

2.事例

package practice;

 

public class S7 {

public static void main(String[] args) {

String s ="helloworld";

//替换

String s2= s.replace('l', 'k');

String s3= s.replace("hel", "hhh");

//去空格

String s4=s.trim();

System.out.println(s2+" "+s3+" "+s4);

//字典顺序比较

System.out.println(s2.compareTo(s3));

 

}

}

 

 

 

 

原创粉丝点击