Java的String类常用方法
一、构造函数
String(byte[ ] bytes):通过byte数组构造字符串对象。
String(char[ ] value):通过char数组构造字符串对象。
String(Sting original):构造一个original的副本。即:拷贝一个original。
String(StringBuffer buffer):通过StringBuffer数组构造字符串对象。
例如:
byte[] b = {'a','b','c','d','e','f','g','h','i','j'};
char[] c = {'0','1','2','3','4','5','6','7','8','9'};
String sb = new String(b); //abcdefghij
String sb_sub = new String(b,3,2); //de
String sc = new String(c); //0123456789
String sc_sub = new String(c,3,2); //34
String sb_copy = new String(sb); //abcdefghij
System.out.println("sb:"+sb);
System.out.println("sb_sub:"+sb_sub);
System.out.println("sc:"+sc);
System.out.println("sc_sub:"+sc_sub);
System.out.println("sb_copy:"+sb_copy);
输出结果:sb:abcdefghij
sb_sub:de
sc:0123456789
sc_sub:34
sb_copy:abcdefghij
二、方法:
说明:①、所有方法均为public。
②、书写格式: \[修饰符\] <返回类型><方法名(\[参数列表\])>
例如:static int parseInt(String s)
表示此方法(parseInt)为类方法(static),返回类型为(int),方法所需要为String类型。
char charAt(int index) :取字符串中的某一个字符,其中的参数index指的是字符串中序数。字符串的序数从0开始到length()-1 。
例如:String s = new String("abcdefghijklmnopqrstuvwxyz");
System.out.println("s.charAt(5): " + s.charAt(5) );
结果为: s.charAt(5): fint compareTo(String anotherString) :当前String对象与anotherString比较。相等关系返回0;不相等时,从两个字符串第0个字符开始比较,返回第一个不相等的字符差,另一种情况,较长字符串的前面部分恰巧是较短的字符串,返回它们的长度差。
int compareTo(Object o) :如果o是String对象,和2的功能一样;否则抛出ClassCastException异常。
例如:String s1 = new String("abcdefghijklmn");
String s2 = new String("abcdefghij");
String s3 = new String("abcdefghijalmn");
System.out.println("s1.compareTo(s2): " + s1.compareTo(s2) ); //返回长度差
System.out.println("s1.compareTo(s3): " + s1.compareTo(s3) ); //返回'k'-'a'的差
结果为:s1.compareTo(s2): 4
s1.compareTo(s3): 10String concat(String str) :将该String对象与str连接在一起。
boolean contentEquals(StringBuffer sb) :将该String对象与StringBuffer对象sb进行比较。
static String copyValueOf(char[] data) :
static String copyValueOf(char[] data, int offset, int count) :这两个方法将char数组转换成String,与其中一个构造函数类似。
boolean endsWith(String suffix) :该String对象是否以suffix结尾。
例如:String s1 = new String("abcdefghij");
String s2 = new String("ghij");
System.out.println("s1.endsWith(s2): " + s1.endsWith(s2) );
结果为:s1.endsWith(s2): trueboolean equals(Object anObject) :当anObject不为空并且与当前String对象一样,返回true;否则,返回false。
byte[] getBytes() :将该String对象转换成byte数组。
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) :该方法将字符串拷贝到字符数组中。其中,srcBegin为拷贝的起始位置、srcEnd为拷贝的结束位置、字符串数值dst为目标字符数组、dstBegin为目标字符数组的拷贝起始位置。
例如:char[] s1 = {'I',' ','l','o','v','e',' ','h','e','r','!'};//s1=I love her!
String s2 = new String("you!"); s2.getChars(0,3,s1,7); //s1=I love you!
System.out.println( s1 );
结果为:I love you!int hashCode() :返回当前字符的哈希表码。
int indexOf(int ch) :只找第一个匹配字符位置。
int indexOf(int ch, int fromIndex) :从fromIndex开始找第一个匹配字符位置。
int indexOf(String str) :只找第一个匹配字符串位置。
int indexOf(String str, int fromIndex) :从fromIndex开始找第一个匹配字符串位置。
例如:String s = new String("write once, run anywhere!");
String ss = new String("run");
System.out.println("s.indexOf('r'): " + s.indexOf('r') );
System.out.println("s.indexOf('r',2): " + s.indexOf('r',2) );
System.out.println("s.indexOf(ss): " + s.indexOf(ss) );
结果为:s.indexOf('r'): 1
s.indexOf('r',2): 12
s.indexOf(ss): 12int lastIndexOf(int ch)
int lastIndexOf(int ch, int fromIndex)
int lastIndexOf(String str)
int lastIndexOf(String str, int fromIndex) 以上四个方法与13、14、15、16类似,不同的是:找最后一个匹配的内容。
public class CompareToDemo {
public static void main (String[] args) {
String s1 = new String("acbdebfg");System.out.println(s1.lastIndexOf((int)'**b**',**7**));
}
}
运行结果:5
(其中fromIndex的参数为 7,是从字符串acbdebfg的最后一个字符g开始往前数的位数。既是从字符c开始匹配,寻找最后一个匹配b的位置。所以结果为 5)int length() :返回当前字符串长度。
String replace(char oldChar, char newChar) :将字符号串中第一个oldChar替换成newChar。
boolean startsWith(String prefix) :该String对象是否以prefix开始。
boolean startsWith(String prefix, int toffset) :该String对象从toffset位置算起,是否以prefix开始。
例如:String s = new String("write once, run anywhere!");
String ss = new String("write");
String sss = new String("once");
System.out.println("s.startsWith(ss): " + s.startsWith(ss) );
System.out.println("s.startsWith(sss,6): " + s.startsWith(sss,6) );
结果为:s.startsWith(ss): true
s.startsWith(sss,6): trueString substring(int beginIndex) :取从beginIndex位置开始到结束的子字符串。
26.String substring(int beginIndex, int endIndex) :取从beginIndex位置开始到endIndex位置的子字符串。char[ ] toCharArray() :将该String对象转换成char数组。
String toLowerCase() :将字符串转换成小写。
String toUpperCase() :将字符串转换成大写。
例如:String s = new String("java.lang.Class String");
System.out.println("s.toUpperCase(): " + s.toUpperCase() );
System.out.println("s.toLowerCase(): " + s.toLowerCase() );
结果为:s.toUpperCase(): JAVA.LANG.CLASS STRING
s.toLowerCase(): java.lang.class stringstatic String valueOf(boolean b)
static String valueOf(char c)
static String valueOf(char[] data)
static String valueOf(char[] data, int offset, int count)
static String valueOf(double d)
static String valueOf(float f)
static String valueOf(int i)
static String valueOf(long l)
static String valueOf(Object obj)
以上方法用于将各种不同类型转换成Java字符型。这些都是类方法。
**Java中String类的常用方法:
public char charAt(int index)**
返回字符串中第index个字符;
public int length()
返回字符串的长度;
public int indexOf(String str)
返回字符串中第一次出现str的位置;
public int indexOf(String str,int fromIndex)
返回字符串从fromIndex开始第一次出现str的位置;
public boolean equalsIgnoreCase(String another)
比较字符串与another是否一样(忽略大小写);
public String replace(char oldchar,char newChar)
在字符串中用newChar字符替换oldChar字符
public boolean startsWith(String prefix)
判断字符串是否以prefix字符串开头;
public boolean endsWith(String suffix)
判断一个字符串是否以suffix字符串结尾;
public String toUpperCase()
返回一个字符串为该字符串的大写形式;
public String toLowerCase()
返回一个字符串为该字符串的小写形式
public String substring(int beginIndex)
返回该字符串从beginIndex开始到结尾的子字符串;
public String substring(int beginIndex,int endIndex)
返回该字符串从beginIndex开始到endsIndex结尾的子字符串
public String trim()
返回该字符串去掉开头和结尾空格后的字符串
public String[] split(String regex)
将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组
实例:
public class SplitDemo{
public static void main (String[] args) {
String **date** = "2008/09/10";
**String\[ \]** **dateAfterSplit**= new String\[3\];
**dateAfterSplit**=**date.split("/")**; **//以“/”****作为分隔符来分割date字符串,并把结果放入3个字符串中。**
for(int i=0;i<**dateAfterSplit**.length;i++)
System.out.print(**dateAfterSplit**\[i\]+" ");
}
}
运行结果:2008 09 10 //结果为分割后的3个字符串
实例:
TestString1.java:
程序代码
public class TestString1
{
public static void main(String args[]) {
String s1 = "Hello World" ;
String s2 = "hello world" ;
System.out.println(s1.charAt(1)) ;
System.out.println(s2.length()) ;
System.out.println(s1.indexOf("World")) ;
System.out.println(s2.indexOf("World")) ;
System.out.println(s1.equals(s2)) ;
System.out.println(s1.equalsIgnoreCase(s2)) ;
String s = "我是J2EE程序员" ;
String sr = s.replace('我','你') ;
System.out.println(sr) ;
}
}
TestString2.java:
程序代码
public class TestString2
{
public static void main(String args[]) {
String s = "Welcome to Java World!" ;
String s2 = " magci " ;
System.out.println(s.startsWith("Welcome")) ;
System.out.println(s.endsWith("World")) ;
String sL = s.toLowerCase() ;
String sU = s.toUpperCase() ;
System.out.println(sL) ;
System.out.println(sU) ;
String subS = s.substring(11) ;
System.out.println(subS) ;
String s1NoSp = s2.trim() ;
System.out.println(s1NoSp) ;
}
Java的String类常用方法的更多相关文章
- JAVA中String类常用方法 I
String类常用方法有: int length() -– 返回当前字符串的长度 int indexOf(int ch) -– 查找ch字符在该字符串中第一次出现的位置 int indexOf(Str ...
- Java中String类常用方法(字符串中的子字符串的个数)
重点内容 4种方法: 1.int indexOf(String str)返回第一次出现的指定子字符串在此字符串中的索引. 2.int indexOf(String str, int startInde ...
- java 中String类的常用方法总结,带你玩转String类。
String类: String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.String类对象创建后不能修改,StringBuffer & St ...
- JAVA的String类的常用方法(转载)
Java-String类的常用方法总结 一.String类String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.java把String类声明的f ...
- java 中String类的常用方法总结,玩转String类
String类: String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.String类对象创建后不能修改,StringBuffer & St ...
- JAVA String类常用方法
一.String类String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.java把String类声明的final类,不能有类.String类对象创建 ...
- JAVA中String类以及常量池和常用方法
一.String类的定义 String类特点:String 代表字符串.java程序中所有的字符串文字(例如:"abc")都被实现为String类的子类 String类特点:长度不 ...
- 菜鸡的Java笔记 第十四 String 类常用方法
/*String 类常用方法 将所有String类的常用方法全部记下来,包括方法名称,参数作用以及类型 一个成熟的编程语言,除了它的语法非常完善之外,那么也需要提供有大量的开发类库 ...
- Java中String类的方法及说明
String : 字符串类型 一. String sc_sub = new String(c,3,2); // String sb_copy = new String(sb) ...
随机推荐
- 牛客多校第一场 B Inergratiion
牛客多校第一场 B Inergratiion 传送门:https://ac.nowcoder.com/acm/contest/881/B 题意: 给你一个 [求值为多少 题解: 根据线代的知识 我们可 ...
- ES安装&常见错误
ES常见错误 案例一 [2018-06-20T02:35:47,152][INFO ][o.e.b.BootstrapChecks ] [SUcoFrg] bound or publishing to ...
- 使用eclipse运行maven web项目 插件/非插件
一.使用插件 tomcat 8.5 tomcat-users.xml中添加这一行就ok <user username="admin" password="admin ...
- mysql find_in_set 与 in 的用法与区别,mysql范围搜索,mysql范围查询
mysql find_in_set 与 in 的用法与区别 1.find_in_set 用于模糊查询,并且数据库中的值是用英文逗号分隔的: 例如: (1).去字段中查询 select find_in_ ...
- (1)51单片机NOP指令
提问:什么是NOP指令?干什么用的?单片机程序里执行一条nop指令需要多长时间? (1)一个NOP就是一个机器周期 (2)空指令,延时一个机器周期 (3)这个与单片机型号.指令类型和使用的晶振频率有关 ...
- MyBatis整合Spring MVC(易百教程)
MyBatis是ibatis的升级版,作为hibernate的老对手,它是一个可以自定义SQL.存储过程和高级映射的持久层框架.与Hibernate 的主要区别就是 Mybatis 是半自动化的,而 ...
- 第二阶段:2.商业需求文档MRD:5.MRD-Roadmap及规划
产品路线图可以用泳道图来实现.将之前做过的泳道图的角色换为阶段即可. 可以以月为单位.左边就是一些产品的功能. 基础功能,有的功能会跨月甚至夸功能模块.比如图中的会员等级. 通过线段来联系各个功能与先 ...
- SpringMVC||IQ
Here's question about springmvc 1.Springmvc spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即 ...
- Python中三大框架各自的应用场景(DJango,flask,Tornado)
django:主要是用来搞快速开发的,他的亮点就是快速开发,节约成本,正常的并发量不过10000,如果要实现高并发的话,就要对django进行二次开发,比如把整个笨重的框架给拆掉,自己写socket实 ...
- 1069 微博转发抽奖 (20分)C语言
小明 PAT 考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔 N 个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行给出三个正整数 M(≤ 1000). ...