Java API_String类


String类

1.String类的概述和构造方法以及相关方法

(1).String类概述
字符串就是由多个字符组成的一串数据。也可以看成是一个字符数组。

(2).String类的构造方法
public String():空参构造
public String(byte[] bytes):把字节数组转化成为字符串
public String(byte[] bytes,int index,int length):把字节数组的一部分转化成为字符串
public String(chart[] value):把字符数组转成字符串
public String(chart[] value,int index,int count):把字符数组的一部分转成字符串
public String(String original):把字符串常量值转成字符串


使用举例:
public static void main(String[] args) throws CloneNotSupportedException {
char[] chs={'a','b','c','d','e'};
String str=new String(chs);
System.out.println(str);
}
//输出结果:
abcde

(3).String类的面试题1
String s=new String("hello");和String s="hello";有什么区别?
String s=new String("hello");
创建了俩个对象,一个是先创建了"hello"的字符串常量,然后又创建了一个对象s(new 出来的东西位于堆内存),引用的是堆内存的地址
String s="hello"
只创建了一个对象,引用的是方法区里面字符串常量池里面的地址。

(4).String类的面试题2
看程序写结果并分析:
public static void main(String[] args) throws CloneNotSupportedException {
String str1="hello";
String str2="world";
String str3="helloworld";
System.out.println(str3==str1+str2);
System.out.println(str3.equals(str1+str2));
}
//输出结果:
false
true

分析:
"==": 比较引用类型比较的是地址值是否相同
equals String类型重写了equal()方法,比较的是内容是否相同

字符串如果是变量相加,先开空间,然后再拼接
字符串如果是常量相加,是先加,然后再常量池里面找,如果有就直接返回,否则就创建。


(5).String类的判断功能
boolean equals(Object obj):比较字符串的内容是不是相同,区分大小写
boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
boolean contains(String str):判断字符串中是否包含特定字符串
boolean startWidth(String str):判断字符串是否以某个指定的字符串开头
boolean endsWidth(String str):判断字符串是否以某个指定的字符串结尾
boolean isEmpty();判断字符串是否为空

使用注意事项:
String s=""; //可以调用方法
String s=null; //不可以调用方法
字符串内容为空(存在对象但是没有内容)和字符串对象为空(表示的是连对象都不存在)


