String类

String类---------------------------------------------

字符串:就是由多个字符组成的一串数据,也可以看成一个字符数组
通过查看api,我们可以知道:
1.字符串字面值“abc”也可以看成是一个字符串对象
2.字符串是常量,一旦被赋值,就不能改变。这里的不能改变是说值本身,引用是可以改变的
构造方法:
public String();
空构造
public String(byte [] bytes):
把字节数组转成字符串
public String(byte [] bytes,int index,int length)
把字节数组的一部分转成字符串
public String(char [] value)
把字符数组转成字符串
public String(char [] value,int index,int count)
把字符数组的一部分转成字符串
public String(String original)
把字符串常量值转成字符串
字符串的方法:
public int length()
返回此字符串的长度

字符串直接赋值的方式是先到字符串常量池里面去找,如果有就直接返回,没有,就创建并返回
一旦被赋值就不能被改变,这里说的不能改变,是值不能改变,而不是引用

面试题
String s = new String("hello")和String s = "hello"的区别
有区别,前者或创建两个对象,后者创建1个对象

结论:
new出来的,在方法区的常量池和堆区各一个,
直接赋值的,只会在常量池中找,跟堆区没什么关系

面试题:
String s1 = new String("hello");
String s2 = new String("hello");
Sytem.out.println(s1==s2);//false
Sytem.out.println(s1.equals(s2));//true

String s3 = new String("hello");
String s4 = "hello";
Sytem.out.println(s3 == s4);//false
Sytem.out.println(s3.equals(s4));//true

String s5 = "hello";
String s6 = "hello";
System.out.println(s5 == s6);//true
Sytem.out.println(s5.equals(s6));//true


结论:字符串如果是变量相加,先另外开空间,再拼接
字符串如果是常量相加,先拼接,然后再从常量池找,有就返回,没有就创建
面试题:
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
System.out.prrintln(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

判断功能:
boolean equals(Object obj)
判断字符串的内容是否相同
boolean equalsIgnoreCase(String str)
判断字符串的内容是否相同(区分大小写)
boolean contains(String str)
判断大字符串中是否包含小字符串
boolean startWith(String str)
判断字符串是否以某个指定的字符串开头
boolean endsWith(String str)
判断字符串是否以某个指定的字符串结尾
boolean isEmpty()
判断字符串是否为空

获取功能:
int length()
获取字符串长度
char charAt(int index)
获取指定索引位置的字符
int indexOf(int ch)
获取指定字符在此字符串中第一次出现处的索引
这里为什么是int类型,而不是char类型
因为是:‘a’和97其实都可以表示char‘a’
int indexOf(String str)
返回指定字符串在此字符串中第一次出现处的索引
int indexOf(int ch,int fromIndex)
返回指定字符在此字符串中从指定位置后第一次出现处的索引
int indexOf(String str,int fromIndex)
返回指定字符串在此字符串中从指定位置后第一次出现处的索引
String substring(int start)
从指定位置开始截取字符串,默认到末尾
string subString(int start,int end)
从指定位置开始到指定位置结束截取字符串

需求:遍历获取字符串中的每一个字符

分析:如何能够拿到每一个字符串呢?
char charAt(int index)
我怎么知道字符到底多少个呢?
int length()

String s = "helloworld";
for(int i = 0;i<s.length();i++){
System.out.println(s.charAt(i));
}


String类的转换功能
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)
把两个字符串进行拼接


需求把一个字符串的首字母转成大写,其余为小写(只考虑英文大小写字母字符)
* 分析:
* 1.先获取第一个字符
* 2.获取除了第一个字符的以外字符
* 3.把第一个字符转成大写
* 4.把第一个字符转成大写
* 5.把除第一个字符以外的字符转成小写
* 6.字符串拼接
* */
public class zifu {
public static void main(String[] args) {
String str = "helloWORLD";
String str2 = str.substring(0,1);
String str3 = str.substring(1);

String str4 = str2.toUpperCase();
String str5 = str3.toLowerCase();

String str6 = str4.concat(str5);
System.out.println("结果是:"+str6);
//优化代码
System.out.println(str.substring(0,1).toUpperCase().concat(str.substring(1).toLowerCase()));

}
}

 

