String类

  • Post author:
  • Post category:其他


一、String类

String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。java把String类声明的final类,不能有类。String类对象创建后不能修改,由0或多个字符组成,包含在一对双引号之间。

二、String类对象的创建

字符串声明:String stringName;

字符串创建:stringName = new String(字符串常量);或stringName = 字符串常量;

构造方法:

1、public String()

无参构造方法,用来创建空字符串的String对象。

String str1 = new String(); 

2、public String(String value)

用已知的字符串value创建一个String对象。

public static void main(String[] args) {         
  String str2 = new String("wcancan");
  String str3 = new String(str2); 
     System.out.println(str3);
        }
        // str3="wcancan"

3、public String(char[] value)

用字符数组value创建一个String对象。

 public static void main(String[] args) {         
  char[] value = {'a','w','c','c'};
   String str4 = new String(value); 
     System.out.println(str4);
        }
   //str4 = "awcc"

4、public String(char chars[], int startIndex, int numChars)

用字符数组chars的startIndex开始的numChars个字符创建一个String对象。

public static void main(String[] args) {         
   char[] value = {'a','w','c','c'};
   String str5 = new String(value, 1, 2);  
     System.out.println(str5);
        }
        //str5 ="wc"

5、public String(byte[] values)

用比特数组values创建一个String对象。

public static void main(String[] args) {         
  byte[] strb = new byte[]{66,99};
  String str6 = new String(strb);  
     System.out.println(str6);
        }
        //  str6 ="Bc"  

三、String类常用方法

1、求字符串长度

public int length()//返回该字符串的长度

public static void main(String[] args) {         
  String str = new String("awcclww27");
  int strlength = str.length();  
     System.out.println(strlength);
        }
       //  strlength =9

2、求字符串某一位置字符

public char charAt(int index) //返回字符串中指定位置的字符;注意字符串中第一个字符索引是0,最后一个是length()-1。

public static void main(String[] args) {         
   String str = new String("awcclww27");
   char ch = str.charAt(4);   
   System.out.println(ch);
      }
      // ch=1

3、提取子串

用String类的substring方法可以提取字符串中的子串,该方法有两种常用参数:

1)public String substring(int beginIndex)//该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。

2)public String substring(int beginIndex, int endIndex)//该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。

 public static void main(String[] args) {         
 String str="wcawwalf";
 String name=str.substring(1,3);
 System.out.println(name);
    }
// name=ca

4、字符串比较

1)public int compareTo(String anotherString) //该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。

2)public int compareToIgnore(String anotherString) //与compareTo方法相似,但忽略大小写。

3)public boolean equals(Object anotherObject) //比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。

4)public boolean equalsIgnoreCase(String anotherString) //与equals方法相似,但忽略大小写。

public static void main(String[] args) {      
String str1 = new String("abc");
String str2 = new String("ABC");
int a = str1.compareTo(str2);  
int b = str1.compareTo(str2);  
boolean c = str1.equals(str2);  
boolean d = str1.equalsIgnoreCase(str2);  
System.out.println(a);
System.out.println(b);
System.out.println(c)
System.out.println(d);
//a=32 b=-32 c=false d=true
 }

5、字符串连接

public String concat(String str)//将参数中的字符串str连接到当前字符串的后面,效果等价于”+”。

public static void main(String[] args) {    
   String str = "111".concat("222").concat("333");
   System.out.println(str);
    }
    //str = 111222333

6、字符串中单个字符查找

1)public int indexOf(int ch/String str) //用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。

2)public int indexOf(int ch/String str, int fromIndex) //改方法与第一种类似,区别在于该方法从fromIndex位置向后查找。

 public static void main(String[] args) {           
         String str="asdfghjdjkl";
         int index=str.indexOf("d");          
         int index2=str.indexOf("d",4);
         System.out.println(index);
         System.out.println(index2);           
}
//2 7

7、字符串中字符的大小写转换

1)public String toLowerCase()//返回将当前字符串中所有字符转换成小写后的新串

2)public String toUpperCase()//返回将当前字符串中所有字符转换成大写后的新串

public static void main(String[] args) {    
     String str="Hellow World";
      str=str.toUpperCase();
      System.out.println(str);
      str=str.toLowerCase();
      System.out.println(str);       
      
   }
   // HELLOW WORLD  hellow world

