package com.lovo.homeWork;public class TestString { public static void main(String[] args) { String asd = "asdfjvjadsffvaadfkfasaffdsasdffadsafafsafdadsfaafd"; asd.trim(); char [] array = asd.toCharArray(); int index = 0; for (int i = 0; i < array.length-2; i++) { if (array[i] == 'a' & array[i+1] == 'f'); index++; } System.out.println(index);
// 2、输入任意一个字符串,如:“abDEe23dJfd343dPOddfe4CdD5ccv!23rr”。取出该字符串中所有的字母。顺序不能改变!// 并把大写字母变成小写,小写字母变成大写!
package com.lovo.homeWork;public class TestString { public static void main(String[] args) { String str = "abDEe23dJfd343dPOddfe4CdD5ccv!23rr"; str.trim(); char [] array = str.toCharArray(); for (int i = 0; i < array.length; i++) { if (array[i] >= 'A'&& array[i] <= 'Z') { array[i] += 32; } else if (array[i] >='a'&& array[i] <='z') { array[i] = (char) (array[i] - 32); } System.out.print(array[i]); }
// 3、写一个程序可以对两个字符串进行测试,得知第一个字符串是否包含在第二个字符串中。// 如字符串”PEN”包含在字符串“INDEPENDENT”中。
package com.lovo.homeWork;public class TestString { public static void main(String[] args) { String str2 = "wfw"; String str1 = "adafafawawfaffawfwa"; System.out.println(str1.contains(str2));
// 4、编写程序,统计出字符串“want you to know one thing”中字母n和字母o的出现次数。
package com.lovo.homeWork;public class TestString { public static void main(String[] args) { String str = "want you to know one thing"; str.trim(); char [] array=str.toCharArray(); int index = 0; int temp = 0; for (int i = 0; i < array.length; i++) { if (array[i] == 'n') { index++; } else if (array[i] == 'o') { temp++; } } System.out.println("n出现"+index+"次,o出现"+temp+"次");
package com.lovo.homeWork;public class TestString { public static void main(String[] args) { String str = "LOVOT90班20111208"; str.trim(); System.out.println(str.substring(8));
// 2)将“LOVO JAVA”字符串中的“JAVA”替换为“J2EE”。
package com.lovo.homeWork;public class TestString { public static void main(String[] args) { String str = "LOVO JAVA"; str.trim(); System.out.println(str.replace("JAVA", "J2EE"));
// 3)取出“LOVOT96班20120611”第8个字符。
package com.lovo.homeWork;public class TestString { public static void main(String[] args) { String str = "LOVOT96班20120611"; str.trim(); System.out.println(str.charAt(8));
// 4)清除“LOVOT96班20120611”中所有的0。
package com.lovo.homeWork;public class TestString { public static void main(String[] args) { String str = "LOVOT96班20120611"; str.trim(); String str1 =str.replace('0', ' '); String [] array = str1.split(" "); System.out.println(array[0] + array[1] + array[2]);
// 5)清除“LOVOT96班20120611 LOVO 老师”中所有的空格。
package com.lovo.homeWork;public class TestString { public static void main(String[] args) { String str = "LOVOT96班20120611 LOVO 老师"; str.trim(); String [] array = str.split(" "); System.out.println(array[0]+array[1]+array[2]);
// 6)从任意给定的身份证号码中提取此人的出生日期
package com.lovo.homeWork;public class TestString { public static void main(String[] args) { String id = "511321199209067274"; System.out.println("生日号码是:"+id.substring(6, 14));
// 6、查看API DOC文档。自己查找到String提供的方法完成以下功能:
// 1>、将"Hello"和"World"两个字串连接到一起。
package com.lovo.homeWork;public class TestString { public static void main(String[] args) { String str = "Hello"; System.out.println(str.concat("World"));
// 2>、将"I saw a saw saw a saw !"将里面所有的‘s’替换成‘S’
package com.lovo.homeWork;public class TestString { public static void main(String[] args) { String str = "I saw a saw saw a saw !"; str.trim(); System.out.println(str.replace('s', 'S'));
// 3>、解析// "http://localhost:8080/login.jsp?username=zhang3&password=1234567",// 将得到的用户名和密码的值放到一个String数组中。
package com.lovo.homeWork;public class TestString { public static void main(String[] args) { String str = "http://localhost:8080/login.jsp?username=zhang3& password=1234567"; str.trim(); String [] str1 = str.split("[?]"); String [] str2 = str1[1].split("[&]"); String [] str3 = str2[0].split("[=]"); String [] str4 = str2[1].split("[=]"); System.out.println("账号是:"+str3[1]+"密码是:"+str4[1]); }}