*01-常用对象API(String类-特点)
package cn.itcast.p1.string.demo;
public class StringDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
* String类的特点:
* 字符串对象一旦被初始化就不会被改变。
*
*/
stringDemo2();
}
public static void stringDemo2() {
// TODO Auto-generated method stub
String s = "abc";//创建一个字符串对象在常量池中。
String s1 = new String("abc");//创建两个对象一个new一个字符串对象在堆内存中。
System.out.println(s==s1);//false
System.out.println(s.equals(s1));
//string类中的equals复写Object中的equals建立了string类自己的判断字符串对象是否相同的依据。
//其实就是比较字符串内容。
// System.out.println("s="+s);
// System.out.println("s1="+s1);
}
/**
* 演示字符串定义的第一种方式,并明确字符串常量池的特点.
* 池中没有就建立,池中有,直接用。
*/
private static void stringDemo1() {
String s = "abc";//"abc"存储在字符串常量池中。
// s = "nba";
String s1 = "abc";
System.out.println(s==s1);//true?
// System.out.println("s="+s);
}
}
ctrl + m 编辑窗口的最大化和还原
*02-常用对象API(String类-构造函数)
将字节数组变成了字符串。
将字符数组变成字符串。
将字符数组的一部分变成字符串。
同样的字节数组也是可以的。
*03-常用对象API(String类-常见功能-获取_1)
StringMethodDemo
方法-->函数-->两个点:1.返回值 2. 参数
String有自己的HashCode方法。
1.1 获取字符串的长度 int length()
1.2 根据位置获得字符 char charAt(int index)
1.3 根据一个字符获得所在位置 int indexOf(int ch) 获取该字符第一次出现的位置 !!!从左边第一个开始索引。
int indexOf(int ch,int fromIndex):从指定位置开始索引该字符第一次出现的位置。
int indexOf(String str) :找字符串
int indexOf(String str,int fromIndex) : 找字符串从指定位置开始
int lastIndexOf(int ch)
int lastIndexOf(int ch,int fromIndex):从指定位置进行ch的查找第一次出现位置
int lastIndexOf(String str);
int lastIndexOf(String str,int fromIndex);
*04-常用对象API(String类-常见功能-获取_2)
1.4 获取字符串中一部分字符串,子串
String substring(int beginIndex,int endIndex) :注意包括beginIndex,不包含endIndex
String substring(int beginIndex)
*05-常用对象API(String类-常见功能-转换)
2.转换
2.1 将字符串转换成字符串数组,用的是字符串的切割。函数里面传进来的是一种规则。
2.2 字符串转成字符数组
char[] toCharArray()
2.3 将字符串转成字节数组
byte[] getBytes
中文为什么是这样的? ascii------>美国标准信息交换码 GB2312 ----> 6、7千 GBK---->扩展了,2w多。 GB180----->又扩展了。
中文是两个字节,设计的时候,第一位设计成1,所以是负数。
2.4将字符串的字母转成大(小)写
String toUppercase()
String toLowerCase():小写
2.5 将字符串中的字符替换
String replace(char oldCh,char newCh)
CharAequence
2.5 String replace(String s1,String s2); 替换字符串
2.6去除字符串两段的空格
2.6 将字符串进行拼接
将字符数组转成字符串,
1.用其中的静态方法
基本类型转成字符串
一样。
06-常用对象API(String类-常见功能-判断)
3 判断
3.1 两个字符串的内容是否相等。
boolean equals.(Object obj)
忽略大小写比较字符串
3.2 判断字符串中是否包含某字符串
这个方法1.5之后才有,但其实,用indexof来寻找就可以了,找到就true,没有就false。
3.3 字符串是否以指定字符串开始、是否以字符串结尾。
startswith() endswith() contains
*07-常用对象API(String类-常见功能-比较)
4.比较
int compareTo(String s1)
基本数值比较用大于小于号,对象比较是用方法来完成的。
*08-常用对象API(String类-intern方法)
看源文件-----光标放在对象上面,按F3就可以跳到这个类中了。【ctrl + 左键】
package cn.itcast.p1.string.demo;
public class StringObjectDemo {
/**
* @param args
*/
public static void main(String[] args) {
// String s1 = "abc";
// String s2 = "abc";
//intern():对字符串池进行操作的
String s1 = new String("abc");
String s2 = s1.intern();
System.out.println(s1==s2);
}
}
----------------------------------小结--------------------------------------
package cn.itcast.p1.string.demo;
public class StringMethodDemo {
/**
* @param args
*/
public static void main(String[] args) {
/*
* 按照面向对象的思想对字符串进行功能分类。
* "abcd"
*
* 1,获取:
* 1.1 获取字符串中字符的个数(长度).
* int length();
* 1.2 根据位置获取字符。
* char charAt(int index);
* 1.3 根据字符获取在字符串中的第一次出现的位置.
* int indexOf(int ch)
* int indexOf(int ch,int fromIndex):从指定位置进行ch的查找第一次出现位置
* int indexOf(String str);
* int indexOf(String str,int fromIndex);
* 根据字符串获取在字符串中的第一次出现的位置.
* int lastIndexOf(int ch)
* int lastIndexOf(int ch,int fromIndex):从指定位置进行ch的查找第一次出现位置
* int lastIndexOf(String str);
* int lastIndexOf(String str,int fromIndex);
* 1.4 获取字符串中一部分字符串。也叫子串.
* String substring(int beginIndex, int endIndex)//包含begin 不包含end 。
* String substring(int beginIndex);
*
*
*
* 2,转换。
* 2.1 将字符串变成字符串数组(字符串的切割)
* String[] split(String regex):涉及到正则表达式.
* 2.2 将字符串变成字符数组。
* char[] toCharArray();
* 2.3 将字符串变成字节数组。
* byte[] getBytes();
* 2.4 将字符串中的字母转成大小写。
* String toUpperCase():大写
* String toLowerCase():小写
* 2.5 将字符串中的内容进行替换
* String replace(char oldch,char newch);
* String replace(String s1,String s2);
* 2.6 将字符串两端的空格去除。
* String trim();
* 2.7 将字符串进行连接 。
* String concat(string);
*
* 3,判断
* 3.1 两个字符串内容是否相同啊?
* boolean equals(Object obj);
* boolean equalsIgnoreCase(string str);忽略大写比较字符串内容。
* 3.2 字符串中是否包含指定字符串?
* boolean contains(string str);
* 3.3 字符串是否以指定字符串开头。是否以指定字符串结尾。
* boolean startsWith(string);
* boolean endsWith(string);
*
* 4,比较。
*
*/
stringMethodDemo_4();
// System.out.println("abc".concat("kk"));
// System.out.println("abc"+"kk");
// System.out.println(String.valueOf(4)+1);
// System.out.println(""+4+1);
}
private static void stringMethodDemo_4() {
System.out.println("abc".compareTo("aqz"));
}
private static void stringMethodDemo_3() {
String s = "abc";
System.out.println(s.equals("ABC".toLowerCase()));
System.out.println(s.equalsIgnoreCase("ABC"));
System.out.println(s.contains("cc"));
String str = "ArrayDemo.java";
System.out.println(str.startsWith("Array"));
System.out.println(str.endsWith(".java"));
System.out.println(str.contains("Demo"));
}
private static void stringMethodDemo_2() {
String s = "张三,李四,王五";
String[] arr = s.split(",");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
char[] chs = s.toCharArray();
for (int i = 0; i < chs.length; i++) {
System.out.println(chs[i]);
}
s = "ab你";
byte[] bytes = s.getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
System.out.println("Abc".toUpperCase());
String s1 = "java";
String s2 = s1.replace('q', 'z');
System.out.println(s1==s2);//true
System.out.println("-"+" ab c ".trim()+"-");
}
private static void stringMethodDemo_1() {
String s = "abcdae";
System.out.println("length:"+s.length());//6
System.out.println("char:"+s.charAt(2));//c//StringIndexOutOfBoundsException
System.out.println("index:"+s.indexOf('k'));//0//-1 我们可以根据-1,来判断该字符或者字符串是否存在。
System.out.println("lastIndex:"+s.lastIndexOf('a'));//4
System.out.println("substring:"+s.substring(2,4));
}
}
练习:
/*
* 1,给定一个字符串数组。按照字典顺序进行从小到大的排序。
* {"nba","abc","cba","zz","qq","haha"}
*
*
* 2,一个子串在整串中出现的次数。
* "nbaernbatynbauinbaopnba"
*
*
*
* 3,两个字符串中最大相同的子串。
*
*
*
* 4,模拟一个trim功能一致的方法。
*
*
*/
package cn.itcast.p1.string.test;
/*
* 1,给定一个字符串数组。按照字典顺序进行从小到大的排序。
* {"nba","abc","cba","zz","qq","haha"}
*
* 思路:
* 1,对数组排序。可以用选择,冒泡都行。
* 2,for嵌套和比较以及换位。
* 3,问题:以前排的是整数,比较用的比较运算符,可是现在是字符串对象。
* 字符串对象怎么比较呢?爽了,对象中提供了用于字符串对象比较的功能。
*
*
*/
public class StringTest_1 {
/**
* @param args
*/
public static void main(String[] args) {
String[] arr = { "nba", "abc", "cba", "zz", "qq", "haha" };
printArray(arr);
sortString(arr);
printArray(arr);
}
public static void sortString(String[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if(arr[i].compareTo(arr[j])>0)//字符串比较用compareTo方法
swap(arr,i,j);
}
}
}
private static void swap(String[] arr, int i, int j) {
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void printArray(String[] arr) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
if (i != arr.length - 1)
System.out.print(arr[i] + ", ");
else
System.out.println(arr[i] + "]");
}
}
}
package cn.itcast.p1.string.test;
/*
* 2,一个子串在整串中出现的次数。
* "nbaernbatynbauinbaopnba"
* 思路:
* 1,要找的子串是否存在,如果存在获取其出现的位置。这个可以使用indexOf完成。
* 2,如果找到了,那么就记录出现的位置并在剩余的字符串中继续查找该子串,
* 而剩余字符串的起始位是出现位置+子串的长度.
* 3,以此类推,通过循环完成查找,如果找不到就是-1,并对 每次找到用计数器记录。
*
*/
public class StringTest_2 {
/**
* @param args
*/
public static void main(String[] args) {
String str = "nbaernbatnbaynbauinbaopnba";
String key = "nba";
int count = getKeyStringCount_2(str,key);
System.out.println("count="+count);
}
public static int getKeyStringCount_2(String str, String key) {
int count = 0;
int index = 0;
while((index = str.indexOf(key,index))!=-1){
index = index + key.length();
count++;
}
return count;
}
/**
* 获取子串在整串中出现的次数。
* @param str
* @param key
* @return
*/
public static int getKeyStringCount(String str, String key) {
//1,定义计数器。
int count = 0;
//2,定义变量记录key出现的位置。
int index = 0;
while((index = str.indexOf(key))!=-1){
str = str.substring(index+key.length());//这种方法会在常量池中产生很多字符串
count++;
}
return count;
}
}
利用切割字符的方法不推荐。
package cn.itcast.p1.string.test;
/*
* 3,两个字符串中最大相同的子串。
* "qwerabcdtyuiop"
* "xcabcdvbn"
*
* 思路:
* 1,既然取得是最大子串,先看短的那个字符串是否在长的那个字符串中。
* 如果存在,短的那个字符串就是最大子串。
* 2,如果不是呢,那么就将短的那个子串进行长度递减的方式去子串,去长串中判断是否存在。
* 如果存在就已找到,就不用在找了。
*
*
*/
public class StringTest_3 {
/**
* @param args
*/
public static void main(String[] args) {
String s1 = "qwerabcdtyuiop";
String s2 = "xcabcdvbn";
String s = getMaxSubstring(s2, s1);
System.out.println("s=" + s);
}
/**
* 获取最大子串
*
* @param s1
* @param s2
* @return
*/
public static String getMaxSubstring(String s1, String s2) {
String max = null,min = null;
max = (s1.length()>s2.length())?s1:s2;
min = max.equals(s1)?s2:s1;
System.out.println("max="+max);
System.out.println("min="+min);
for (int i = 0; i < min.length(); i++) {
for(int a = 0,b = min.length()-i; b != min.length()+1; a++,b++){
String sub = min.substring(a, b);
// System.out.println(sub);
if(max.contains(sub))
return sub;
}
}
return null;
}
}
package cn.itcast.p1.string.test;
/*
* 4,模拟一个trim功能一致的方法。去除字符串两端的空白
* 思路:
* 1,定义两个变量。
* 一个变量作为从头开始判断字符串空格的角标。不断++。
* 一个变量作为从尾开始判断字符串空格的角标。不断--。
* 2,判断到不是空格为止,取头尾之间的字符串即可。
*/
public class StringTest_4 {
/**
* @param args
*/
public static void main(String[] args) {
String s = " ab c ";
s = myTrim(s);
System.out.println("-" + s + "-");
}
public static String myTrim(String s) {
int start = 0, end = s.length() - 1;
while (start <= end && s.charAt(start) == ' ') {
start++;
}
while (start <= end && s.charAt(end) == ' ') {
end--;
}
return s.substring(start, end + 1);
}
}