String

 

public class StringTest1 {

    public static void main(String[] args) {

       String str = new String("My name is ");

       String name = " Wit";

       str += "Edgar";

       str = str + name;

       System.out.println(str);

       System.out.println(str.length());

       System.out.println(str.equals("my name is edgar" +

              " wit"));

       System.out.println(str.equalsIgnoreCase("my name " +

              "is edgar wit"));

       System.out.println(str.toUpperCase());

       System.out.println(str.toLowerCase());

       System.out.println(str.toString());

    }

}

输出结果:

My name is Edgar Wit

20

false

true

MY NAME IS EDGAR WIT

my name is edgar wit

My name is Edgar Wit

JDK文档:

int

length()
          
返回此字符串的长度。

 String

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

 String

toLowerCase(Locale locale)
          
使用给定 Locale 的规则将此 String 中的所有字符都转换为小写。

 String

toString()
          
返回此对象本身(它已经是一个字符串!)。

 String

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

 String

toUpperCase(Locale locale)
          
使用给定的 Locale 规则将此 String 中的所有字符都转换为大写

boolean

equals(Object anObject)
          
比较此字符串与指定的对象。

 boolean

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

boolean

isEmpty()
          
当且仅当 length() 0 时返回 true

 

codePointCount()charAt()codePointAt()codePointBefore()方法

public class StringTest2 {

    public static void main(String[] args) {

       String str = "Edgar";

       int m = str.length();

       int n = str.codePointCount(0, str.length());

       System.out.println(m + " " + n);

      

       char ch1 = str.charAt(2);

       char ch2 = str.charAt(4);

       System.out.println(ch1 + " " + ch2);

      

       int i = str.codePointAt(3);

       System.out.println(i);

      

       int j = str.codePointBefore(4);

       System.out.println(j);

    }

}

输出结果:

5 5

g r

97

97

JDK文档:

 char

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

 int

codePointAt(int index)
          
返回指定索引处的字符(Unicode 代码点)。

 int

codePointBefore(int index)
          
返回指定索引之前的字符(Unicode 代码点)。

 int

codePointCount(int beginIndex, int endIndex)
          
返回此 String 的指定文本范围中的 Unicode 代码点数。

compareTo()compareToIgnoreCace()方法

public class StringTest3 {

    public static void main(String[] args) {

       String str1 = "Edgar";

       String str2 = "Edgar";

       String str3 = "edgar";

      

       System.out.println(str1.compareTo(str2));

       System.out.println(str1.compareTo(str3));

      

       System.out.println(str1.compareToIgnoreCase(str3));

    }

}

输出结果:

0

-32

0

JDK文档:

int

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

 int

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

 

concat()contains()copyValeOf()方法

public class StringTest4 {

    public static void main(String[] args) {

       String str1 = "abcdefg";

       System.out.println(str1);

      

       str1 = str1.concat("abcd");

       System.out.println(str1);

      

       boolean bool1 = str1.contains("abcd");

       boolean bool2 = str1.contains("hijk");

       System.out.println(bool1 + " " + bool2);

      

       char[] ch = {'a','b','c'};

       System.out.println(str1.copyValueOf(ch));

    }

}

输出结果:

abcdefg

abcdefgabcd

true false

abc

JDK文档:

String

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

 boolean

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

static String

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

static String

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

 

public class StringTest5 {

    public static void main(String[] args) {

       String str1 = "abcabcdeabcdefg";

      

       System.out.println(str1.indexOf('a'));

       System.out.println(str1.indexOf('a', 2));

      

       System.out.println(str1.indexOf("bc"));

       System.out.println(str1.indexOf("bc", 6));

      

       System.out.println(str1.lastIndexOf('a'));

       System.out.println(str1.lastIndexOf('a', 6));

      

       System.out.println(str1.lastIndexOf("bc"));

       System.out.println(str1.lastIndexOf("bc", 4));

    }

}

 

 int

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

 int

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

 int

indexOf(String str)
          
返回指定子字符串在此字符串中第一次出现处的索引。

 int

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

 int

lastIndexOf(int ch)
          
返回指定字符在此字符串中最后一次出现处的索引。

 int

lastIndexOf(int ch, int fromIndex)
          
返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。

 int

lastIndexOf(String str)
          
返回指定子字符串在此字符串中最右边出现处的索引。

 int

lastIndexOf(String str, int fromIndex)
          
返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。

 

public class StringTest6 {

    public static void main(String[] args) {

       String str1 = "Edgar is a student";

       str1 = str1.replace('a', 'A');

      

       System.out.println(str1);

       System.out.println(str1.startsWith("E"));

       System.out.println(str1.startsWith("E", 1));

       System.out.println(str1.endsWith("ent"));

      

       String str2 = str1.substring(3);

       String str3 = str1.substring(5, 11);

       System.out.println(str2);

       System.out.println(str3);  

      

       String str4= "  Edgar   ";

       System.out.println(str4);

       str4 = str4.trim();

       System.out.println(str4);

    }

}

输出结果:

EdgAr is A student

true

false

true

Ar is A student

 is A

  Edgar  

Edgar

 

 

JDK文档:

 boolean

endsWith(String suffix)
          
测试此字符串是否以指定的后缀结束。

 boolean

startsWith(String prefix)
          
测试此字符串是否以指定的前缀开始。

 boolean

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

 String

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

 String

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

 String

trim()
          
返回字符串的副本,忽略前导空白和尾部空白。

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值