JavaSE(05)(String类)
2015-11-26 12:48
351 查看
****String类总结:** 统计一个字符串中大写小写字母和数字的个数: package zz.itheima.string; import java.util.Scanner; public class CalcNum { //示例:统计一个字符串中大写小写字母和数字的个数 public static void main(String[] args) { System.out.println("请任意输入一个字符串:"); Scanner sc=new Scanner(System.in); String val=sc.nextLine(); int n1=0; int n2=0; int n3=0; //1.先转换为byte类型的数组 byte[] temp=val.getBytes(); //2.循环遍历,判断范围,计数 for (int i = 0; i < temp.length; i++) { byte t=temp[i]; //48-57是数字 65-90是大写字母 97-122是小写字母 if(t>=48&&t<=57) n1++; if(t>=65&&t<=90) n2++; if(t>=97&&t<=122) n3++; } System.out.println("数字有"+n1+",大写字符有"+n2+"小写字符有"+n3); } } 字符串的分割: package zz.itheima.string; import java.util.Scanner; public class TestCaseCast { //字符串的分割 public static void main(String[] args) { //练习:把任意字符串的首字母转成大写,其余为小写 System.out.println("请任意输入一个字符串:"); Scanner sc=new Scanner(System.in); String val=sc.nextLine(); String first=val.substring(0, 1); first=first.toUpperCase(); String last=val.substring(1); last=last.toLowerCase(); System.out.println(first+last); } } 字符串的获取: package zz.itheima.string; import java.util.Scanner; public class TestHuoqu { public static void main(String[] args) { // int length() String s1="hlelhlo"; System.out.println(s1.length());//7 // char charAt(int index) System.out.println(s1.charAt(3));//l // int indexOf(int ch) 和int indexOf(String str); 找不到返回-1 System.out.println(s1.indexOf("hl"));//0 // int indexOf(int ch,int fromIndex)和int indexOf(String str,int fromIndex) System.out.println(s1.indexOf("hl",3));//4,从第三个位置开始首次出现的位置 // int lastIndexOf(String s) System.out.println("last"+s1.lastIndexOf("hl"));//last4 // int lastIndexOf(String s,int fromIndex) System.out.println("last"+s1.lastIndexOf("hl",3));//last0 // String substring(int start) System.out.println(s1.substring(2)); //包含开始下标的字符 // String substring(int start,int end) System.out.println(s1.substring(2, 4)); //不包含结束下标的字符 //示例:判断电子邮件的格式是否正确 zhangsan@qq.com System.out.println("请输入你的邮箱地址:"); Scanner sc=new Scanner(System.in); String email=sc.nextLine(); //必须有@ 和 . @必须在.之前 if(email.indexOf("@")!=-1&&email.indexOf("@")<email.indexOf(".")) System.out.println("正确"); else System.out.println("不正确"); //练习:换行输出字符串中的每一个字符 for (int i = 0; i < email.length(); i++) { char temp=email.charAt(i); System.out.println(temp); } } } 7 l 0 4 last4 last0 elhlo el 请输入你的邮箱地址: lqc@qq.com 正确 字符串的判断: package zz.itheima.string; public class TestJudge { public static void main(String[] args) { String s1 = "hello"; String s2 = "hEllo"; //boolean equals(Object obj) System.out.println(s1.equals(s2));//false //boolean equalsIgnoreCase(String str) System.out.println(s1.equalsIgnoreCase(s2));//true //boolean contains(String str) System.out.println(s1.contains("he"));//true //boolean startsWith(String str) System.out.println(s1.startsWith("h"));//true //boolean endsWith(String str) System.out.println(s1.endsWith("llo"));//true //boolean isEmpty() “” null String s3=null;//没有开辟堆内存 //s3.length(); String s4=""; System.out.println(s4.isEmpty()); System.out.println(s4.length());//0 } } 运行结果: false true true true true true 0 模拟用户登录: package zz.itheima.string; import java.util.Scanner; public class TestLogin { public static void main(String[] args) { //示例:模拟管理员登录(账号和密码),给三次机会,并提示还有几次(假设正确的账号和密码是: admin 0000) Scanner sc=new Scanner(System.in); for(int j=1;j<4;j++) { //1.给出提示信息(账号) System.out.println("请输入账号:"); String account=sc.nextLine(); //2.给出提示信息(密码) System.out.println("请输入密码:"); String pwd=sc.nextLine(); //3.判断 if(account.equalsIgnoreCase("admin")&&pwd.equals("abcd")) { System.out.println("登录成功"); break; } else { System.out.println("登录失败,你还剩"+(3-j)+"次机会"); } } } } String测试类: package zz.itheima.string; public class TestString { //String测试类 public static void main(String[] args) { //1.String类是一个特殊的引用类型,可以像基本数据类型一样使用 int j = 10; String s1 = "hello"; //2。String类的构造方法 //空构造public String() String s2 = new String();//已经开辟了堆内存 System.out.println("s2="+s2); //参数为字节数组 public String(byte[] bytes) byte[] bytes = {97,98,99}; String s3 = new String(bytes); System.out.println("s3="+s3); //参数为字符数组 public String(char[] values) char[] chars = {'a','b','c'}; String s4 = new String (chars); System.out.println("s4="+s4); //String对象的不可变性,也就是一旦初始化后值不可以改变 String s5 = new String("hello"); s5 += ",world";//重新指向了一个新的地址,原来的地址(存放hello)还存在,只是没有引用了,只能被垃圾回收掉了。 System.out.println("s5="+s5); System.out.println("**********************"); System.out.println("abc"=="ab"+"c"); String s6 = "abc"; String s7 = "ab"; String s8 = "c"; System.out.println(s6==s7+s8); } } 运行结果: s2= s3=abc s4=abc s5=hello,world ********************** true false 字符串的转换: package zz.itheima.string; public class TestZhuanhuan { public static void main(String[] args) { //字符串转换测试 // byte[] getBytes() String s1="hello"; byte[] b=s1.getBytes(); for (int i = 0; i < b.length; i++) { System.out.println(b[i]); } // char[] toCharArray() char[] c=s1.toCharArray(); for (int i = 0; i < c.length; i++) { System.out.println(c[i]); } // static String valueOf(char[] chs)把字符数组转化为字符串 String s2=String.valueOf(c); System.out.println(s2); // static String valueOf(基本数据类型 i) 类似于“+”,但是要比“+”效率要高,把基本数据类型转化为字符串 System.out.println(String.valueOf(10.0)); //+作用是连接字符串 System.out.println(s2+"asdf"); //+还能起到类型转换的作用,其他类型会转换为字符串的 int n=10; System.out.println(n+""); // String toLowerCase() String s3="heLLo"; System.out.println(s3.toLowerCase()); // String toUpperCase() System.out.println(s3.toUpperCase()); // String concat(String str) 类似于 “+” s3=s3.concat(",world"); System.out.println(s3); } } 运行结果: 104 101 108 108 111 h e l l o hello 10.0 helloasdf 10 hello HELLO heLLo,world 其他的一些常用方法: package zz.itheima.string; public class TestOther { public static void main(String[] args) { // 替换功能: // String replace(char old,char new) String s1="abcdecfg"; s1=s1.replace('c', 'x'); System.out.println(s1); // String replace(String old,String new) s1="abcdecfg"; s1=s1.replace("de", "xyz"); System.out.println(s1); // 分割 String[] split(String regex) String s2="abc-defg-hijk"; String[] ss=s2.split("-"); for (int i = 0; i < ss.length; i++) { System.out.println(ss[i]); } String s3="-abc-defg"; String[] st=s3.split("-"); System.out.println(st.length); // 去除字符串两端的空格 String trim() String s4=" hello world "; System.out.println(s4.trim()); // 练习1:把字符串中的“o”替换成“0”,“l”替换成“1” String s5="hellolosdflool"; s5=s5.replace("o", "0").replace("l", "1"); System.out.println(s5); // 练习2:给一个字符串"5 , 4 , 3 ",然后分别换行输出 String s6="1,2,3,4,5"; String[] temp=s6.split(","); for (int i = 0; i < temp.length; i++) { System.out.println(temp[i]); } } } 运行结果: abxdexfg abcxyzcfg abc defg hijk 3 hello world he11010sdf1001 1 2 3 4 5
相关文章推荐
- SSH:Struts2框架(总结)
- java设计模式(三)--单例模式
- struts表单数据获取方式
- Java日期处理
- spring+hibernate+c3p0整合链接池配置-连接无法释放问题(已经解决)
- 在Java中,如何跳出当前的多重嵌套循环?
- [Java] 简单文件数据库实现
- java从文本文件中一次读入一行数据进行处理
- SSH:EL表达式讲解
- Java中数组声明和初始化
- SPRING官方网下载地址
- Java通过JNI调用DLL动态库
- 掷骰子游戏窗体实现--Java初级小项目
- java27
- java26
- 把应用服务写成系统服务
- java25
- java24
- java23
- 详解Java的Spring框架中的注解的用法