Java开发中,基本都会用户String,有些时候忘记了它还有某一个方法,或者曾经没有使用到。而这些方法可能会节约非常多时间。自己为了学习这些方法,决定对部分測试一下。

定义:String=”atm”;

System.out.println();//这里为打印结果

以下開始測试各个方法

char charAt(int index)
返回指定索引处的 char 值。 System.out.println(a.charAt(0));//a
int codePointAt(int index)
返回指定索引处的字符(Unicode 代码点)。
System.out.println(a.codePointAt(1));//116
int codePointBefore(int index)
返回指定索引之前的字符(Unicode 代码点)。
System.out.println(a.codePointBefore(1));//97
int codePointCount(int beginIndex, int endIndex)
返回此 String 的指定文本范围中的 Unicode 代码点数。
System.out.println(a.codePointCount(0,1));//1
int compareTo(String anotherString)
按字典顺序比較两个字符串。
System.out.println(a.compareTo("ATM1"));//32
关于compareTo能够看另外一篇文章
[字符串比較](http://blog.csdn.net/qq_17326933/article/details/47452467)
int compareToIgnoreCase(String str)
不考虑大写和小写,按字典顺序比較两个字符串。
System.out.println(a.compareToIgnoreCase("atm"));//0
String concat(String str)
将指定字符串联到此字符串的结尾。
System.out.println(a.concat("BANK"));//atmBANK
boolean contains(CharSequence s)
当且仅当此字符串包括 char 值的指定序列时,才返回 true。
System.out.println(a.contains("a")); //true
boolean contentEquals(CharSequence cs)
当且仅当此 String 表示与指定序列同样的 char 值时,才返回 true。
boolean contentEquals(StringBuffer sb)
当且仅当此 String 表示与指定的 StringBuffer 同样的字符序列时,才返回 true。 System.out.println(a.contentEquals("atm"));//true
System.out.println(a.contentEquals("atm1"));//false
static String copyValueOf(char[] data)
返回指定数组中表示该字符序列的字符串。
char[] test = new char[]{'a','b','c'};
String a = String.valueOf(test);
String b = String.copyValueOf(test);
System.out.println(a);//abc
System.out.println(b);//abc
static String copyValueOf(char[] data, int offset, int count)
返回指定数组中表示该字符序列的字符串。
char[] test = new char[]{'a','b','c'};
String b = String.copyValueOf(test,1,2);
System.out.println(b);//bc
boolean endsWith(String suffix)
測试此字符串是否以指定的后缀结束。
String a="atm";
System.out.println(a.endsWith("atm"));//true
System.out.println(a.endsWith("tm"));//true
System.out.println(a.endsWith("m"));//true
boolean equals(Object anObject)
比較此字符串与指定的对象。
能够看另外一篇文章
[equales与==](http://blog.csdn.net/qq_17326933/article/details/47188879)
boolean equalsIgnoreCase(String anotherString)
将此 String 与还有一个 String 进行比較,不考虑大写和小写。
static String format(Locale l, String format, Object... args)
使用指定的语言环境、格式字符串和參数返回一个格式化字符串。 网上有一篇文章,总结的非常具体:
[JAVA字符串格式化-String.format()的使用](http://blog.csdn.net/lonely_fireworks/article/details/7962171)
static String format(String format, Object... args)
使用指定的格式字符串和參数返回一个格式化字符串。
byte[] getBytes()
使用平台默认的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。
byte[] bb=a.getBytes();
void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
已过时。 该方法无法将字符正确转换为字节。 从 JDK 1.1 起,完毕该转换的首选方法是通过 getBytes() 构造方法。该方法使用平台的默认字符集。
byte[] getBytes(String charsetName)
使用指定的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
将字符从此字符串拷贝到目标字符数组。
int hashCode()
返回此字符串的哈希代码。
int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引。
int indexOf(int ch, int fromIndex)
从指定的索引開始搜索,返回在此字符串中第一次出现指定字符处的索引。
int indexOf(String str)
返回第一次出现的指定子字符串在此字符串中的索引。
System.out.println(a.indexOf("t"));//1
int indexOf(String str, int fromIndex)
从指定的索引处開始,返回第一次出现的指定子字符串在此字符串中的索引。 String a="atmat";
System.out.println(a.indexOf("t",2));//true
String intern()
返回字符串对象的规范化表示形式。
int lastIndexOf(int ch)
返回最后一次出现的指定字符在此字符串中的索引。
int lastIndexOf(int ch, int fromIndex)
从指定的索引处開始进行后向搜索。返回最后一次出现的指定字符在此字符串中的索引。
int lastIndexOf(String str)
返回在此字符串中最右边出现的指定子字符串的索引。
//要获取最后的11.doc
String a="c/sys/atm/map/11.doc";
System.out.println(a.substring(a.lastIndexOf("/")+1));
int lastIndexOf(String str, int fromIndex)
从指定的索引处開始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。
int length()
返回此字符串的长度。
boolean matches(String regex)
通知此字符串是否匹配给定的正則表達式。
int offsetByCodePoints(int index, int codePointOffset)
返回此 String 中从给定的 index 处偏移 codePointOffset 个代码点的索引。
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
測试两个字符串区域是否相等。
boolean regionMatches(int toffset, String other, int ooffset, int len)
測试两个字符串区域是否相等。
String replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的全部 oldChar 而生成的。
String replace(CharSequence target, CharSequence replacement)
使用指定的字面值替换序列替换此字符串匹配字面值目标序列的每一个子字符串。
String replaceAll(String regex, String replacement)
使用给定的 replacement 字符串替换此字符串匹配给定的正則表達式的每一个子字符串。
String replaceFirst(String regex, String replacement)
使用给定的 replacement 字符串替换此字符串匹配给定的正則表達式的第一个子字符串。
String[] split(String regex)
依据给定的正則表達式的匹配来拆分此字符串。
String[] split(String regex, int limit)
依据匹配给定的正則表達式来拆分此字符串。
boolean startsWith(String prefix)
測试此字符串是否以指定的前缀開始。
boolean startsWith(String prefix, int toffset)
測试此字符串是否以指定前缀開始。该前缀以指定索引開始。
CharSequence subSequence(int beginIndex, int endIndex)
返回一个新的字符序列。它是此序列的一个子序列。
String substring(int beginIndex)
返回一个新的字符串,它是此字符串的一个子字符串。
String substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。
char[] toCharArray()
将此字符串转换为一个新的字符数组。
String toLowerCase()
使用默认语言环境的规则将此 String 中的全部字符都转换为小写。
String toLowerCase(Locale locale)
使用给定 Locale 的规则将此 String 中的全部字符都转换为小写。
String toString()
返回此对象本身(它已经是一个字符串! )。
String toUpperCase()
使用默认语言环境的规则将此 String 中的全部字符都转换为大写。
String toUpperCase(Locale locale)
使用给定的 Locale 规则将此 String 中的全部字符都转换为大写。
String trim()
返回字符串的副本。忽略前导空白和尾部空白。
static String valueOf(boolean b)
返回 boolean 參数的字符串表示形式。
static String valueOf(char c)
返回 char 參数的字符串表示形式。
static String valueOf(char[] data)
返回 char 数组參数的字符串表示形式。
static String valueOf(char[] data, int offset, int count)
返回 char 数组參数的特定子数组的字符串表示形式。
static String valueOf(double d)
返回 double 參数的字符串表示形式。
static String valueOf(float f)
返回 float 參数的字符串表示形式。
static String valueOf(int i)
返回 int 參数的字符串表示形式。
static String valueOf(long l)
返回 long 參数的字符串表示形式。
static String valueOf(Object obj)
返回 Object 參数的字符串表示形式。

从类 java.lang.Object 继承的方法 clone, finalize, getClass, notify, notifyAll, wait, wait, wait

String字符串方法具体解释的更多相关文章

  1. JavaScript String 字符串方法

    JavaScript String 字符串方法汇总   1.str.indexOf() 方法查找字符串中的字符串  返回   字符串中指定文本首次出现的索引(位置)       JavaScript ...

  2. Java String字符串方法

    1.String构造函数 1> String() 2> String(char[] chars) String(char[] chars,int startIndex,int numCha ...

  3. Java中String.valueOf()方法的解释

    1. 由 基本数据型态转换成 String String 类别中已经提供了将基本数据型态转换成 String 的 static 方法 也就是 String.valueOf() 这个参数多载的方法 有下 ...

  4. java算法:统计数字-将数字转换成字符串,然后使用字符串String.valueOf()方法进行判断

    题目: 计算数字 k 在 0 到 n 中的出现的次数,k 可能是 0~9 的一个值. 样例 样例 1: 输入: k = 1, n = 1 输出: 1 解释: 在 [0, 1] 中,我们发现 1 出现了 ...

  5. 深入解析字符串的比较方法:“==”操作符;String.Equals方法;String.Compare方法;String.CompareOrdinal方法。

    1:要判断2个字符串变量是否相等,最高效的方法是看它们是否指向相同的内存地址.前面使用RefernceEquals方法来比较.如果2个变量指向的是不同的内存地址,那么就需要逐字符的比较2个字符串的变量 ...

  6. 字符串对比.net String.EndsWith方法 (String)

    string str="web/abc.aspx"; if(str.EndsWith("abc.aspx")) { 此方法将 value 与位于此实例末尾.与 ...

  7. C# 字符串比较大小 string.Compare()方法

    string.Compare方法,用来比较2个字符串值得大小 string.Compare(str1, str2, true); 返回值: 1 : str1大于str2 0 : str1等于str2 ...

  8. C#基础知识之字符串比较方法:“==”操作符;RefernceEquals;String.Equals方法;String.Compare方法;String.CompareOrdinal方法。

    一.“==”操作符:String.Equals:ReferenceEquals 方法 1.在编程中实际上我们只需要这两种比较,c#中类型也就这两种 (1)值类型的比较:一般我们就是判断两个值类型实例各 ...

  9. JavaScript数据类型 String字符串类型的属性和方法

    属性 字符串String类型的每个实例都有一个length属性,表示字符串中的字符个数.由于字符串是不可变的,所以字符串的长度也不可变 字符串的length属性不会在for/in循环中枚举,也不能通过 ...

随机推荐

  1. Flume OG 与 Flume NG 的对比

    Flume OG 与 Flume NG 的对比 1.Flume OG Flume OG:Flume original generation 即Flume 0.9.x版本,它由agent.collect ...

  2. Java 中 父类变量访问子类方法 需要使用 类型转换 (instenceof)关键字 /类型判断/

    通过数组元素访问方法的时候只能访问在 Animal中定义的方法,对 于 Tiger类和  Fish中定义的方法时却不能调用,例如语句  animal[2].swim();就是不正确的.当 需要访问这些 ...

  3. [ SDOI 2011 ] 打地鼠

    \(\\\) \(Description\) 给出一个\(N\times M\)的矩阵,你可以自由确定一个\(R\times C(R,C>0)\)的矩形,使得可以多个用矩形覆盖整个矩阵,覆盖的定 ...

  4. android黑科技系列——获取加固后应用App的所有方法信息

    一.前言 在逆向应用的时候,我们有时候希望能够快速定位到应用的关键方法,在之前我已经详细介绍了一个自己研发的代码动态注入工具icodetools,来进行动态注入日志信息到应用中,不了解的同学可以查看这 ...

  5. react native 报错日常 if (_total > 0) { ~~~~~~ ^ ~ 1 error generated.

    node_modules/react-native/React/Base/RCTJavaScriptLoader.mm::: error: ordered comparison between poi ...

  6. unity3d 各键值对应代码

    KeyCode :KeyCode是由Event.keyCode返回的.这些直接映射到键盘上的物理键.  值        对应键 Backspace     退格键 Delete      Delet ...

  7. webstorm中配置过visualsvn,后面做更改要更换authentication realm的解决办法

    找这个找了好久,一直改不过来,终于找到了解决办法 首先,在提交代码时提示: 但是我的authentication realm已经时这个了,并且账号和密码也改了,所以要更改authentication ...

  8. [Windows Server 2008] IIS自带FTP配置方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:IIS自带FT ...

  9. CAD类型转换

    AcDbEntity *pEnt; AcDbCircle *pcir = AcDbCircle::cast(pEnt); static_cast<AcDbCircle*>(pEnt); p ...

  10. 谈一谈Dijkstra

    dijkstra呢是最短路三大算法之一.很多人都觉得不如spfa,但是这两者在跑稠密图时,dijkstra有奇效 在讲之前先说一说食用方法: 适用于有向的无负权值的图. 样例飘过 6 9 1 //n个 ...