码迷,mamicode.com
首页 > 其他好文 > 详细

常用类-String

时间:2017-04-23 19:26:30      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:code   gis   reg   sage   concat   under   with   指定   数组名   

java.lang.String
  常用的构造函数:
    String()
    String(char[] value) : String str = new String({‘j‘,‘a‘,‘v‘,‘a‘});
    String(String original):String str = new String("java");
  字符串中常用的方法:
    1.length():获取字符串的长度
    2.toUpperCase():将字符串转换成大写
    3.toLowerCase():将字符串转换成小写
    4.indexOf():获取指定字符串在字符串中出现的下标位置(第一个字符的位置),如果不存在返回-1
    5.lastIndexOf("z"):获取指定字符串最后一个字符的位置 ,如果不存在返回-1
    6.charAt():获取指定下标的字符(下标从0开始)
    7.substring(begin, end):字符串截取包含起始位置,不包含结束位置
    8.substring(begin):字符串截取,从指定的起始位置截取到最后
    9.contains():判断是否包含指定字符串,包含返回true,否则返回false
    10.equals():比较两个字符串内容是否相等
    11.equalsIgnoreCase():忽略大小写比较两个字符串内容是否相等
    12.startsWith():是否以指定字符串开头
    13.endsWith():是否以指定字符串结尾:

public class StringDemo {
    public static void main(String[] args) {
        String str1=new String("bjsxt");
        String str2=new String("bjsxt");
        System.out.println(str1==str2);//false
        System.out.println(str1.equals(str2));//true
        
        String str3="bjsxt";
        String str4=new String("bjsxt");
        System.out.println(str3==str4);//false
        System.out.println(str3.equals(str4));//true
        
        String str5="bjsxt";
        String str6="bjsxt";
        System.out.println(str5==str6);//ture
        System.out.println(str5.equals(str6));//true
        
        
    }
}
public class StringDemo2 {
    public static void main(String[] args) {
        String str="欢迎来到zzsxt进行学习,zzsxt!";
        System.out.println("获取字符串长度的方法:"+str.length());//获取数组的长度用的是数组名.length
        System.out.println("转大写:"+str.toUpperCase());//将字符串转换成大写
        System.out.println("转小写:"+str.toLowerCase());//将字符串转换成小写
        System.out.println("获取指定下标的字符:"+str.charAt(3));//获取指定下标的字符(下标从0开始)
        System.out.println("获取指定字符在字符串中出现的下标位置(第一个字符的位置):"+str.indexOf("a"));//
        System.out.println("获取指定字符在字符串中出现的下标位置(最后一个字符的位置):"+str.lastIndexOf("z"));//
        System.out.println("截取字符串:"+str.substring(2, 5));//字符串截取包含起始位置,不包含结束位置
        System.out.println("截取字符串:"+str.substring(2));//字符串截取,从指定的起始位置截取到最后
        System.out.println("判断是否包含指定字符串:"+str.contains("z"));
        String str2="zzsxt";
        String str3="ZZsxt";
        System.out.println("比较两个字符串内容是否相等:"+str2.equals(str3));
        System.out.println("忽略大小写比较两个字符串内容是否相等:"+str2.equalsIgnoreCase(str3));
//        System.out.println(str2.toUpperCase().equals(str3.toUpperCase()));//将字符串统一转换为大写或小写在进行比较
        System.out.println("是否以指定字符串开头:"+str.startsWith("zzsxt"));
        System.out.println("是否以指定字符串结尾:"+str.endsWith("zzsxt!"));
        
        
    }
}

 

字符串常用方法2:
  trim():去掉字符串中前后空格
  concat(str):连接字符串 可以使用"+"替代
  String[] split(String regex):按照指定字符串进行分割,分割后子字符保存到数组返回
  replace(char oldChar, char newChar):返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的

public class StringDemo3 {
    public static void main(String[] args) {
        String str=" hello  world ";
        String str2="zzsxt";
        str=str.trim();
        System.out.println(str);
//        System.out.println(str.concat(str2));
        System.out.println(str+str2);
        String str3="窗前明月光,疑是地上霜.举头望明月,低头思故乡";
        String[] strs = str3.split(",");
        for (String string : strs) {
            System.out.println(string);
        }
        str3=str3.replace(‘.‘, ‘,‘);
        System.out.println(str3);
    }
}

一个简单的注册演示

