`
zxDawn
  • 浏览: 3988 次
社区版块
存档分类
最新评论

String类中常用的方法

    博客分类:
  • Java
阅读更多
String类中有许多方法和构造方法:

一、常见的构造方法:
1、public String()
  String str=new String("abcdefg")

2、public String(byte[] bytes):通过byte数组构造字符串对象
     byte[]b={'a','b','c','d'};
     String t1=new String (b);
     System.out.println(t1);
输出结果:abcd

3、public String(byte[] bytes,int offset, int length):bytes - 要解码为字符的 byte,offset - 要解码的第一个 byte 的索引,length - 要解码的 byte 数
      byte[]b={'a','b','c','d'};
      String t3=new String(b,1,2);
      System.out.println(t3);
  输出结果:bc

4、public String(char[] value):通过char数组构造字符串对象
      char[]c={'0','1','2','3'};
      String t4=new String (c);
      System.out.println(t4);
  输出结果:0123

5、public String(char[] value,int offset,int count):value - 作为字符源的数组,offset - 初始偏移量,count - 长度
      char[]c={'0','1','2','3'};
      String t5=new String(c,1,3);
      System.out.println(t5);
输出结果:123
 
二、常见的方法:

   以字符串对象String str="abcdefg"
             String str1="bcdefg" 为例

1、public char charAt(int index):返回指定索引处的 char 值。索引范围为从 0 到 length() - 1。
 
      char str2=str.charAt(2);
      System.out.println(str2);
输出结果:c

2、public int codePointAt(int index):返回指定索引处的字符(Unicode 代码点)。索引引用 char 值(Unicode 代码单元),其范围从 0 到 length() - 1。

     int t=str.codePointAt(2);
     System.out.println(t);
输出结果:99

3、public int codePointBefore(int index):返回指定索引之前的字符(Unicode 代码点),其范围从 1 到 length。

     int t1=str.codePointBefore(2);    
     System.out.println(t1);
输出结果:98

4、public int codePointCount(int beginIndex,int endIndex):返回此 String 的指定文本范围中的 Unicode 代码点数,文本范围始于指定的 beginIndex到 endIndex - 1 处。

    int t2=str.codePointCount(0,2);
System.out.println(t2);
  输出结果:2

5、public int compareTo(String anotherString):按字典顺序比较两个字符串,该比较基于字符串中各个字符的 Unicode 值。
    
     int t3=str.compareTo(str1);
     System.out.println(t3);
输出结果:-1

6、public int compareToIgnoreCase(String str):按字典顺序比较两个字符串,不考虑大小写。
7、public String concat(String str):将指定字符串连接到此字符串的结尾。如果参数字符串的长度为 0,则返回此 String 对象。

    String str3=str.concat(str1);
    System.out.println(str3);
输出结果:abcdefgbcdefg

8、public boolean contains(CharSequence s):当且仅当此字符串包含指定的 char 值序列时,返回 true,只要有包含,但是是要连续的包含,区分大小写。
       
       boolean str4=str.contains(str1);
       System.out.println(str4);
输出结果:true

9、public boolean contentEquals(CharSequence cs):将此字符串与指定的 CharSequence 比较。当且仅当此 String 与指定序列表示相同的 char 值序列时,结果才为 true。
       
       boolean s=str.contentEquals(str1);
       System.out.println(s);
输出结果:false

10、public boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束,如果参数是空字符串,或者等于此 String 对象,则结果为 true。
      
       boolean t=str.endsWith(str1);
       System.out.println(t);
输出结果:true

11、public boolean equals(Object anObject):将此字符串与指定的对象比较。当且仅当该参数不为 null,并且是与此对象表示相同字符序列的 String 对象时,结果才为 true。
     
      boolean t1= str.equals(str1);
      System.out.println(t1);
输出结果:false

12、boolean equalsIgnoreCase(String anotherString):将此 String 与另一个 String 比较,不考虑大小写。
13、static String format(String format,Object... args):使用指定的格式字符串和参数返回一个格式化字符串。

       String s1=str.format( "%e", 2.22);
       System.out.println(s1);
输出结果:2.220000e+00

14、public byte[] getBytes():默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中,所得 byte 数组。

      byte[] bytes= str.getBytes();
      System.out.println(bytes);
输出结果:[B@1ac3379

15、public void getChars(int srcBegin,int srcEnd,char[] dst,int dstBegin):要复制的第一个字符位于 srcBegin 处;要复制的最后一个字符位于 srcEnd-1 处。要复制到 dst 子数

组中,从 dstBegin 处开始,

     char t[]={'k','l','m','n','o','p'};
      str.getChars(1,3,t,1);
      System.out.println(t);
输出结果:kbcnop

16、public int hashCode():返回此字符串的哈希码。

      int x=str.hashCode();
      System.out.println(x);
输出结果:-1206291356

17、int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引,返回第一次出现该字符的索引。

    int x1=str.indexOf(99);
    System.out.println(x1);
输出结果:2

18、public int indexOf(int ch,int fromIndex):返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。

       int x2=str.indexOf(99, 1);
       System.out.println(x2);
输出结果:2

19、public int indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引。
20、public int indexOf(String str, int fromIndex):返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
21、public boolean isEmpty():当且仅当 length() 为 0 时返回 true。
22、public int lastIndexOf(int ch):返回指定字符在此字符串中最后一次出现处的索引。
23、public int lastIndexOf(int ch,int fromIndex):返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。
24、public int lastIndexOf(String str):返回指定子字符串在此字符串中最右边出现处的索引。
25、public int lastIndexOf(String st,int fromIndex):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
26、public int length():返回此字符串的长度。
27、public String replace(char oldChar,char newChar):返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。
      
      String s3= str.replace(str, str1);
      System.out.println(s3);
输出结果:bcdefg

28、public boolean startsWith(String prefix):测试此字符串是否以指定的前缀开始,如果参数是空字符串,或者等于此 String 对象,则返回 true。
     
       boolean s5=str.startsWith(str1);
       System.out.println(s5);
输出结果:false

29、public boolean startsWith(String prefix,int toffset):测试此字符串从指定索引开始的子字符串是否以指定前缀开始。

       boolean s6=str.startsWith(str1, 1);
       System.out.println(s6);
输出结果:true

30、public String substring(int beginIndex):返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。

       String s7=str.substring(5);
       System.out.println(s7);
输出结果:fg

31、public String substring(int beginIndex,int endIndex):返回一个新字符串,它是此字符串的一个子字符串。该子字符串从 beginIndex 处开始,直到索引 endIndex - 1 处。
32、public String toLowerCase():将此 String 中的所有字符都转换为小写。

    String str="ABCD";
    String s9=str.toLowerCase();
    System.out.println(s9);
输出结果:abcd

33、public String toUpperCase():将此 String 中的所有字符都转换为大写。
34、public String trim():此字符串移除了前导和尾部空白的副本;如果没有前导和尾部空白,则返回此字符串。

    String str=" abc "
    String a2=str.trim();
    System.out.println(a2);
输出结果:abc

 
2
4
分享到:
评论
1 楼 lshhjxlj 2015-10-26  
总结的还不错,支持一下!

相关推荐

    String类的常用方法

    String类的常用方法 String类是Java语言中最基本的类之一,用于处理字符串。字符串是一个字符序列,Java中字符串是不可变的,即创建后不能被修改。下面是String类的常用方法: 1. equals()方法:用于判断两个字符...

    Java中String类中的常用方法.TXT

    简单总结可以下Java中String类中的常用方法

    java string类常用方法

    下面我们将详细介绍Java String类中的常用方法。 charAt方法 charAt方法是String类中的一个基本方法,该方法返回指定位置的代码单元。这个方法的参数是一个整数,表示要返回的代码单元的索引。例如,字符串"hello...

    java中的String类常用方法解析(一)

    本文将深入解析`String`类的一些常用方法,帮助开发者更好地理解和使用这个核心类。 1. **构造方法** - `String()`:创建一个空字符串。 - `String(char[] value)`:根据字符数组创建字符串。 - `String(String ...

    string类的常用方法

    string类的常用方法

    string类常用函数

    string类常用函数 包括各种方法 c++ c语言

    C#中String类常用方法汇总

    以下是对C#中String类常用方法的详细解释和示例: 1. `.ToLower()` 和 `.ToUpper()` 这两个方法用于将字符串转换为小写或大写形式,方便进行大小写的统一处理。 ```csharp string str = "AbC"; string lowerStr...

    Python中 string类的常用方法

    string类的常用方法

    C++中常用的string类方法

    C++中常用的string类方法C++中常用的string类方法C++中常用的string类方法C++中常用的string类方法C++中常用的string类方法C++中常用的string类方法C++中常用的string类方法C++中常用的string类方法C++中常用的...

    string类的常用方法.md

    string类的常用方法

    string类的常用方法 .md

    string类的常用方法

    Java程序设计基础:String类的常用方法(一.pptx

    String类的常用方法(一) 目录 课程导入 掌握获取字符串长度的方法 掌握字符串连接、截取子串的方法 方法定义:public int length() 获取字符串的长度 例如: String message = “Welcome to Java”; System.out....

    string类的常用方法.zip

    这个压缩包"string类的常用方法.zip"包含了一份名为"string类的常用方法.txt"的文档,很显然,它是对`String`类中常见操作的一个介绍。在这篇文章中,我们将深入探讨`String`类的一些重要方法,主要针对C++和Java的`...

    string类的常用方法,附带详细操作步骤.txt

    string类的常用方法

    string类的常用方法(具体操作和步骤).txt

    string类的常用方法

    Java 中 String 类的常用方法

    Java中的String类提供了丰富的字符串操作...以上是Java中String类的一些常用方法,它们是进行字符串操作时的基础工具,每一个方法都根据不同的使用场景来实现相应的功能,从而满足在Java开发中对字符串的各种处理需求。

    string 类及所有的方法(c++)

    string类提供的方法非常丰富,包括但不限于上述介绍的。完整的方法列表可以在C++标准库文档中找到。这些方法使得在C++中处理字符串变得简单且高效,避免了使用原始字符数组时可能出现的内存管理和错误。 总结来说,...

Global site tag (gtag.js) - Google Analytics