A​n​d​r​o​i​d​的​S​t​r​i​n​g​用​法


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类型。
1. 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): f
2. int compareTo(String anotherString) :当前String对象与anotherString比较。相等关系返回0;不相等时,从两个字符串第0个字符开始比较,返回第一个不相等的字符差,另一种情况,较长字符串的前面部分恰巧是较短的字符串,返回它们的长度差。
3. 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) ); //返回长度差


 


 


 

 
 
 
运行结果: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)      }    

 

 
 
String类主要方法的使用:
1、获取长度 *.length();//这与数组中的获取长度不同,*.length; 2、比较字符串(1) equals() //判断内容是否相同 (2)compareTo() //判断字符串的大小关系
(3)compareToIgnoreCase(String int) //在比较时忽略字母大小写 (4)==   //判断内容与地址是否相同
(5)equalsIgnoreCase() //忽略大小写的情况下判断内容是否相同 如果想对字符串中的部分内容是否相同进行比较,可以用
(6)reagionMatches() //有两种 public boolean regionMatches(int toffset, String other,int ooffset,int len);表示如果String对象的一个子字符串与参数other的一个子字符串是相同的字符序列,则为true.要比较的String 对象的字符串从索引toffset开始,other的字符串从索引ooffset开始,长度为len。
public boolean reagionMatches(boolean ignoreCase,int toffset,String other,int ooffset,int len);//用布尔类型的参数指明两个字符串的比较是否对大小写敏感。 一、查找字符串中某个位置的字符
public char charAt(int index);//返回指定索引index位置上的字符,索引范围从0开始 四、查找指定字符串在字符串中第一次或最后一词出现的位置
在String类中提供了两种查找指定位置的字符串第一次出现的位置的方法
(1)public int indexOf(String str);//从字符串开始检索str,并返回第一次出现的位置,未出现返回-1 (2)public int indexOf(String str,int fromIndex);//从字符串的第fromIndex个字符开始检索str 查找最后一次出现的位置有两种方法 (1)public int lastIndexOf(String str);
(2)public int lastIndexOf(String str,int fromIndex);
如果不关心字符串的确切位置则可使用public boolean contains(CharSequence s); 二、检查字符串的起始字符和结束字符 开始的字符串两种方法

 

 
 
(1)public boolean starWith(String prefix,int toffset);//如果参数prefix表示的字符串序列是该对象从索引toffset处开始的子字符串,则返回true (2)public boolean starWith(String prefix); 结束的字符串方法
public boolean endsWith(String suffix); 三、截取子串
(1)public String subString(int beginIndex);
(2)public String subString(int beginIndex,int endIndex);//返回的字符串是从beginIndex开始到endIndex-1的串
要返回后4位可以这样写Syetem.out.println(*.subString()(*.length()-4)); 四、字符串的替换 两种方法
(1)public String replace(char oldChar,char newChar);
(2)public String replace(CharSequence target,CharSequence replacement);//把原来的etarget子序列替换为replacement序列,返回新串
(3)public String replaceAll(String regex,String replacement);//用正则表达式实现对字符串的匹配 五、字符串的大小写替转换
(1)public String toLowerCase(Locale locale); (2)public String toLowerCase();
(3)public String toupperCase(Locale locale); (4)public String toUpperCase(); 六、去除字符串首尾空格 *.trim(); 七、字符串转换
1、将字符串转换成字符数组

 

 
 
public char[] toCharArray(); 2、将字符串转换成字符串数组
public String[] split(String regex);//regex 是给定的匹配 3、将其它数据类型转化为字符串 (1)public static String valueOf(boolean b); (2)public static String valueOf(char c); (3)public static String valueOf(int i); (4)public static String valueOf(long i); (5)public static String valueOf(float f); (6)public static String valueOf(double d); (7)public static String valueOf(char[] data); (8)public static String valueOf(Object obj); 可变字符串的创建和初始化 两种方法:
public StringBuffer();
public StringBuffer(int caoacity); StringBuffer类主要方法的使用: 一、获取可变字符串长度 (1)public int length(); (2)public int capacity();
(3)public void setLength(int newLength); 二、比较可变字符串
用String 类的equals()方法比较,但是不同。
类Object中的equals()方法比较的是两个对象的地址是否相等,而不仅仅是比较内容,但是String类在继承Object类的时候重写了equals()方法,只是比较两个对象的内容是否相等

 

 
 
而在StringBuffer类中没有重写Object类的equals()方法,所以比较的是地址和内容。 三、追加和插入字符串
(1)追加 public StringBuffer append(type t);
(2)插入 public StringBuffer insert(int offset,type t);//在offset处加入类型为type的字符串 四、反转和删除字符串
(1)反转 public StringBuffer reverse();
(2)删除 public StringBuffer delete(int start,int end); 五、减少用于可变字符序列的存储空间 public void trimToSize();
六、StringBuffer类转换成String类 public String toString();
 
 

 

 

 

运行结果

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)  

    

 

 

 

 

 

 

String

