JAVA(String 类)

boolean isEmpty()

判断字符串是否为空

public boolean isEmpty(){
        return value.length==0;
    }

char charAt(int index)

返回index处的字符

 public char charAt(int index){
        if(index<0||index>=value.length){
            throw new StringIndexOutOfBoundsException();
        }
        return value[index];
    }
boolean equals(Object anObject)

比较字符串的内容。

    0.先判空
    1.自己先跟自己比
    2.判断传入的数据是否是字符串
    3.判断长度是否一样
    4.逐一元素进行比较
     public boolean equals(MyString another){
        if(another==null){
            return false;
        }
        if(this==another){
            return true;
        }
        int len1=this.value.length;
        int len2=another.value.length;
        if(len1==len2){
            char[] c1=value;
            char[] c2=another.value;
            int i=0;
            while(i<len1){
                if(c1[i]!=c2[i]){
                    return false;
                }
                i++;
            }
            return true;
        }else{
            return false;
        }
    }
    
boolean equalsIgnoreCase(Object anObject)

忽略大小写比较。

public boolean equalsIgnoreCase(MyString another){
        if(another==null){
            return false;
        }
        if(this==another){
            return true;
        }
        int len1=this.value.length;
        int len2=another.value.length;
        if(len1==len2){
            char[] c1=value;
            char[] c2=another.value;
            int i=0;
            while(i<len1){
                char ch1=c1[i];
                char ch2=c2[i];
                if(Character.isLetter(ch1)&&Character.isLetter(ch2)){
                    ch1=ch1>=97?(char)(ch1-32):ch1;
                    ch2=ch2>=97?(char)(ch2-32):ch2;
                }
                if(ch1!=ch2){
                    return false;
                }
                i++;
            }
            return true;
        }else{
            return false;
        }
    }
int compareTo(String anotherString)

按字典顺序比较两个字符串,从第一个开始的差值不相等时输出该差值。

 public int compareTo(MyString another){
        if(another==null){
            return -1;
        }
        if(this==another){
            return 0;
        }
        char[] c1=value;
        char[] c2=another.value;
        int len1=this.value.length;
        int len2=another.value.length;
        int limi=Math.min(len1,len2);
        int i=0;
        while(i<limi){
            if(c1[i]!=c2[i]){
                return c1[i]-c2[i];
            }
            i++;
        }
        return len1-len2;
    }
boolean startsWith(String prefix)

从开头对比是否一样。是否以prefix开头

public boolean startsWith(MyString prefix){
        if(prefix==null){
            return false;
        }
        if(prefix==this){
            return true;
        }
        int len1=value.length;
        int len2=prefix.value.length;
        if(len2>len1){
            return false;
        }
        char[] c1=value;
        char[] c2=prefix.value;
        for(int i=0;i<len2;i++){
            if(c1[i]!=c2[i]){
                return false;
            }
        }
        return true;
    }
boolean contains(CharSequence s)

是否包含s字符串

public boolean contains(MyString s){
        //"abcdefg" 
        //"def"
        if(s==null){
            return false;
        }
        if(s==this){
            return true;
        }
        for(int i=0;i<value.length-s.length()+1;i++){
            MyString sub=substring(i,i+s.length());
            if(sub.equals(s)){
                return true;
            }
        }
        return false;
    }
    
String substring(int beginIndex, int endIndex)

切取字符串

 public MyString substring(int beginIndex, int endIndex){
        if(beginIndex>=endIndex){
            return null;
        }
        if(beginIndex<0||endIndex>value.length){
            return null;
        }
        char[] c=new char[endIndex-beginIndex];
        int index=0;
        for(int i=beginIndex;i<endIndex;i++){
            c[index++]=value[i];
        }
        return new MyString(c);
    }
String concat(String str)

将str连接在该字符之后

 public MyString concat(MyString str){
        if(str==null){
            return this;
        }
String replace(char oldChar, char newChar)

替换

class Test{
    public static void main(String args[]) {
        String str1=new String("www.xuexi.com");
        String str2=new String("z");
        
        System.out.print("返回值 :" );
        System.out.println(str1.replace("w",str2)); 
    }
}


String toLowerCase()

全部装换成小写

class Demo{
    public static void main(String args[]) {
        String str1=new String("WWW.xuExI.com");
        
        System.out.print("返回值 :" );
        System.out.println(str1.toLowerCase()); 
    }
}


String toUpperCase()

全部装换成大写

class Demo{
    public static void main(String args[]) {
        String str1=new String("WWW.xuExI.com");
        
        System.out.print("返回值 :" );
        System.out.println(str1.toUpperCase()); 
    }
}


String trim()

去除两边的空格

class Test{
    public static void main(String args[]) {
        String str1=new String("      www.xuexi.com  ");
        
        System.out.print("返回值 :" );
        System.out.println(str1.trim()); 
    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值