import sun.text.normalizer.UTF16;
import java.io.UnsupportedEncodingException;
/**
* Created by Kodulf on 2017/4/4.
*/
public class StringTest {
int num = 88;
/**
* 影城流行不同不变,或判换换(获取,判断,转换(最重要),替换),尝滋味包拯(等)开涮复羯鸡,忍受兔兔吃素, 开包吉步
* 影城流行不同不变。影城(y引号,常量c池),流行(new 新的)不同(equals 和== 不一样)不变(字符串常量,不能够改变)
* 获取:长度,字符,位置,(尝滋味)
* 判断:包拯(等),开刷(开始结束)
* 转换:字符数组,字节数组,基本数据类型, 复(一个是字符数组的意思,还有复也有两种的意思,静态方法和构造方法)羯(字节数组)鸡
* 替换:忍受兔兔吃素,r replace subString,toUpCase toLowCase, trim(), compareTo,split
* 从某个位置开始查找,注意这里的fromindex 是包含这个index的
* 开始的是包含的,结束的是不包含的,开包吉步
*/
public static void main(String[] args){
stringTest();
stringTestGet();
testStringPanDun();
try {
testStringChange();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
testStringReplace();
testBianjie();
}
/**
*
* 开始的是包含的,结束的是不包含的,开包吉步
* 测试边界,一定要注意的是indexOf如果是没有那么返回的是-1,和io流里面是一样的
* indexOf(fromIndex) fromIndex是包含的
* new String 的byte 里面也是fromIndex 是包含的,后面的是长度, copyValueOf(char[],fromIndex,endIndex)
* subString(fromIndex,endIndex)fromIndex 是包含的,endIndex 是不包含的。
*/
public static void testBianjie(){
String aa = "01234567789";
System.out.println(aa.indexOf('2',2));
byte[] bytes = {'0','1','2','3','4','5','6','7','8','9'};
System.out.println(new String(bytes,2,2));
System.out.println(aa.substring(2,3));
}
/**
* 获取:长度,字符,位置,(尝滋味)
*/
public static void stringTestGet(){
String aa = "hello hell hee hahah he ha";
System.out.println(aa.length());
System.out.println(aa.charAt(1));
System.out.println(aa.indexOf('a'));
System.out.println(aa.indexOf('a',0));
System.out.println(aa.indexOf("he"));
System.out.println(aa.indexOf("he",0));
System.out.println(checkEmail("helloworld@500mi.com")?"验证成功":"验证失败");
System.out.println(checkUserNameAndPassword("haha","password")?"账号密码正确":"账号密码错误");
}
/**
* 包拯(等),开涮
*/
public static void testStringPanDun(){
System.out.println("hello.world".contains("wor"));
System.out.println("hello.world".equals("hello.World"));
System.out.println("hello.world".equalsIgnoreCase("Hello.World"));
System.out.println("hello.world".startsWith("he"));
System.out.println("hello.world".endsWith("ld"));
}
/**
* 复羯鸡
* 字符数组,字节数组,基本数据类型, 复(一个是字符数组的意思,还有复也有两种的意思,静态方法和构造方法)羯(字节数组)鸡
* 字符数组,new String(chars),copyValueof
* 字节数组,new String(bytes),new String(bytes,offset,length);
* 鸡,基本数据类型,valueOf()
*/
public static void testStringChange() throws UnsupportedEncodingException {
char[] chars = {'a','b','c'};
System.out.println(new String(chars));
System.out.println(new String(chars,0,2));
System.out.println(String.copyValueOf(chars));
System.out.println(String.copyValueOf(chars,0,2));
String aa = "abcdefg";
char[] chars1 = aa.toCharArray();
for (int i = 0; i < chars1.length; i++) {
System.out.println(chars1[i]);
}
byte[] bytes = "hijklmn".getBytes();
System.out.println(new String(bytes));
System.out.println(new String(bytes,0,5));
System.out.println(new String(bytes, "utf-8"));
String.valueOf('c');
String.valueOf((short)1);
}
/**
* 测试替换的,忍受兔兔吃素
* 忍受兔兔吃素,r replace subString,toUpCase toLowCase, trim(), compareTo,split
*/
public static void testStringReplace(){
String aa = "0123456789aa bbcc ";
String replace = aa.replace("aa", "bb");
System.out.println("替换后的"+replace);
//子串
String substring = aa.substring(3);
System.out.println("子串:"+substring);
//子串计算的时候开始是[s,e)开始的是计算在内的
String substring1 = aa.substring(3, 4);
System.out.println("子串:"+substring1);
//忍受兔兔吃素r replace subString,toUpCase toLowCase, trim(), compareTo,split
String s = aa.toLowerCase();
System.out.println(s);
String s1 = aa.toUpperCase();
System.out.println(s1);
String trim = aa.trim();
System.out.println(trim);
int abcdef = aa.compareTo("0123456789");
System.out.println(abcdef);
}
public static boolean checkEmail(String email){
boolean checkEmailResult = false;
if(StringUtil.isNotEmpty(email)) {
if (email.indexOf('@') < email.indexOf('.')) {
checkEmailResult = true;
}
}
return checkEmailResult;
}
public static boolean checkUserNameAndPassword(String userName,String password){
boolean ret = false;
if(StringUtil.isNotEmpty(userName)&&StringUtil.isNotEmpty(password)){
if(userName.equals("haha")&&password.equals("password"))
ret = true;
}
return ret;
}
/**
* String的测试:
* 影城流行不同不变。影城(y引号,常量c池),流行(new 新的)不同(equals 和== 不一样)不变(字符串常量,不能够改变)
* 引号,的如果在常量池里面有了,那么就会调用常量池里面的
* new 新,流行,new出来的都是新的
* 不同,equals 方法有重写,== 比较的是地址
* String 字符串是常量,他们的只在创建以后就不能够修改了,例如下面的"hello world",就是不能修改的,如果说修改,其实是将修改后的新的字符串常量的地址给aa了
*/
public static void stringTest(){
String aa ="hello world";
String bb = "hello world";
System.out.println(aa==bb);
System.out.println(aa.equals(bb));
String cc = new String("hello world");
System.out.println(aa==cc);
}
}
class StringUtil{
public static boolean isNotEmpty(String string){
boolean ret = false;
if(string!=null&&string.length()>0){
ret = true;
}
return ret;
}
public static boolean isEmpty(String s){
return !isNotEmpty(s);
}
}
String的个人记录
最新推荐文章于 2024-08-28 21:15:00 发布