简单总结一下String常用的方法:

1.charAt(int index) 方法:

Returns the char value at the specified index. An index ranges from0 to length() - 1. The first char value of the sequence is at index0, the next at index 1, and so on, as for array indexing.

返回String 对应位置上的字符。

String s = "abc";

System.out.println(s.charAt(0));  //输出a

System.out.println(s.charAt(1));  //输出b

System.out.println(s.charAt(3));  //运行时出错 java.lang.StringIndexOutOfBoundsException: String index out of range: 3

2.codePointAt(int index) 方法:

Returns the character (Unicode code point) at the specified index. The index refers tochar values (Unicode code units) and ranges from 0 to length() - 1.

返回String对应位置上的字符的ascii码。

String s = "abc";

System.out.println(s.charAt(0));  //输出97

3.codePointBefore(int index)  方法:

Returns the character (Unicode code point) before the specified index. The index refers tochar values (Unicode code units) and ranges from 1 to length.

返回String对应位置上的前一个位置上的字符的ascii码。

String s = "abc";

System.out.println(s.charAt(1));  //输出97 注意是位置的前一个位置,不是字符的前一个字符,所以整个范围是从1到length

4.codePointCount(int beginIndex, int endIndex)方法:

Returns the number of Unicode code points in the specified text range of this String. The text range begins at the specified beginIndex and extends to thechar at index endIndex - 1. Thus the length (in chars) of the text range isendIndex-beginIndex. Unpaired surrogates within the text range count as one code point each.

codePointCount()准确计算unicode字符的数量,而不是char的数量。

这个方法有些复杂。。。。。。

5.compareTo(String anotherString) 方法:

两个字符串的比较:

String s = "abc";
String s1 = "aaa";
String s2 = "abcdef";
String s3 = "a";

假设k是两个字符串上第一个不相同的字符位置:分两种情况

1.如s.compareTo(s1); 此时k=1; 返回值就是:this.charAt(k)-anotherString.charAt(k) 也就是b-a = 1; 如果s换成"adc",返回值就是d-a=3了。

2.如s.compareTo(s2);此时k=3;s和s2的前半部份相等。返回值是:this.length()-anotherString.length() 也就是3-6 = -3,再如 s2.compareTo(s3) 返回值是5;

值得注意的是compareTo方法是区分字符的大小写的,如果String s="aBc"; 然后s.compareTo(s1),返回值就是B-a = -31 了。

6.compareToIgnoreCase(String str)方法:

Compares two strings lexicographically, ignoring case differences. This method returns an integer whose sign is that of callingcompareTo with normalized versions of the strings where case differences have been eliminated by callingCharacter.toLowerCase(Character.toUpperCase(character)) on each character.

与compareTo方法不同的是不区分大小写。会调用大小写转换方法,先把所有字符变成小写,再进行比较。

7.concat(String str) 方法:

Concatenates the specified string to the end of this string.

If the length of the argument string is 0, then this String object is returned. Otherwise, a newString object is created, representing a character sequence that is the concatenation of the character sequence represented by thisString object and the character sequence represented by the argument string.

字符串的拼接。

String s = "abc";
String s1 = "def";

System.out.println(s.concat(s1)); //输出abcdef

System.out.println(s.concat("")); //拼接个空字符串,返回原字符串

8.contains(CharSequence s) 方法:

Returns true if and only if this string contains the specified sequence of char values.

返回是否包含某一字符序列。返回值为true或者false;

String s = "abc";

s.contains("a"); //返回true

s.contains("d");//返回false

9.contentEquals(CharSequence cs) 方法:

Compares this string to the specified CharSequence. The result istrue if and only if this String represents the same sequence of char values as the specified sequence.

返回是否与参数字符序列相等。返回值为true或者false

String s = "abc";

s.contentEquals("abc"); //返回true

s.contentEquals("ddd");//返回false

10.endsWith(String suffix)

返回是否以suffix结尾。

String s = "abc";

s.endsWith("c"); //true

s.endsWith("b"); //false

11.equals(Object anObject)  and  equalsIgnoreCase(String anotherString)

Compares this string to the specified object. The result is true if and only if the argument is notnull and is a String object that represents the same sequence of characters as this object.

字符串相等比较

12 indexOf(String str)

Returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest value k such that:

包含特定字符串的第一个位置。

String s = "abcdcd";