类主要方法的使用:

 

1

、获取长度

 

*.length();//

这与数组中的获取长度不同,

*.length; 

2

、比较字符串

(1) equals() //

判断内容是否相同

 

(2)compareTo() //

判断字符串的大小关系

 

(3)compareToIgnoreCase(String int) //

在比较时忽略字母大小写

 

(4)==   

//

判断内容与地址是否相同

 

(5)equalsIgnoreCase() //

忽略大小写的情况下判断内容是否相同

 

如果想对字符串中的部分内容是否相同进行比较,可以用

 

(6)reagionMatches() //

有两种

 

public boolean regionMatches(int toffset, String other,int ooffset,int len);

示如果

String

对象的一个子字符串与参数

other

的一个子字符串是相同的字符序列,则为

true.

要比较的

String 

对象的字符串从索引

toffset

开始

,other

的字符串从索引

ooffset

开始,长度为

len

 

public boolean reagionMatches(boolean ignoreCase,int toffset,String other,int ooffset,int len);//

用布尔类

型的参数指明两个字符串的比较是否对大小写敏感。

 

一、查找字符串中某个位置的字符

 

public char charAt(int index);//

返回指定索引

index

位置上的字符,索引范围从

0

开始

 

四、查找指定字符串在字符串中第一次或最后一词出现的位置

 

String

类中提供了两种查找指定位置的字符串第一次出现的位置的方法

 

(1)public int indexOf(String str);//

从字符串开始检索

str

,并返回第一次出现的位置,未出现返回

-1 

(2)public int indexOf(String str,int fromIndex);//

从字符串的第

fromIndex

个字符开始检索

str 

查找最后一次出现的位置有两种方法

 

(1)public int lastIndexOf(String str); 

(2)public int lastIndexOf(String str,int fromIndex); 

如果不关心字符串的确切位置则可使用

public boolean contains(CharSequence s); 

二、检查字符串的起始字符和结束字符

 

开始的字符串两种方法

 

 

 

(1)public boolean starWith(String prefix,int toffset);//

如果参数

prefix

表示的字符串序列是该对象从索引

toffset

处开始的子字符串,则返回

true 

(2)public boolean starWith(String prefix); 

结束的字符串方法

 

public boolean endsWith(String suffix); 

三、截取子串

 

(1)public String subString(int beginIndex); 

(2)public String subString(int beginIndex,int endIndex);//

返回的字符串是从

beginIndex

开始到

endIndex-1

的串

 

要返回后

4

位可以这样写

Syetem.out.println(*.subString()(*.length()-4)); 

四、字符串的替换

 

两种方法

 

(1)public String replace(char oldChar,char newChar); 

(2)public String replace(CharSequence target,CharSequence replacement);//

把原来的

etarget

子序列替

换为

replacement

序列,返回新串

 

(3)public String replaceAll(String regex,String replacement);//

用正则表达式实现对字符串的匹配

 

五、字符串的大小写替转换

 

(1)public String toLowerCase(Locale locale); 

(2)public String toLowerCase(); 

(3)public String toupperCase(Locale locale); 

(4)public String toUpperCase(); 

六、去除字符串首尾空格

 

*.trim(); 

七、字符串转换

 

1

、将字符串转换成字符数组

 

 

 

public char[] toCharArray(); 

2

、将字符串转换成字符串数组

 

public String[] split(String regex);//regex 

是给定的匹配

 

3

、将其它数据类型转化为字符串

 

(1)public static String valueOf(boolean b); 

(2)public static String valueOf(char c); 

(3)public static String valueOf(int i); 

(4)public static String valueOf(long i); 

(5)public static String valueOf(float f); 

(6)public static String valueOf(double d); 

(7)public static String valueOf(char[] data); 

(8)public static String valueOf(Object obj); 

可变字符串的创建和初始化

 

两种方法

public StringBuffer(); 

public StringBuffer(int caoacity); 

StringBuffer

类主要方法的使用:

 

一、获取可变字符串长度

 

(1)public int length(); 

(2)public int capacity(); 

(3)public void setLength(int newLength); 

二、比较可变字符串

 

String 

类的

equals()

方法比较,但是不同。

 

Object

中的

equals()

方法比较的是两个对象的地址是否相等,而不仅仅是比较内容,但是

String

类在继

Object

类的时候重写了

equals()

方法,只是比较两个对象的内容是否相等

 

 

 

而在

StringBuffer

类中没有重写

Object

类的

equals()

方法,所以比较的是地址和内容。

 

三、追加和插入字符串

 

(1)

追加

 

public StringBuffer append(type t); 

(2)

插入

 

public StringBuffer insert(int offset,type t);//

offset

处加入类型为

type

的字符串

 

四、反转和删除字符串

 

(1)

反转

 

public StringBuffer reverse(); 

(2)

删除

 

public StringBuffer delete(int start,int end); 

五、减少用于可变字符序列的存储空间

 

public void trimToSize(); 

六、

StringBuffer

类转换成

String

 

public String toString(); 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值