Java核心编程

                                       1 常用类

 1.1 string和stringbuffer

                                                  string类常用方法

.length(长度)

     Java中的string类在Java.lang包中

    String str = new String();

    str.length()之所以要使用一个方法还不是通过属性来获取长度,是为了防止

    str.length=4;之类的情况发生,这样就可以实现了面向对象封装的特性。

   另外,str.length()有一个特点  就是不论是中文还是英文,都是按照1个长度来看待的

  而不是根据所占字节来计算length长度

eg.String a="Java程序设计";a.length()=8;

 

indexOf (索引)

Java中字符串的查找共有四种方法,如下:

1:int indexof(String str) :返回第一次出现的指定子字符串在此字符串中的索引

2:intindexof(String str ,int startIndex);从指定的索引处开始,返回第一次出现的指定字符串

在此字符串中的索引

3:int lastIndexof(String str):返回在此字符串中最右边出现的指定子字符串的索引

4:int lastIndexof(String str,int startIndex);从指定的索引出开始向后搜索,返回在此字符串中

最后一次出现的指定子字符串的索引

.charAt

此方法返回位于字符串中的指定索引处的字符,该字符串的索引从零开始

此方法定义的语法如下:

punlic char chatAt(int index)

参数

这里是参数的细节

   index --返回字符的索引

返回值

  该方法的返回指定索引处char值

equals、      (等于)

String str4 = new String("abc");
String str5 = new String("abc");
System.out.println(str4 == str5);//判断的是引用

System.out.println(str4.equals(str5));//判断的是内容

String str1 = new String ("A");

String str2 = new String ("B");

String str3 = new String ("B");

boolean
result1 = str1.equals(str2);

boolean
result2 = str2.equals(str3);
System.out.println(result1);
System.out.println(result2);

String str6 = new String("B");
String str7 = new String("B");
String str8 = str7;
System.out.println(str8 == str7);

结果是:

最后的str8 ==7为turn的原因是他两个指向的是同一个String对象

再说明一点,String str = new String("abc")String str ="abc"是有一点小区别的,对于new出来的String对象,是每new一个内存里生成一个,也就是说其允许存在内容相同的重复对象。而String str ="abc"这种形式是不允许存在内容相同的重复对象,只要内存已经存在了,就不再新生成,而是把新的引用指向原来的对象

replace、(代替)

replace() 方法通过用 newChar 字符替换字符串中出现的所有 oldChar 字符,并返回替换后的新字符串。

语法

public String replace(char oldChar,
                      char newChar)

参数

  • oldChar -- 原字符。
  • newChar -- 新字符。

返回值

替换后生成的新字符串。

实例

public class Test {
    public static void main(String args[]) {
        String Str = new String("hello");
 
        System.out.print("返回值 :" );
        System.out.println(Str.replace('o', 'T'));
 
        System.out.print("返回值 :" );
        System.out.println(Str.replace('l', 'D'));
    }
}

以上程序执行结果为:

返回值 :hellT
返回值 :heDDo

 

 

split、(分裂)

把相应字符串按照格式分隔开来

split 方法:将一个字符串分割为子字符串,然后将结果作为字符串数组返回。

stringObj.split([separator],[limit])
参数:stringObj   必选项。要被分解的 String 对象或文字。该对象不会被 split 方法修改。
separator 可选项。字符串或 正则表达式 对象,它标识了分隔字符串时使用的是一个还是多个字符。如果忽

略该选项,返回包含整个字符串的单一元素数组。 limit可选项。该值用来限制返回数组中的元素个数。

说明:split 方法的结果是一个字符串数组,在 stingObj 中每个出现 separator 的位置都要进行分解。separator 不作为任何数组元素的部分返回。

split 的实现直接调用的 matcher 类的 split 的方法。“ . ”在正则表达式中有特殊的含义,因此我们使用的时候必须进行转义"\\."

如果用竖线“|”分隔的话,将出现不可得到的结果,必须改为“\\|”  

,+ * 不是有效的模式匹配规则表达式,用"//*"        "//+"转义后即可得到正确的结果。

 

substring、(子串,子链)

https://i-blog.csdnimg.cn/blog_migrate/b2fd02e1be919d653568af4efac81e6e.jpeg

可是实现截取字符串

 

trim、(修剪)

最后总结一下: 
String.Trim()方法会去除字符串两端,不仅仅是空格字符,它总共能去除25种字符: 
('/t', '/n', '/v', '/f', '/r', ' ', '/x0085', '/x00a0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '?', '/u2028', '/u2029', ' ', '?')


如果你想保留其中的一个或多个(例如/t制表符,/n换行符,/r回车符等),请慎用Trim方法。

请注意,Trim删除的过程为从外到内,直到碰到一个非空白的字符为止,所以不管前后有多少个连续的空白字符都会被删除掉。

 最后附上两个相关的方法(也是String类直接提供的),分别去除字符串头部空白字符的TrimStart方法和去除字符串尾部空白字符的 TrimEnd方法:

TrimStartTrimEnd方法

如果想去除字符串两端其他任意字符,可以考虑Trim他的重载兄弟:String.Trim(Char[]),传入你想要去除的哪些字符的数组。

源码奉上:

public string Trim(params char[] trimChars)

{

    if ((trimChars == null) || (trimChars.Length == 0))

    {

        trimChars = WhitespaceChars;

    }

    return this.TrimHelper(trimChars, 2);

}

空格 != 空白字符,删除空格请使用: Trim(‘ ‘);

 

format。(格式)

按格式输出

StringBuffer类常用方法:(伸缩缓冲器)

append、(追加)

    public static void main(String[] args) {

        StringBuffer buf = new StringBuffer("Hard");
       
String astring="waxworks";
       
System.out.println(buf.append(astring,3,8));//buf加上astring中的字符串下标3到下标8的字符


       
StringBuffer buf1 = new StringBuffer("Hard");
        char
[] text = {'W','a','x','w','o','r','k','s'};
       
System.out.println(buf1.append(text,3,5));//buf1加上从数组的下标为3的地方开始加上下标3本身往后查五个

   
}
}

 

insert、(插入)

public static void main(String[] args) {
       StringBuffer str = new StringBuffer("qwer");

       
System.out.println(str.insert(1,"ang"));//先选中在字符串中的第几位,在插入所需要插入的内容
   
}
}

 

 

delete、(删除)

deleteCharAt、(删除程序)

      StringBuffer buf = new StringBuffer("123456789123");
        
//delete删除程序
       
//删除字符串中第五个的后一个到第九个字符
        
buf.delete(5,9);
       
System.out.println(buf);



       
buf = new StringBuffer("abcd");
       
System.out.println(buf);

       
//删除第二个的后一个
       
buf.deleteCharAt(2);
       
System.out.println(buf);
   
}
}

 

replace、(代替)

    public static void main(String[] args) {//代替
       
//将第三个的下一个到第八个删除,并替换成haha
    
StringBuffer str = new StringBuffer("qwertyuio");
       
System.out.println(str.replace(3,8,"haha"));

   
}
}

 

setCharAt、

    public static void main(String[] args) {
         //这个方法主要用来替换
       
StringBuffer str = new StringBuffer("LOVE");
        
str.setCharAt(0,'a');
       
System.out.println(str);
   
}
}

 

 

reverse。(颠倒)

   public static void main(String[] args) {
        StringBuffer str = new StringBuffer("abc");
       
StringBuffer str1 = new StringBuffer();
       
str1 = str.reverse();
       
System.out.println(str1);
       
   
}
}

 

 

String 是不可更改的,如需改字符串内容用StringButter

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值