String中一些方法的使用

charcharAt(int index)
          返回指定索引处的 char 值。

例:

    public class Test {
 public static void main(String[] args) {
  String s = "asdasdadasd";
  for (int i = 0; i < s.length(); i++) {
   System.out.print(s.charAt(i) + " ");

  }
 }

}

输出结果:a s d a s d a d a s d

 

 

 

 

intcompareTo(String anotherString)
          按字典顺序比较两个字符串。

public int compareTo(String anotherString)
按字典顺序比较两个字符串。该比较基于字符串中各个字符的 Unicode 值。按字典顺序将此 String 对象表示的字符序列与参数字符串所表示的字符序列进行比较。如果按字典顺序此 String 对象位于参数字符串之前,则比较结果为一个负整数。如果按字典顺序此 String 对象位于参数字符串之后,则比较结果为一个正整数。如果这两个字符串相等,则结果为 0; compareTo 只在方法 equals(Object) 返回 true 时才返回 0

例:public class Test {
 public static void main(String[] args) {
  String s = "a", ss = "A;
  int a = s.compareTo(ss);
  System.out.println(a);

 }
}

输出结果:32

 

 

intcompareToIgnoreCase(String str)
          按字典顺序比较两个字符串,不考虑大小写。

 

例: public class Test {
 public static void main(String[] args) {

  String s = "A", ss = "a";

  System.out.println(s.compareToIgnoreCase(ss));
 }
}

输出结果:0

 

 

Stringconcat(String str)
          将指定字符串连接到此字符串的结尾。

 

例:public class Test {
 public static void main(String[] args) {

  String s = "abc", ss = "cde";

  System.out.println(s.concat(ss));
 }
}

输出结果:abccde

 

 

booleancontains(CharSequence s)
          当且仅当此字符串包含指定的 char 值序列时,返回 true。

 

例:public class Test {
 public static void main(String[] args) {

  String s = "abc";
  String ss = "ab";

  System.out.println(s.contains(ss));
 }
}
输出结果:true

 

 

booleancontentEquals(CharSequence cs)
          将此字符串与指定的 CharSequence 比较。

将此字符串与指定的 CharSequence 比较。当且仅当此 String 与指定序列表示相同的 char 值序列时,结果才为 true

 

例:public class Test {
 public static void main(String[] args) {

  String s = "abc";
  String s1 = "ab";
        String s2="abc";
  System.out.println(s.contentEquals(s1));
  System.out.println(s.contentEquals(s2));
 }
}

输出结果:false
               true

 

 

booleanequalsIgnoreCase(String anotherString)
          将此 String 与另一个 String 比较,不考虑大小写。

public boolean equalsIgnoreCase(String anotherString)
将此 String 与另一个 String 比较,不考虑大小写。如果两个字符串的长度相同,并且其中的相应字符都相等(忽略大小写),则认为这两个字符串是相等的。

例:public class Test {
 public static void main(String[] args) {

  String s = "abc";
  String s1 = "ABC";
        String s2="abc";
  System.out.println(s.equalsIgnoreCase(s1));
  System.out.println(s.equalsIgnoreCase(s2));
 }
}
输出结果:true
               true

 

static StringcopyValueOf(char[] data, int offset, int count)
          返回指定数组中表示该字符序列的 String。

public static String copyValueOf(char[] data,
                                 int offset,
                                 int count)
返回指定数组中表示该字符序列的 String。

 

参数:
data - 字符数组。
offset - 子数组的初始偏移量。
count - 子数组的长度。
返回:
一个 String,它包含字符数组的指定子数组的字符

例:public class Test {
 public static void main(String[] args) {

  char[] a = { 'a', 'b', 'c', 'd' };
  String s = String.copyValueOf(a, 1, 2);
  System.out.println(s);
 }
}

输出结果:bc

 

 

static StringcopyValueOf(char[] data)
          返回指定数组中表示该字符序列的 String。

copyValueOf

public static String copyValueOf(char[] data)
返回指定数组中表示该字符序列的 String。

 

参数:
data - 字符数组。
返回:
一个 String,它包含字符数组的字符。

例:public class Test {
 public static void main(String[] args) {

  char[] a = { 'a', 'b', 'c', 'd' };
  String s = String.copyValueOf(a);
  System.out.println(s);
 }
}

输出结果:abcd

 

 
 booleanequals(Object anObject)
          将此字符串与指定的对象比较。
 booleanequalsIgnoreCase(String anotherString)
          将此 String 与另一个 String 比较,不考虑大小写。

 

 

      int      hashCode()
          返回此字符串的哈希码。       int      indexOf(int ch)
          返回指定字符在此字符串中第一次出现处的索引。       int      indexOf(int ch, int fromIndex)
          返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。       int      indexOf(String str)
          返回指定子字符串在此字符串中第一次出现处的索引。       int      indexOf(String str, int fromIndex)
          返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始

 String[]split(String regex)
          根据给定正则表达式的匹配拆分此字符串。

 

例:public class Test {
 public static void main(String[] args) throws Exception {
  String s = "a,b,c,d,e";
  String[] s1 = s.split(",");
  for (int i = 0; i < s1.length; i++) {
   System.out.print(s1[i] + " ");

  }

 }
}
输出结果:a b c d e

 

 

 Stringsubstring(int beginIndex)
          返回一个新的字符串,它是此字符串的一个子字符串。
      String      substring(int beginIndex, int endIndex)
          返回一个新字符串,它是此字符串的一个子字符串。

substring

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

例:public class Test {
 public static void main(String[] args) throws Exception {
  String s = "abcdefghijk";
  String s1 = s.substring(5);

  System.out.println(s1);

 }
}

输出结果:fghijk

 Stringsubstring(int beginIndex, int endIndex)
          返回一个新字符串,它是此字符串的一个子字符串。

 

 char[]toCharArray()
          将此字符串转换为一个新的字符数组

 

例:public class Test {
 public static void main(String[] args) throws Exception {
  String s = "abcdefghijk";
  char[] a=s.toCharArray();
  for(int i=0;i<a.length;i++){
  System.out.print(a[i]+" ");
  }
 }
}

输出结果:a b c d e f g h i j k

 

 

 StringtoLowerCase()
          使用默认语言环境的规则将此 String 中的所有字符都转换为小写。

 StringtoUpperCase()
          使用默认语言环境的规则将此 String 中的所有字符都转换为大写。

 

 

static StringvalueOf(boolean b)
          返回 boolean 参数的字符串表示形式。

例:public class Test {
 public static void main(String[] args) throws Exception {
  boolean boo = true;

  System.out.println(String.valueOf(boo));
 }
}

输出结果:true

static StringvalueOf(char c)
          返回 char 参数的字符串表示形式。
static StringvalueOf(char[] data)
          返回 char 数组参数的字符串表示形式。
static StringvalueOf(char[] data, int offset, int count)
          返回 char 数组参数的特定子数组的字符串表示形式。
static StringvalueOf(double d)
          返回 double 参数的字符串表示形式。
static StringvalueOf(float f)
          返回 float 参数的字符串表示形式。
static StringvalueOf(int i)
          返回 int 参数的字符串表示形式。
static StringvalueOf(long l)
          返回 long 参数的字符串表示形式。
static StringvalueOf(Object obj)
          返回 Object 参数的字符串表示形式。

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值