您的位置:首页 > 编程语言 > Java开发

Java中String类的构造方法

2019-04-24 21:17 344 查看

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<='z'){
xiaoxie++;
}else if (c>='A'&&c<='z'){
daxie++;
}else {
shuzi++;
}
}
System.out.println(daxie);
System.out.println(xiaoxie);
System.out.println(shuzi);
}

}
输出结果:
9
6
9

案例三:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)

public class MyTest {
public static void main(String[] args) {
String str="gjgGJGJjkjHkJJJgfuiljn";

String s1 = str.substring(0,1).toUpperCase();
String s2 = str.substring(1, str.length() - 1).toLowerCase();
String concat = s1.concat(s2);
System.out.println(concat);
}
}

案例四:把数组中的数据按照指定个格式拼接成一个字符串
举例:
int[] arr = {1,2,3};
拼接结果:
“[1, 2, 3]”

public class MyTest {
public static void main(String[] args) {
int[] arr={1,2,3};
String str="[";
for (int i = 0; i < arr.length; i++) {
if (i==arr.length-1){
str+=arr[i]+"]";
}else {
str+=arr[i]+",";
}
}
System.out.println(str);
}
}
输出结果:
[1,2,3]

案例五:需求:统计大串中小串出现的次数
举例: "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun”中java出现了5次

public class MyTest {
public static void
24c52
main(String[] args) {
String str="woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
String str1="java";
int count=0;
int i = str.indexOf(str1);
while (i!=-1){

str= str.substring(i + 4);
i = str.indexOf(str1);
count++;
}
System.out.println(count);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: