java,String

很多的编程语言都会强调利用字符数组来描述字符串,实际上在Java里面也存在有类似的概念,在String类中也提供有一系列与字符操作有关的方法。

No.

方法名称

类型

描述

1

public String(char[] value)

构造

将全部的字符数组作为String的内容

2

public String(char[] value, int offset, int count)

构造

将部分的字符数组变为字符串,设置字符数组的开始索引与使用个数

3

public char charAt(int index)

普通

返回指定索引位置上的字符

4

public char[] toCharArray()

普通

将字符串以字符数组的形式返回

范例:验证charAt()方法

Java代码  收藏代码

  1. public class StringDemo {  

  2.    

  3.       public static void main(String args[]) {  

  4.              String str = "hello" ;  

  5.             char c = str.charAt(0) ;  

  6.             System.out.println(c) ;  

  7.        }  

  8. }  

 

在程序中字符串索引都是从0开始的。

而后最为关键的是实现字符串与字符数组间的互相转换。

范例:将字符串变为字符数组


Java代码  收藏代码

  1. public class StringDemo {  

  2.        public static void main(String args[]) {  

  3.            String str = "helloworld" ;  

  4.    char [] data = str.toCharArray() ;// 将字符串变为字符数组  

  5.           for (int x = 0 ; x < data.length ; x ++) {  

  6.       System.out.print(data[x] + "、") ;  

  7.           }  

  8.      }  

  9. }  

 

    当字符串变为字符数组之后就可以针对于里面的每一个字符进行操作。

范例下载将字符串中的小写字母变为大写字母

· 小写字母的编码 大写字母的编码,差了32,也就是说每一个字符都减去32即可。

 

Java代码  收藏代码

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String str = "helloworld" ;  

  4.         char [] data = str.toCharArray() ;  // 将字符串变为字符数组  

  5.         for (int x = 0 ; x < data.length ; x ++) {  

  6.             data[x] -= 32 ;         // 编码减去32,成大写  

  7.         }  

  8.         System.out.println(new String(data)) ;  

  9.         System.out.println(new String(data,5,5)) ;   // 第5个索引,之后取5个长度的内容  

  10.     }  

  11. }  

   下面可以利用此操作功能判断某一个字符串是否全部由数字所组成。

 

     · 将字符串变为字符数组,这样做的目的可以进行每个字符的依次判断;

     · 判断每一个字符的范围是否是数字:'0' ~ '9'之间。

范例:实现判断

 

Java代码  收藏代码

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String str = "123" ;  

  4.         System.out.println(isNumber(str)) ;  

  5.     }  

  6.     public static boolean isNumber(String temp) {  

  7.         char data [] = temp.toCharArray() ;  // 将字符串变为字符数组  

  8.         for (int x = 0 ; x < data.length ; x ++) {  

  9.             if (data[x] < '0' || data[x] > '9') {  // 不是数字  

  10.                 return false ;  

  11.             }  

  12.         }  

  13.         return true ;  

  14.     }  

  15. }  

 

 

3.2、字节与字符串

    除了字符可以与字符串进行互相转换之外,字节也可以进行转换,但是这样的转换往往会出现在实际的开发环节中。

No.

方法名称

类型

描述

1

public String(byte[] bytes)

构造

将全部字节数组变为字符串

2

public String(byte[] bytes, int offset, int length)

构造

将部分字节数组变为字符串

3

public byte[] getBytes()

普通

将字符串变为字节数组

4

public byte[] getBytes(String charsetName) throws UnsupportedEncodingException

普通

编码转化

    首先对于byte一定要清楚,虽然它的范围要比char小,但是byte还是可以明确的描述出字母的范围的。

范例下载利用字节数组实现小写字母变为大写字母的操作

 

Java代码  收藏代码

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String str = "helloworld" ;  

  4.         byte data [] = str.getBytes() ; // 将字符串变为字节数组  

  5.         for (int x = 0 ; x < data.length ; x ++) {  

  6.             data[x] -= 32 ;  // 改变编码  

  7.         }  

  8.         System.out.println(new String(data)) ;  

  9.     }  

  10. }  

 

 

3.3、字符串比较

现在至少应该知道了字符串比较有一个equals()方法,但是此方法本身是属于区分大小写的比较,为此在Java里面针对于字符串的比较实际上还有其他方法。

No.

方法名称

类型

描述

1

public boolean equals(String anObject)

普通

区分大小写的字符串比较

2

public boolean equalsIgnoreCase(String anotherString)

普通

不区分大小写的字符串比较

3

public int compareTo(String anotherString)

普通

比较字符串大小

4

public int compareToIgnoreCase(String str)