String类的替换功能

替换功能
String replace(char old,char new)
String replace(String old,String new)

去除字符串两端空格
String trim()

按字典顺序比较两个字符串
int compareTo(String str)
区分大小写
int compareToIgnoreCase(String str)
不区分大小写

 

 

需求:把数组中的数据按照指定的格式拼接成一个字符串
* 举例:
* int [] arr = {1,2,3};
* 输出结果
* “[1, 2, 3]”
* 分析:
* 1.定义一个字符串对象,只不过内容为空
* 2.把字符串拼接一个“[”
* 3.遍历数组,得到每一个元素
* 4.先判断该元素是否为最后一个
* 是:就直接拼接元素和]
* 不是:就拼接元素和逗号以及空格
* 5.输出拼接后的字符串
* */
public class zifu {
public static void main(String[] args) {

int[] arr = {1, 2, 3};
String s = "";
s += "[";
for(int i = 0;i<arr.length;i++){
if(i == arr.length-1){
s += arr[i];
s += "]";
}
else
{
s += arr[i];
s +=", ";
}
}
System.out.println("最终的字符串是:"+s);
}
}

字符串反转
* 举例:键盘录入 “abc”
* 结构:输出“cba”
*
* 分析:1.键盘录入一个字符串
* 2.定义一个新字符串
* 3.倒着遍历字符串,得到每一个字符
* a.length()和charAt()结合
* b.把字符串转换成字符数组
* 4,用新字符串把每一个字符拼接起来
* 5输出新字符串
* */
public class FanZhan {
public static void main(String[] args) {
/*
Scanner sc = new Scanner(System.in);
System.out.println("请输入字母:");
String s = sc.nextLine();
String result = "";
char[] chs = s.toCharArray();
for(int i = chs.length-1;i>=0;i--){
result += chs[i];
}
System.out.println(result);*/


Scanner sc = new Scanner(System.in);
System.out.println("请输入字母:");
String s = sc.nextLine();
String result = "";
for(int i =s.length()-1;i>=0;i--){
result += s.charAt(i);
}
System.out.println(result);


public class Count {
/*
* 统计大串中小串出现的次数
* 举例:在字符串“woaijavawozhendeaijavawozhendejava”
* 结果;
* java出现了5次
*
* 分析:
* 前提知道了大串和小串
* 1.定义一个统计变量,初始化值是0;
* 2.先在大串中查找一次小串第一次出现的位置
* a.索引是-1,说明不存在了,就返回统计变量
* b.索引不是-1,说明存在,统计变量++
* 3.把刚才的索引+小串的长度作为开始位置截取上一次的大串
* 返回一个新的字符串,并把该字符串的值重新赋值给大串
* 4.回到2
* */
public static void main(String[] args) {
String maxString = "woaijavawozhenjavadeaijavawozhjavaendejava";
String minString = "java";
int count = getCount(maxString,minString);
System.out.println("子串出现的次数是:"+count);
}
public static int getCount(String maxString,String minString){
//定义一个统计变量,初始化值是0
int count = 0;
//先在大串中查找一次小串第一次出现的位置
int index = maxString.indexOf(minString);
//索引是-1,说明不存在了,就返回统计变量
while(index != -1){
count++;
//把刚才的索引+小串的长度作为开始位置截取上一次的大串
//返回一个新的字符串,并把字符串重新赋值给大串
//int startIndex = index + minString.length();
//maxString = maxString.substring(startIndex);
//以上两行代码可优化为
maxString = maxString.substring(index + minString.length());
//继续查
index = maxString.indexOf(minString);
}
return count;
}
}

 

posted @ 2015-12-23 00:44  Deleting  阅读(158)  评论(0编辑  收藏  举报