qishi821的个人博客分享 http://blog.sciencenet.cn/u/qishi821

博文

Java(一)

已有 1699 次阅读 2016-9-26 17:03 |个人分类:Java|系统分类:科研笔记| Java相关知识

字符串String

1 字符串的长度

public int length();//此方法可以获得当前字符串对象中字符的个数。e.g: String s = Happy!;

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

2 判断字符串的前缀和后缀

 (1)public boolean startsWith(String prifix);

 (2)public boolean endWith(String suffix);

 这两个方法可以分别判断当前字符串的前缀和后缀是否是指定的子字符串。如果是就返回true,否则返回false.

e.g: String phonNon = 01084572918;

  If(phonNon.StartsWith(010))

{

System.out.println(北京);

}

3 字符串中单个字符的索引

  (1)public int indexOf(int ch);//返回指定字符串中第一次出现处的索引

   参数:ch - 一个字符

    e.g.

     String s = Hello World!;

     System.out.println(s.indexOf(e));

     程序运行结果:

      1

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

4 字符串中子字符串的索引

 (1)public int indexOf(String str);//返回指定子字符串中第一次出现处的索引

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

 (3)public int lastIndexOf(String str);//如果字符串参数作为一个子字符串在此对象中出现一次或多次,则返回最后一个字符串的第一个字符的索引位置。如果它不作为一个子字符串出现,则返回-1.

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

5 字符串大小写相互转换

 (1)public String toLowerCase();//使用默认语言环境的规则将此String中的所有字符都转换成小写。返回字符全是小写的字符串。

 (2)public String toUpperCase();//使用默认语言环境的规则将此String中的所有字符都转换为大写。返回字符全是大写的字符串。

6 字符串比较

 (1)public int compareTo(String anotherString);//按字典顺序比较两个字符串。若按字典顺序将此String对象位于参数字符串之前,则比较结果为一个负数。如果按字典顺序此String对象位于参数字符串之后,则比较结果为一个正整数。如果这两个字符串相等,则结果为0

 (2)public boolean equals(Object anObject);//将此字符串与指定的对象比较。当且仅当该参数不为null,并且是与此对象表示相同字符序列的String对象时,结果才为true,否则返回false

 (3)public boolean equalsIgnoreCase(String anotherString);//此方法和第二个方法相似,只是在比较的过程中,忽略了字符串中字符的大小写。

 e.g.

 String s1 = hello world;

 String s2 = Hello World;

 System.out.println(s1.equals(s2));

 System.out.println(s1.equalsIgnoreCase(s2));

 程序运行结果如下:

 false

 True

7 字符串连接

 public String concat(String str);//这个方法将参数字符串连接到当前字符串的尾部,并返回这个连接而成的长字符串,但是当前的字符串本身并没有改变。

 e.g.

  String s1  = Hello;

  String s2  = World;

  System.out.println(s1.concat(s2));

  System.out.println(s1);

  运行结果是:

  Hello World

  Hello

8 截取子字符串

 (1)public String substring(int beginIndex);//此方法返回一个当前字符串的子字符串,该子字符串从指定的索引处的字符开始,直到此字符串的末尾。

  e.g: String s = unhappy;

     System.out.println(s.substring(2));

程序运行结果:

Happy

 注:如果beginIndex为负或大于此String对象的长度,则抛出异常:IndexOutOfBoundsException.

 (2)public String substring(int beginIndex, int endIndex);//此方法返回字符串的子字符串,该字符串从指定的beginIndex处开始,直到索引endIndex-1处的字符,因此,该字符串的长度为endIndex-beginIndex

 e.g.String s = unhappy;

    System.out.println(s.substring(2,5));

   程序运行结果是:

    hap

9 字符串的其他操作

 (1)public static String valueOf(Object obj);//此函数将其他类型的数据转换成字符串,obj可以是整型、字符型、浮点型等类型的数据。

e.g.

int d = 1234;

String s = String.valueOf(d);

System.out.println(s);

程序运行结果如下:

  1234

 (2)public String trim();//此方法将返回字符串移除了前导和尾部的字符串;如果没有前导和尾部空白字符串,则将返回此字符串。

 (3)public String replace(char oldChar, char newChar);//此函数返回通过调用newChar替换此字符串中出现的所有oldChar得到的字符串。如果oldChar在此String对象表示的字符序列中没有出现,则返回对此对象的引用。否则,创建一个新的String对象,它所表示的字符串序列除了所有的oldChar都被替换成newChar之外,与此String对象表示的字符序列相同。

    e.g.

      String s = Heloooo!;

      System.out.println(s.replace(o,p));

      程序运行结果是:

      Hellpppp!

 (4)字符串的加法+

   字符串是经常使用的数据类型,为了编程方便,Java编译系统引入了字符串烦人的加法和赋值。

e.g.

 String Mystr = hello;

 Mystr = Mystr + world!;

 System.out.println(Mystr);

  程序运行结果:

  hello world!

 (5)String[] strArray = String.split(",");//按照“,”进行切割,返回字符串数组.




https://blog.sciencenet.cn/blog-2355761-1005192.html

上一篇:Java_继承和多态
下一篇:Java(二)
收藏 IP: 182.150.36.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-3-28 18:38

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部