普通

不区分大小写比较字符串大小

范例下载观察equals()的问题

 

Java代码  收藏代码

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String strA = "hello" ;  

  4.         String strB = "HEllo" ;  

  5.         System.out.println(strA.equals(strB)) ; // false  

  6.         System.out.println(strA.equalsIgnoreCase(strB)) ;   // true  

  7.     }  

  8. }  

     验证码操作中可真的部分大写或者是小写字母。

 

     在进行字符串相等的比较操作之中,最为关键的一个方法是:compareTo(),这个方法本身返回一个int型数据,而这个int型数据有三种结果:大于(>0)、小于(<0)、等于(=0)。

范例:比较字符串大小

 

Java代码  收藏代码

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String strA = "A" ;  

  4.         String strB = "a" ;  

  5.         System.out.println(strA.compareTo(strB)) ;  // -32  

  6.         System.out.println(strA.compareToIgnoreCase(strB)) ;    // 0  

  7.     }  

  8. }  

     compareTo()方法之中要进行比较是依据编码进行相减得来的。所以利用compareTo()返回值范围进行判断就可以区分大小关系了。

 

 

Java代码  收藏代码

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String strA = "a" ;  

  4.         String strB = "a" ;  

  5.         System.out.println(strA.compareTo(strB)) ;  // -32  

  6.         System.out.println(strA.compareToIgnoreCase(strB)) ;    // 0  

  7.         if (strA.compareTo(strB) == 0) {  

  8.             System.out.println("两个字符串的内容相同!") ;  

  9.         }  

  10.     }  

  11. }  

 

 

3.4、字符串查找

     从一个完整的字符串之中查找一些子字符串,而可以实现这种字符串查找功能的方法有如下几个:

No.

方法名称

类型

描述

1

public boolean contains(String s)

普通

判断某一个字符串是否存在

2

public int indexOf(String str)

普通

取得某一个子字符串的索引位置,找不到返回-1

3

public int indexOf(String str, int fromIndex)

普通

从指定索引位置开始检索子字符串位置,找不到就返回-1

4

public int lastIndexOf(String str)

普通

从后向前查找指定子字符串的位置,找不到返回-1

5

public int lastIndexOf(String str, int fromIndex)

普通

从指定位置由后向前查找子字符串的位置,找不到返回-1

6

public boolean startsWith(String prefix)

普通

判断是否以某个字符串开头

7

public boolean startsWith(String prefix, int toffset)

普通

从指定位置判断是否以某个字符串开头

8

public boolean endsWith(String suffix)

普通

是否以指定的字符串结尾

      如果要查找中间的内容,那么现在基本上都使用contains()方法了。

范例下载使用contains()方法

 

Java代码  收藏代码

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String str = "hello" ;  // 0:h、1:e、2:l、3:l、4:o。  

  4.         if (str.contains("l")) {     // 此方法直接返回true或者是false  

  5.             System.out.println("已查找到!") ;  

  6.         }  

  7.     }  

  8. }  

 contains()方法虽然现在用的比较多,但是其是在JD K1.5后才提供的。而在JD 1.5之前那么只能够通过indexOf()方法实现。

 

范例:利用indexOf()方法判断

 

Java代码  收藏代码

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String str = "hello" ;  // 0:h、1:e、2:l、3:l、4:o。  

  4.         System.out.println(str.indexOf("l")) ;  // 2,第一个满足的位置  

  5.         if (str.indexOf("l") != -1) {   // 现在表示索引查找到了  

  6.             System.out.println("内容已经查找到!") ;  

  7.         }  

  8.     }  

  9. }  

 默认情况下indexOf()都是从第一个字母开始查找。那么也可以利用其的重载方法从指定索引位置查找。

 

范例:其他查找

Java代码  收藏代码

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String str = "hello" ;  // 0:h、1:e、2:l、3:l、4:o。  

  4.         System.out.println(str.indexOf("l",3)) ;    // 3  

  5.         System.out.println(str.lastIndexOf("l")) ;  // 3  

  6.     }  

  7. }  

 在字符串查找操作里面还可以判断开头或者是结尾。

范例下载判断开头或结尾

Java代码  收藏代码

  1. public class StringDemo {  

  2.     public static void main(String args[]) {  

  3.         String str = "**@@hello##" ;  

  4.         System.out.println(str.startsWith("**")) ;      // true  

  5.         System.out.println(str.startsWith("@@",2)) ;    // true  

  6.         System.out.println(str.endsWith("##")) ;    // true  

  7.     }  

  8. }  

 这几个查询现在出现最多的就是contains()startsWith()endsWith()


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章