System.out.println(s.indexOf("d")); //返回3

String 常用方法总结相关推荐

  1. 小汤学编程之JAVA基础day10——常用类(二):String常用方法、正则、StringBuffer和StringBuilder、Math和Random类、日期类和数字类

    一.String常用方法 二.正则表达式 1.基本     2.常用     3.高级 三.StringBuilder和StringBuffer类 1.区别     2.常用方法     3.继承结构 ...

  2. 更高效地刷OJ——String常用方法(一)

    因为常用方法较多,所以分为3篇 1.length() : 返回字符串长度,此处的length有别于数组中的length数组中的length为属性,此处的length为方法. 2.charAt() :将 ...

  3. String常用方法API

    String类 1.创建字符串对象--String 类有 11 种构造方法,这些方法提供不同的参数来初始化字符串 1)String s ="Hello"; s引用静态区的字符串字面 ...

  4. StringBuffer 和String常用方法

    StringBuffer 常用方法 在字符串末尾添加各种类型: public StringBuffer append(各种类型) 在某个位置添加各种类型: public StringBuffer in ...

  5. java string 常用方法_String类的12个常用方法

    1.用字符数组value创建一个String对象 方法: public String(char[] value) 实例: char[] value ={"a","b&qu ...

  6. String常用方法总结

    和长度有关的方法 返回类型 方法名 作用 int length() 得到一个字符串的字符个数(一个中文是一个字符,一个英文是一个字符,一个转义字符是一个字符) 和数组有关的方法 返回类型 方法名 作用 ...

  7. [JAVA] String常用方法

    近期学习java,发现String有很多好用并且常用的方法,这里取几个较为常用的做下笔记! 1. length()  获得字符串长度 String a = "Hello World!&quo ...

  8. String常用方法大全(深入源码层面分析)

    1 构造方法类型 String(String original):把字符串数据封装成字符串对象 String(char[] value):把字符数组的数据封装成字符串对象 String(char[] ...

  9. JavaScript字符串String常用方法介绍

    JavaScript字符串在底层是一个字符串数组,比如hello字符串在底层是["h","e","l","l",&quo ...

  10. java String 常用方法集合

    String a = "abc"; String b = "abc"; a==b ;//返回true,因为a,b指向的是同一个地址 String a = new ...

最新文章

  1. 实际应用中git(合并本地与服务器项目)
  2. C++学习笔记-----存在多态调用时,为基类定义虚析构函数
  3. 《Linux就该这么学》培训笔记_ch06_存储结构与磁盘划分
  4. Psych101(part3)--Day3
  5. spark sql读取hive底层_[大数据]spark sql读写Hive数据不一致
  6. java ajax 联动菜单_java结合jQuery.ajax实现左右菜单联动刷新列表内容
  7. mysql乐观锁总结和实践 - 青葱岁月 - ITeye博客
  8. pytorch中的squeeze和unsqueeze对比
  9. 新手做短视频自媒体,再也不用担心找不到视频素材了,抓紧收藏
  10. Algorithm Review 5 图论
  11. 【QT】QCustomPlot图表控件
  12. ppt太大如何压缩到最小的批量处理方式
  13. Vue实战项目开发--首页开发
  14. Python暴力破解ZIP文件密码
  15. Linux:netstat命令结果详解
  16. 腾讯大王卡 蚂蚁宝卡 区别 哪个好
  17. 内网穿透工具-venom
  18. 《计算机网络技术》第三章课后习题答案(全)
  19. 使用hanewin实现 win系统主机,vm虚拟机中linux系统和开发板三方共享文件夹
  20. 3D游戏编程 作业五 枪打恶鬼(打飞碟)

热门文章

  1. Lottie 动画在项目中的使用总结
  2. 岩土工程颗粒流软件PFC6.0技巧——按计算时间导出数据、图像及保存文件
  3. web渗透测试----14、CSRF(跨站请求伪造攻击)
  4. CSRF--跨站请求伪造
  5. 【JavaEE】Spring 事务传播机制
  6. Windows 好用的护眼软件
  7. html页面分页显示问题,HTML分页,显示分页页面内容
  8. 未来城市插画mac动态桌面壁纸
  9. 变量之八大基本数据类型#基本数据类型相互转换#基本数据类型与String字符串间转换
  10. stc c语言子程序库,STC15 系列宏晶单片机的样例程序 - 下载 - 搜珍网