---------------------- 黑马程序员 Android培训、期待与您交流! ----------------------
获取和判断
/*
* String类适用于描述字符串事物:
* 那么他那提供了多个方法,对字符串进行操作。
*
* 常见操作有哪些呢?
* “abcd”
*
* 1. 获取
* 1.1 字符串中包含的字符数,也就是字符串的长度?
* int length();获取长度
* 1.2 根据位置获取位置上某个字符?
* char charAt(int index);
* 1.3 根据字符获取该字符在字符串中的位置。
* int indexOf(int ch);返回的是ch在字符串中第一次出现的位置。
* int indexOf(int ch,int formIndex); 从formIndex指定位置开始,获取ch在字符串中的位置。
*
* intindexOf(String str);返回的是str在字符串中第一次出现的位置。
* int indexOf(String str, intformIndex);从formIndex指定位置开始,获取str在字符串中出现的位置。
*
* int lastIndexOf(int ch);
*
* 2. 判断
* 2.1 字符串中是否包含某一个字符串?
* boolean contains(str);
* 特殊之处:indexOf(str):可以索引str第一次出现的位置,如果返回-1。
* 表示该str不在字符串中存在,所以也可以用于对指定判断是否包含。
*
* if(str.indexOf("aa")!=-1)
* 该方法即可以判断,又可以获取出现的位置
*
* 2.2 字符串中是否有内容?
* boolean isEmpty();原理就是判断长度是否为0
*
* 2.3 字符串是否以指定内容开头?
* boolean startswith(str);
*
* 2.4 字符串是否以指定内容结尾?
* boolean endswith(str);
*
* 2.5 判断字符串的内容是否相同?复写了Object类中的equals方法
* boolean equlas(str);
*
* 2.6 判断内容是否相同,并忽略大小写?
* boolean equalsIgnoreCase();
public class _02获取和判断 {
public static void main(String[]args)
{
method_get();
//System.out.println("abc");
}
public static void method_get()
{
String str = "abcdeakpf";
//长度
System.out.println(str.length());
//根据索引获取字符
System.out.println(str.charAt(4));
//当访问到字符串中不存在的角标时,会发生StringIndexOutOfBoundsException角标越界
//根据字符获取索引 (从指定位置开始获取要查找字符在字符串中的位置)
System.out.println(str.indexOf('m',3));//如果没有找到,返回-1
//反向索引一个字符出现的位置
System.out.println(str.lastIndexOf("a"));
//-----------------------------------------------------------------------------
String arr = "ArrayDemo.java";
//判断是否以指定字符串开头
System.out.println(arr.startsWith("Array"));
//判断是否以指定字符串结尾
System.out.println(arr.endsWith(".java"));
//判断是否包含指定字符串
System.out.println(arr.contains("Demo"));
//判断两个字符串内容是否相同,并忽略大小写
String s3 = "ABC";
String s4 = "Ab";
String s5 = "abc";
System.out.println(s3.equalsIgnoreCase(s4));
System.out.println(s3.equalsIgnoreCase(s5));
}
}
String类_字节与字符串之间的转换
/*
* 3, 转换
* 3.1将字符数组转成字符串
* 构造函数:String(char[])
* String(char[],offset,count):将字符数组中的一部分转成字符串
* 静态方法:
* staticString copyValueOf(char[]);
* staticString copyValueOf(char[] data, int offset, int count);
*
* staticString ValueOf(char[]);
*
*
* 3.2将字符串转成字符数组*****
* chartocharArray();
*
*
* 3.3将字节数组转成字符串
* String(byte[])
* String(byte[],offset,count);将字节数组中的一部分转成字符串。
*
* 3.4将字符串转成字符数组
* byte[]getBytes();
*
* 3.5将基本数据类型转成字符串
* staticString ValueOf(int);
* staticString ValueOf(double);
*
* //3+"";等同于String.ValueOf(3);
*
*--------------------------------------------------------------------------
* 特殊注意;字符串和字符数组在转换过程中,是可以指定编码表的。
*
*
*/
public class _03String类_转换 {
public static void main(String[]args)
{
char[] arr = {'a','b','c','d','e','f'};
//指定数组中的位置,及输出的个数
String s = new String(arr,1,3);
System.out.println(s);
//将字符组转换成字符串
char[] arr1 = {'中','华','人','民','共','和','国'};
System.out.println(arr1);
//将字符串转换成字符组
String s1 = "zxcvbnm";
//接收的char数组
char[] ch = s1.toCharArray();
for(int x=0; x<ch.length;x++)
{
System.out.println("ch="+ch[x]);
}
}
}
String类_切割和替换
/*
* 4. 替换
* Stringreplace(oldchar,newchar);
*
* 5. 切割
* String[]spilt (regex);
*
* 6. 子串。获取字符串中的一部分。
* StringsubString(geginIndex,endIndex);
*/
public class _04_String类_切割和替换 {
public static void main(String[]args)
{
String s = "Hello Java!";
//单个字符的替换方式-----------------------------
String s1 = s.replace('a','n');//如果要替换的字符不存在,返回的还是原字符串。
System.out.println(s);
System.out.println(s1);
//字符串的替换方式-----------------------------
String s2 = "Hello World!";
String s3 = s2.replace("Hello", "您好");
System.out.println(s3);
//切割----------------------------------------------------
String s4 = "zhangSan,Lisi,Wangwu";
String[] arr = s4.split(",");
for(int x=0; x<s4.length();x++)
{
System.out.println(arr[x]);
}
//子串--------------------------------
String s5 = "abcdef";
System.out.println(s5.substring(2));//这是从2开始到最后取出所有的字符
//这是从2到4取出之间的字符(但包含头,不包含尾)--s.substring(0,s.length());
System.out.println(s5.substring(2, 4));
}
}
String类_比较和去除空格
/*
* 7. 转换,去除空格。比较
* 7.1转字符串转成大写或者小写。
* StringtoUpperCase();
* StringtoLowerCase();
*
* 7.2将字符串两端的之个空格去除。
* Stringtrim();
*
* 7.3对两个字符串进行自然顺序的比较。
* intcompareTo(String);
*/
public class _05_String类_比较和去除空格 {
public static void main(String[]args)
{
String s = " Hello Java ";
System.out.println(s.toLowerCase());//Lower转小写
System.out.println(s.toUpperCase());//Upper转大写
System.out.println(s.trim());//去除两端空格
String s1 = "a1c";
String s2 = "aaa";
System.out.println(s1.compareTo(s2));//输出结果是 -48 a=97 1=49 (a-1=48)
}
}
经典习题
/*
* 获取两个字符串中,最大相同字符串。
* 第一个动作,将短的那个字符串依次递减打印。
*/
public class _09_练习4_获取最大相同字符串 {
public static void main(String[]args)
{
String s1="abcwerthelloyuiodef";
String s2="cvhellobnm";
System.out.println(getMaxString(s1,s2));
}
public static String getMaxString(String s1,String s2)
{
//因为两个字符串的长度不确定哪一个长,所以定义两个变量判断其长度
String max = "",min = "";
max = (s1.length()>s2.length()?s1:s2);//s1的长度比s2的长度大嘛?是就返回s1,不是就返回s2
min = (max==s1)?s2:s1;//max与s1相等嘛?是就返回s2,不是就返回s1;
for(int x=0;x<min.length();x++)
{
for(int y=0,z=min.length()-x; z!=min.length()+1;y++,z++)
{
String temp=min.substring(y,z);
if(max.contains(temp))
{
return temp;
}
}
}
return "";
}
}
---------------------- 黑马程序员 Android培训、期待与您交流! ----------------------