String函数的使用
未完待续。。
/** *String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" ) *都作为此类的实例实现。 字符串是常量;它们的值在创建之后不能更改。 *字符串缓冲区支持可变的字符串。因为 String 对象是不可变的, *所以可以共享。 String字符串中 “==” 表示的是比较字符串的地址,如果存储地址相同 则返回true String字符串中 equals() 表示的是比较字符串的内容,如果存储内容(严格区分 大小写)相同则返回true QQ登陆是验证码输入区分大小写?java中给我们提供了一个忽略字母大小写 的方法equalsIgnoreCase().比较字符串的内容(忽略大小写) */ public static void main(String[] args) { String s = "Hello"; String ss = new String(s); String sss = "hello"; if (s == ss) { System.out.println("地址相同"); }else{ System.out.println("地址不相同"); } System.out.println("************************"); if (s == sss) { System.out.println("地址相同"); }else{ System.out.println("地址不相同"); } System.out.println("************************"); if (s.equalsIgnoreCase(sss)) { System.out.println("内容相同"); }else{ System.out.println("内容不相同"); } }
/** *String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" ) *都作为此类的实例实现。 字符串是常量;它们的值在创建之后不能更改。 *字符串缓冲区支持可变的字符串。因为 String 对象是不可变的, *所以可以共享。 toLowerCase():转小写 返回值String toUpperCase():转大写 返回值String trim():去掉字符串左右两边的空格 返回值String length():获得字符串的长度 返回值int indexOf():获取对应子字符串的索引位置,索引从0开始 返回值int lastIndexOf():获取对应子字符串最后一次出现的索引位置,索引从0开始 返回值int charAt():根据索引来获取对应索引位置上的字符,返回值为char 说明:charAt方法和indexOf方法是一对互逆的方法 substring(int beginIndex):截取子字符串 返回值String substring(int beginIndex,int endIndex): toCharArray():将字符串转变成字符数组 split():字符串的分割,返回的是字符串数组 String.valueOf():将对应的数据转换位字符串 */ public static void main(String[] args) { String s = "Hello,I'm Chinese:)"; int len = s.length(); //区别于数组的length属性,包括集合里的size() System.out.println("len="+len); //最后一个字符的索引为当前长度-1 char w = s.charAt(0); System.out.println(w); int index = s.indexOf("H"); System.out.println(w+","+index); int lastIndex = s.lastIndexOf("e"); System.out.println("lastIndex="+lastIndex); }
package ex; public class StringExample { public static void main(String[] args) { // TODO Auto-generated method stub String s1=new String("you are a student"), s2=new String("how are you"); if(s2.equals(s1)) // 使用equals方法判断s1与s2是否相同 { System.out.println("s1与s2相同"); } else { System.out.println("s1与s2不相同"); } String s3=new String("22030219851022024"); if(s3.startsWith("220302")) //判断s3的前缀是否是“220302”。 { System.out.println("吉林省的身份证"); } String s4=new String("你"), s5=new String("我"); if(s4.compareTo(s5)>0)//按着字典序s4大于s5的表达式。 { System.out.println("按字典序s4大于s5"); } else { System.out.println("按字典序s4小于s5"); } int position=0; String path="c:\\java\\jsp\\A.java"; position=path.lastIndexOf("\\"); //获取path中最后出现目录分隔符号的位置" System.out.println("c:\\java\\jsp\\A.java中最后出现\\的位置:"+position); String fileName=path.substring(position);//获取path中“A.java”子字符串。 System.out.println("c:\\java\\jsp\\A.java中含有的文件名:"+fileName); String s6=new String("100"), s7=new String("123.678"); int n1=Integer.parseInt(s6); //将s6转化成int型数据。 double n2=Double.parseDouble(s7); //将s7转化成double型数据。 double m=n1+n2; System.out.println(m); String s8=String.valueOf(m); //String调用valuOf(int n)方法将m转化为字符串对象 position=s8.indexOf("."); String temp=s8.substring(position+1); System.out.println("数字"+m+"有"+temp.length()+"位小数") ; char a[]=s8.toCharArray(); //将s8存放到数组a中。 for(int i=a.length-1;i>=0;i--) { System.out.print(" "+a[i]); } } }

浙公网安备 33010602011771号