public class Register {
    public static boolean verity(String name,String password,String repassword){
        String message="";
        boolean isVerity,isName,isPassword,isRepassword;
        if(name.length()>3){
            isName=true;
        }else{
            message+="用户名长度大于3位!";//message=message+"用户名长度大于3位!";
            isName=false;
        }
        if(password.length()>6){
            isPassword=true;
        }else{
            message+="密码的长度大于6位!";
            isPassword=false;
        }
        if(password.equals(repassword)){
            isRepassword=true;
        }else{
            message+="两次输入的密码不相同!";
            isRepassword=false;
        }
        isVerity=isName&&isPassword&&isRepassword;
        System.out.println(message);
        return isVerity;
    }
    
    public static void main(String[] args) {
        System.out.println("--------注册系统--------");
        boolean isVerify=false;
        do{
            Scanner input = new Scanner(System.in);
            System.out.println("请输入用户名:");
            String name =input.next();
            System.out.println("请输入密码:");
            String password =input.next();
            System.out.println("请输入重复密码:");
            String repassword =input.next();
            isVerify = verity(name,password,repassword);
        }while(!isVerify);
        System.out.println("注册成功!请牢记你的用户名和密码!");
    }
}

 

String

public class TestString {
    public static void main(String[] args) {
        String str="abc";
        //返回以毫秒为单位的当前时间(获取当前时间距元年的总毫秒数)
        long start = System.currentTimeMillis();
        for(int i=0;i<10000;i++){
            str+=i;
        }
        long end = System.currentTimeMillis();
        System.out.println("总耗时:"+(end-start));
    }
}


String:不可变的字符串
StringBuffer:线程安全的可变字符序列
  构造函数:
    StringBuffer():
    StringBuffer(String str):
  常用方法:
    sb.append(str):将指定str字符串追加到sb后边
    charAt(int index) : 返回此序列中指定索引处的 char 值。
    indexOf(String str):返回第一次出现的指定子字符串在该字符串中的索引。
    lastIndexOf(String str):返回最右边出现的指定子字符串在此字符串中的索引。
    replace(int start, int end, String str):使用给定 String 中的字符替换此序列的子字符串中的字符。
        包含start位置,但不包含end位置-->[start,end)
    reverse():反转

public class TestStringBuffer {
    public static void main(String[] args) {
        StringBuffer sb=new StringBuffer("abc");
        sb.append("edfabc");
        System.out.println(sb.charAt(3));
        System.out.println(sb.indexOf("d"));
        System.out.println(sb.lastIndexOf("a"));
        //sb="abcedfabc"
        System.out.println(sb.replace(2, 4, "hello"));
        System.out.println(sb);
        sb.reverse();//反转
        System.out.println(sb);
        //返回以毫秒为单位的当前时间(获取当前时间距元年的总毫秒数)
        long start = System.currentTimeMillis();
        for(int i=0;i<10000;i++){
            sb.append(i);
        }
        long end = System.currentTimeMillis();
        System.out.println("总耗时:"+(end-start));
        
    }
}

String:不可变的字符串
StringBuffer:线程安全的可变字符序列(synchronized),线程安全(在多线程中使用),效率低
StringBuilder:一个可变的字符序列。此类提供一个与 StringBuffer 兼容的 API,非线程安全,效率高
  交通信号灯:
    同步(存在红绿灯)
    不同步(没有红绿灯)
  构造函数:
    StringBuffer():
    StringBuffer(String str):
  常用方法:
    sb.append(str)***:将指定str字符串追加到sb后边
    charAt(int index) : 返回此序列中指定索引处的 char 值。
    indexOf(String str):返回第一次出现的指定子字符串在该字符串中的索引。
    lastIndexOf(String str):返回最右边出现的指定子字符串在此字符串中的索引。
    replace(int start, int end, String str):使用给定 String 中的字符替换此序列的子字符串中的字符。
        包含start位置,但不包含end位置-->[start,end)
    reverse():反转

public class TestStringBuilder {
    public static void main(String[] args) {
        StringBuilder sb=new StringBuilder("abc");
        sb.append("edfabc");
        System.out.println(sb.charAt(3));
        System.out.println(sb.indexOf("d"));
        System.out.println(sb.lastIndexOf("a"));
        //sb="abcedfabc"
        System.out.println(sb.replace(2, 4, "hello"));
        System.out.println(sb);
        sb.reverse();//反转
        System.out.println(sb);
        //返回以毫秒为单位的当前时间(获取当前时间距元年的总毫秒数)
        long start = System.currentTimeMillis();
        for(int i=0;i<10000;i++){
            sb.append(i);
        }
        long end = System.currentTimeMillis();
        System.out.println("总耗时:"+(end-start));
    }
}

 

常用类-String

标签:code   gis   reg   sage   concat   under   with   指定   数组名   

原文地址:http://www.cnblogs.com/fwdsz/p/6753405.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!