(6).String类的获取功能
int length():获取字符串的长度
char charAt(int index):获取指定索引位置的字符
int indexOf(int ch):返回指定字符在此字符串中第一次出现的索引(注意是字符)
int indexOf(String str):返回指定字符串在此字符串中第一次出现的索引(注意是字符串)
int indexOf(int ch,int formIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引
int indexOf(String str,int formIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引
String subString(int start):从指定位置开始截取字符串。默认到末尾
String subString(int start ,int end):从指定位置开始到指定位置结束截取字符串。


(7).String类的基本使用举例

A:变量获取字符串中的每一个字符
基本方法
如何获取每一个字符
char chartAt(int index);
获取有多少个字符
int length()

代码:
public static void main(String[] args){
String str ="helloWorld";
for(int i=0;i<str.length();i++){
System.out.println("第"+i+"获取的字符是:"+str.charAt(i));
}
}

//输出结果:
第0获取的字符是:h
第1获取的字符是:e
第2获取的字符是:l
第3获取的字符是:l
第4获取的字符是:o
第5获取的字符是:W
第6获取的字符是:o
第7获取的字符是:r
第8获取的字符是:l
第9获取的字符是:d

B:统计字符串中的大小写、数字
使用基本方法
char chartAt(int index);
int length();

大小写数字ASCII码
0 48
A 65
a 97

//判断使用代码
if(ch>='0'&&ch<='9'){
numberCount++;
}
if(ch>='a'&&ch<='z'){
smallCount++;
}
if(ch>='A'&&ch<='A'){
bigCountCount++;
}

代码实现:
public static void main(String[] args) {
//定义需要寻找的字符串
String str="HeLLoWorld129090";
//定义三个统计不了
int bigCount=0;
int smallCount=0;
int numberCount=0;
//遍历字符串,获取每一个字符
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
//判断是否属于哪一种类型
if(ch>='a'&&ch<='z'){
smallCount++;
}else if(ch>='A'&&ch<='Z'){
bigCount++;
}else if(ch>='0'&&ch<='9'){
numberCount++;
}
}
//输出结果
System.out.println(str+":中大写字母有:"+bigCount+"个---小写字母有:"+smallCount+"个---数字有"+numberCount+"个");
}

输出结果为:
HeLLoWorld129090:中大写字母有:4个---小写字母有:6个---数字有6个


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


(9).String类的其他功能
替换功能
String replace(char old,char new):把旧字符转化成为新字符
String replace(String old,String new):把旧的字符串转化成为新字符串

去除字符的俩端空格
String trim()

按照字典顺序比较俩个字符串
int compareTo(String str)
int compareToIgnoreCase(String str)

使用举例:
public static void main(String[] args) {
String str="helloWorld";
String str1=" abc ";
String str2=" abcd ";
System.out.println(str.replace("h", " "));
System.out.println(str.replace("hello", " "));
System.out.println(str1.compareTo(str2));
System.out.println(str1.trim());
System.out.println(str1.compareToIgnoreCase(str2));
}
//输出结果为
elloWorld
World
-68
abc
-68

(10).字符串的基本使用举例2

A:字符串反转
功能结果:键盘输入"abc",输出结果为"cba"

设计分析:
a:键盘输入一个字符串
b:定义一个新的字符串
c:倒着遍历这一个字符串
length()方法和charAt()结合
把字符串转化成为字符数组
d:用新的字符串把每一个字符拼接起来
e:输出新字符串

代码实现:
public class Test {
public static void main(String[] args) {
//键盘输入
@SuppressWarnings("resource")
Scanner sn =new Scanner(System.in);
System.out.println("请输入一个字符串:");
String line=sn.nextLine();
String s=reverseString(line);
System.out.println("倒叙以后的结果是:"+s);
}

/**
*字符串反转
*@author Administrator
*@param String str:原始字符串
*@return String str:结果字符串
*
*/
public static String reverseString(String str){
//定义返回字符串
String result="";
//把字符串转化成为字符数组
char[] chs=str.toCharArray();
//倒着遍历,获取每一个字符串
for(int i=chs.length-1;i>=0;i--){
//使用新字符串把每一个字符拼接起来
result +=chs[i];
}
return result;
}
}


B:统计一个字符串中小字符串出现的次数

代码设计分析:
定义一个大字符串
定义一个小字符串

a:定义一个统计变量,并初始化值为0
b:现在大字符串中查找小字符串中出现的第一次位置
如果说索引是-1,说明在大字符串中没有出现小字符串
如果说索引不是-1,说明存在,统计变量++
c:把刚才的索引+小字符串的长度作为开始位置截取上一次的大字符串,返回一个新的字符串,并把该字符串的值重新赋值给大字符串
d:返回到b过程

代码实现
public class Test {
public static void main(String[] args) {
//定义大的字符串
String maxString="javafjhfvsadvfkashjfvasjjavaalhsdfajavabs";
//定义小字符串
String minString="java";
int count=getCount(maxString, minString);
System.out.println(minString+"出现的次数是:"+count);

}

/**
*统计大的字符串中小字符串出现的次数
*@author Administrator
*@param String str:原始的字符串
*@return int :返回出现的次数
*
*/
public static int getCount(String maxString,String minString){
//定义一个统计变量
int count=0;
//先在大串中查找小串第一次出现的位置,然后再复制
int index=maxString.indexOf(minString);
//如果说索引并不是-1,说明存在,统计变量++
while((index=maxString.indexOf(minString))!=-1){
count++;
maxString=maxString.substring(index+minString.length());
}
return count;
}
}
//测试结果
java出现的次数是:3

猜你喜欢

转载自www.cnblogs.com/nwxayyf/p/9461742.html