8、字符串中字符的替换

1)public String replace(char oldChar, char newChar)//用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。

2)public String replaceFirst(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。

3)public String replaceAll(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。

public static void main(String[] args) {    
   String str = "asdzxcasd";
   String str1 = str.replace('a','g');//str1 = "gsdzxcgsd"
   String str2 = str.replace("asd","fgh");//str2 = "fghzxcfgh"
   String str3 = str.replaceFirst("asd","fgh");//str3 = "fghzxcasd"
   String str4 = str.replaceAll("asd","fgh");//str4 = "fghzxcfgh"  
   System.out.println(str1);
   System.out.println(str2);
   System.out.println(str3);
   System.out.println(str4);     
    }
 //gsdzxcgsd fghzxcfgh fghzxcasd fghzxcfgh

9、其他类方法

1)String trim()//截去字符串两端的空格,但对于中间的空格不处理。

 public static void main(String[] args) {    
   String str = " a sd ";
   String str1 = str.trim();
   int a = str.length();
   int b = str1.length();
   System.out.println("a="+a+"  b="+b);    
    }
    //a=6 b=4

2)boolean statWith(String prefix)或boolean endWith(String suffix)//用来比较当前字符串的起始字符或子字符串prefix和终止字符或子字符串suffix是否和当前字符串相同。

public static void main(String[] args) {
    String str="asdzxcasd";
    boolean starts=str.startsWith("s");
    boolean end=str.endsWith("d");   
    System.out.println(starts);
    System.out.println(end);
     }
     //false true

3)contains(String str)//判断参数s是否被包含在字符串中,并返回一个布尔类型的值。

 public static void main(String[] args) {
 String str = "student";
 boolean a=str.contains("stu");
  boolean b=str.contains("ok");
  System.out.println("a="+a+"  b="+b); 
   }   
   //a=true b=false

4)某个字符在一个字符串中出现的所有下标的程序,可以使用intdexOf实现

public static void main(String[] args) { 
     String s="acbcdbcgdgscbbvfb";  
     for(int i=0;i<s.length();i++){ 
      if('b'==s.charAt(i)){  
      System.out.print( i); 
      } 
       }
         }  
   //2 5 12 13 16
  1. 移除字符串中所有空格的两个方法
public static void main(String[] args) {
      String s=" ab cdf dfd dfsf dsfdsfe dsfdsfsfg";
      s=s.trim();
      String[] eachs=s.split("\\s+");
      StringBuilder builder=new StringBuilder();
      for(int i=0;i<eachs.length;i++){
      builder.append(eachs[i]);
          }
       s=builder.toString();
       System.out.println(s);
          } 
          //abcdfdfddfsfdsfdsfedsfdsfsfg

四、字符串与基本类型的转换

1、字符串转换为基本类型

java.lang包中有Byte、Short、Integer、Float、Double类的调用方法:

1)public static byte parseByte(String s)

2)public static short parseShort(String s)

3)public static short parseInt(String s)

4)public static long parseLong(String s)

5)public static float parseFloat(String s)

6)public static double parseDouble(String s)

例如:

public static void main(String[] args) {
       int n = Integer.parseInt("12");
       float f = Float.parseFloat("12.34");
       double d = Double.parseDouble("1.124");
       System.out.println("n="+n+" f="+f+" d="+d);
          } 
          //n=12 f=12.34 d=1.124

2、基本类型转换为字符串类型

String类中提供了String valueOf()方法,用作基本类型转换为字符串类型。

1)static String valueOf(char data[])

2)static String valueOf(char data[], int offset, int count)

3)static String valueOf(boolean b)

4)static String valueOf(char c)

5)static String valueOf(int i)

6)static String valueOf(long l)

7)static String valueOf(float f)

8)static String valueOf(double d)

例如:

 public static void main(String[] args) {
       int i=15;
       float j=17.23f;
       String s1 = String.valueOf(i);
       String s2 = String.valueOf(j);
          System.out.println(s1);
          System.out.println(s2);
          } 
          //15  17.23

参考网址:

https://www.cnblogs.com/ABook/p/5527341.html



版权声明:本文为wwApathy原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。