Java中String类的构造方法 您所在的位置:网站首页 java字符串转字符数组方法有几种 Java中String类的构造方法

Java中String类的构造方法

2024-07-13 10:59| 来源: 网络整理| 查看: 265

String类的构造方法

String代表字符串,字符串是由多个字符组成的一串数据,字符串可以看成字符数组, 1.字符串字面值“abc”也可以看成一个字符串的对象 2.字符串是常量,一旦被创建,就不能改变 3.字符串可以看做是一个长度固定的有序字符序列,每个组成的字符编有索引从0开始

常见的构造方法 public String():空构造 public String(byte[] bytes):把字符串数组反转成字符串

public class MyTest { public static void main(String[] args) { byte[] b={'2','2','4','6'}; String s = new String(b); System.out.println(s); } } 把字符数组的一部分转成字符串 public class MyTest { public static void main(String[] args) { char[] dat={'a','d','c'}; String s = new String(dat,0,1); System.out.println(s); } }

public String ( String original):把字符常量值转成字符串

String的特点一旦被创建就不能改变

因为字符串的值是在方法区的常量池中划分空间分配地址值

a:如何理解这句话 String s = “hello” ; s = “world” + “java”; 问s的结果是多少?

他的内存图

在这里插入图片描述 String s = new String(“hello”)和String s = “hello”;的区别 在这里插入图片描述

1.首先,通过main()方法进栈。 2.然后再栈中定义一个对象s1,去堆中开辟一个内存空间,将内存空间的引用赋值给s1,“hello”是常量,然后去字符串常量池 查看是否有hello字符串对象,没有的话分配一个空间存放hello,并且将其空间地址存入堆中new出来的空间中。 3.在栈中定义一个对象s2,然后去字符串常量池中查看是否有”hello”字符串对象,有,直接把”hello”的地址赋值给s2. 4.即s1中存的是堆中分配的空间,堆中分配的空间中存的是字符串常量池中分配空间存放”hello”的空间的地址值。而s2中之间存的是字符串常量池中分配空间存放”hello”的空间的地址值。

5.由于s1与s2中存放的地址不同,所以输出false。因为,类String重写了equals()方法,它比较的是引用类型的 的值是否相等,所以输出true。即结果为false、true

String类的判断功能

public boolean equals(Object obj): 比较字符串的内容是否相同,区分大小写 public boolean equalsIgnoreCase(String str): 比较字符串的内容是否相同,忽略大小写 public boolean contains(String str): 判断字符串中是否包含传递进来的字符串 public boolean startsWith(String str): 判断字符串是否以传递进来的字符串开头 public boolean endsWith(String str): 判断字符串是否以传递进来的字符串结尾 public boolean isEmpty(): 判断字符串的内容是否为空 例如

public class MyTest2 { public static void main(String[] args) { String str="sui"; String str1="hello"; String str2="HELLO"; boolean equals = str.equals(str1); boolean b = str1.equalsIgnoreCase(str2); boolean u = str.contains("u"); boolean h = str1.startsWith("h"); boolean l = str1.endsWith("o"); boolean empty = str1.isEmpty(); System.out.println(equals); System.out.println(b); System.out.println(u); System.out.println(h); System.out.println(l); System.out.println(empty); } } 输出结果: false true true true true false

public int length(): 获取字符串的长度。 public char charAt(int index): 获取指定索引位置的字符 public int indexOf(int ch): 返回指定字符在此字符串中第一次出现处的索引。 public int indexOf(String str): 返回指定字符串在此字符串中第一次出现处的索引。 public int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。 public int indexOf(String str,int fromIndex): 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。public String substring(int start): 从指定位置开始截取字符串,默认到末尾。

public String substring(int start,int end): 从指定位置开始到指定位置结束截取字符串。 例如:

public class MyTest3 { public static void main(String[] args) { String str="我是这条街最靓的仔"; int length = str.length(); System.out.println(length); char c = str.charAt(str.length() - 1); System.out.println(c); int i = str.indexOf("条"); System.out.println(i); String substring = str.substring(1, 4); System.out.println(substring); } } 输出结果: 9 仔 3 是这条

public byte[] getBytes(): 把字符串转换为字节数组。 public char[] toCharArray(): 把字符串转换为字符数组。 public static String valueOf(char[] chs): 把字符数组转成字符串。 public static String valueOf(int i): 把int类型的数据转成字符串。 注意:String类的valueOf方法可以把任意类型的数据转成字符串。 public String toLowerCase(): 把字符串转成小写。 public String toUpperCase(): 把字符串转成大写。 public String concat(String str): 把字符串拼接。

例如:下面程序就是对这些方法的应用

String str="我喜欢你"; byte[] bytes = str.getBytes(); char[] chars = str.toCharArray(); System.out.println(bytes); System.out.println(chars); int n=8478; double s1=2763.332; String s3 = String.valueOf(s1); String s = new String(String.valueOf(n)); System.out.println(s); String str1="GHJKJjjooHUjijh"; String s11 = str1.toUpperCase(); String s2 = str1.toLowerCase(); String concat = str.concat(str1); System.out.println(s1); System.out.println(s2); System.out.println(concat); System.out.println(s3); } } 输出结果: [B@1540e19d 我喜欢你 8478 2763.332 ghjkjjjoohujijh 我喜欢你GHJKJjjooHUjijh 2763.332

public String replace(char old,char new) 将指定字符进行互换 public String replace(String old,String new) 将指定字符串进行互换 public String trim() 去除两端空格 String的按字典顺序比较两个字符串及案例演示 public int compareTo(String str) 会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果 如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果 如果连个字符串一摸一样 返回的就是0 public int compareToIgnoreCase(String str) 跟上面一样 只是忽略大小写的比较

例如:下面是对方法的应用

public class MyTest3 { public static void main(String[] args) { String str="我喜欢杜兰特".replace("我","你"); System.out.println(str); String str1="我喜欢詹姆斯".replace("詹姆斯","欧文"); System.out.println(str1); String str2=" sgh ".trim(); System.out.println(str2); int str3="a".compareTo("A"); System.out.println(str3); int str4="b".compareToIgnoreCase("B"); System.out.println(str4); } } 输出结果: 你喜欢杜兰特 我喜欢欧文 sgh 32 0 String类功能的应用

案例一: 需求:模拟登录,给三次机会,并提示还有几次。

public class MyTest3 { public static void main(String[] args) { String username = "张三"; int password = 123456; Scanner sc = new Scanner(System.in); int n = 3; while (n > 0) { System.out.println("请输入你的用户名"); String s = sc.nextLine(); System.out.println("请输入你的密码"); String i = sc.nextLine(); if (s.equals(username) && i.equals(password)) { System.out.println("登录成功"); break; } else { n--; if (n == 0) { System.out.println("次数已用完"); } else { System.out.println("你输入有误,你还有" + n + "次机会"); } } } } }

案例二:需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)

public class MyTest4 { public static void main(String[] args) { String str="HKHK78789hkbjkHHJKJ879o8"; int daxie=0; int xiaoxie=0; int shuzi=0; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if(c>='a'&&c='A'&&c


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有