JAVA String

  • 1. 字符串常用方法
  • 1.1 String.length()方法
  • 1.2 String equals() 方法
  • 1.3 String.startsWith()方法
  • 1.4 String.endswith()方法
  • 1.5 String.reginMatches()方法
  • 1.6 String.compareTo()方法
  • 1.7 String.contains() 方法
  • 1.8 String.indexOf()方法
  • 1.9 String.substring()方法
  • 1.10 String.trim()方法
  • 2. 字符串基本类型转换
  • 2.1 字符串转换成其他变量
  • 2.2 其他类型转换成字符串
  • 3. 字符串综合案例
  • 3.1 图书检索
  • 3.2 购物小票


在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串

  1. 简单的方法构造字符串
String str = "Runoob";
  1. 用构造函数创建字符串
String str2=new String("Runoob");

1. 字符串常用方法

1.1 String.length()方法

String 类的一个访问器方法是 length() 方法,它返回字符串对象包含的字符数。

public class StringDemo {
    public static void main(String args[]) {
        String site = "www.runoob.com";
        int len = site.length();
        System.out.println( "菜鸟教程网址长度 : " + len );
   }
}

1.2 String equals() 方法

  1. 使用 == 和 equals() 比较字符串。String 中 == 比较引用地址是否相同,equals() 比较字符串的内容是否相同:
  2. equals() 方法用于将字符串与指定的对象比较。String 类中重写了 equals() 方法用于比较两个字符串的内容是否相等。
public class Test {
    public static void main(String args[]) {
        String Str1 = new String("runoob");
        String Str2 = Str1;
        String Str3 = new String("runoob");
        boolean retVal;

        retVal = Str1.equals( Str2 );
        System.out.println("返回值 = " + retVal );

        retVal = Str1.equals( Str3 );
        System.out.println("返回值 = " + retVal );
    }
}
返回值 = true
返回值 = true

1.3 String.startsWith()方法

public class Test {
    public static void main(String args[]) {
        String Str = new String("www.runoob.com");
 
        System.out.print("返回值 :" );
        System.out.println(Str.startsWith("www") );
 
        System.out.print("返回值 :" );
        System.out.println(Str.startsWith("runoob") );
 
        System.out.print("返回值 :" );
        System.out.println(Str.startsWith("runoob", 4) );
    }
}
返回值 :true
返回值 :false
返回值 :true

1.4 String.endswith()方法

public class Test {
    public static void main(String args[]) {
        String Str = new String("菜鸟教程:www.runoob.com");
        boolean retVal;
 
        retVal = Str.endsWith( "runoob" );
        System.out.println("返回值 = " + retVal );
 
        retVal = Str.endsWith( "com" );
        System.out.println("返回值 = " + retVal );
    }
}
返回值 = false
返回值 = true

1.5 String.reginMatches()方法

ignoreCase – 如果为 true,则比较字符时忽略大小写。

public class Test {
    public static void main(String args[]) {
        String Str1 = new String("www.runoob.com");
        String Str2 = new String("runoob");
        String Str3 = new String("RUNOOB");

        System.out.print("返回值 :" );
        System.out.println(Str1.regionMatches(4, Str2, 0, 5));

        System.out.print("返回值 :" );
        System.out.println(Str1.regionMatches(4, Str3, 0, 5));

        System.out.print("返回值 :" );
        System.out.println(Str1.regionMatches(true, 4, Str3, 0, 5));
    }
}
返回值 :true
返回值 :false
返回值 :true

1.6 String.compareTo()方法

  1. 如果参数字符串等于此字符串,则返回值 0;
  2. 如果此字符串小于字符串参数,则返回一个小于 0 的值,返回两个字符串的长度差值;
  3. 如果此字符串大于字符串参数,则返回一个大于 0 的值,返回两个字符串的长度差值。
public class Test {
 
    public static void main(String args[]) {
        String str1 = "Strings";
        String str2 = "Strings";
        String str3 = "Strings123";
 
        int result = str1.compareTo( str2 );
        System.out.println(result);
      
        result = str2.compareTo( str3 );
        System.out.println(result);
     
        result = str3.compareTo( str1 );
        System.out.println(result);
    }
}
0
-3
3

1.7 String.contains() 方法

public class Main {
    public static void main(String[] args) {
        String myStr = "Runoob";
        System.out.println(myStr.contains("Run"));
        System.out.println(myStr.contains("o"));
        System.out.println(myStr.contains("s"));
    }
}
true
true
false

1.8 String.indexOf()方法

public class Main {
    public static void main(String args[]) {
        String string = "aaa456ac";  
        //查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.  
        System.out.println(string.indexOf("b")); // indexOf(String str); 返回结果:-1,"b"不存在  
 
        // 从第四个字符位置开始往后继续查找,包含当前位置  
        System.out.println(string.indexOf("a",3));//indexOf(String str, int fromIndex); 返回结果:6  
 
        //(与之前的差别:上面的参数是 String 类型,下面的参数是 int 类型)参考数据:a-97,b-98,c-99  
 
        // 从头开始查找是否存在指定的字符  
        System.out.println(string.indexOf(99));//indexOf(int ch);返回结果:7  
        System.out.println(string.indexOf('c'));//indexOf(int ch);返回结果:7  
 
        //从fromIndex查找ch,这个是字符型变量,不是字符串。字符a对应的数字就是97。  
        System.out.println(string.indexOf(97,3));//indexOf(int ch, int fromIndex); 返回结果:6  
        System.out.println(string.indexOf('a',3));//indexOf(int ch, int fromIndex); 返回结果:6  
    }
}
-1
6
7
7
6
6
public class Test {
    public static void main(String args[]) {
        String Str = new String("菜鸟教程:www.runoob.com");
        String SubStr1 = new String("runoob");
        String SubStr2 = new String("com");
 
        System.out.print("查找字符 o 第一次出现的位置 :" );
        System.out.println(Str.indexOf( 'o' ));
        System.out.print("从第14个位置查找字符 o 第一次出现的位置 :" );
        System.out.println(Str.indexOf( 'o', 14 ));
        System.out.print("子字符串 SubStr1 第一次出现的位置:" );
        System.out.println( Str.indexOf( SubStr1 ));
        System.out.print("从第十五个位置开始搜索子字符串 SubStr1 第一次出现的位置 :" );
        System.out.println( Str.indexOf( SubStr1, 15 ));
        System.out.print("子字符串 SubStr2 第一次出现的位置 :" );
        System.out.println(Str.indexOf( SubStr2 ));
    }
}
查找字符 o 第一次出现的位置 :12
从第14个位置查找字符 o 第一次出现的位置 :17
子字符串 SubStr1 第一次出现的位置:9
从第十五个位置开始搜索子字符串 SubStr1 第一次出现的位置 :-1
子字符串 SubStr2 第一次出现的位置 :16

1.9 String.substring()方法

beginIndex – 起始索引(包括), 索引从 0 开始。

endIndex – 结束索引(不包括)。

public class RunoobTest {
    public static void main(String args[]) {
        String Str = new String("This is text");
 
        System.out.print("返回值 :" );
        System.out.println(Str.substring(4) );
 
        System.out.print("返回值 :" );
        System.out.println(Str.substring(4, 10) );
    }
}
返回值 : is text
返回值 : is te

1.10 String.trim()方法

删除头尾空白符的字符串。

public class Test {
    public static void main(String args[]) {
        String Str = new String("    www.runoob.com    ");
        System.out.print("原始值 :" );
        System.out.println( Str );

        System.out.print("删除头尾空白 :" );
        System.out.println( Str.trim() );
    }
}
原始值 :    www.runoob.com    
删除头尾空白 :www.runoob.com

2. 字符串基本类型转换

2.1 字符串转换成其他变量

int:Integer.parseInt(Str)
double:Double.parseDouble(Str)
float:Float.parseFloat(Str)
byte:Byte.parseByte(Str)
long:Long.parseLong(Str)

eg:

String str = "123";
int str1 = Integer.parseInt(str);
String str="23.78d"
double price=Double.parseDouble(priceMess);

2.2 其他类型转换成字符串

byte:String.valueOf(byte n)
long:String.valueOf(long n)
float:String.valueOf(float n)

eg:

String str=String.valueOf(123.1232)

3. 字符串综合案例

3.1 图书检索

public class FindMess {
public static void main(String args[]) {
String mess="书名:Java程序设计,出版时间:2011.10.01,"+
"出版社:清华大学出版社,价格:29.8元,页数:389页";
if(mess.contains("程序")){//判断mess中是否含有"程序"
System.out.println("图书信息包含有\"程序\"");

}
int index=mess.indexOf(":",3);//【代码2】//mess调用indexOf(Strings)返回mess中第2个冒号的位置
String date=mess.substring(index+1,index+11);
System.out.println(date);
int pricePosition=mess.indexOf("价格");//【代码3】//mess调用indexOf(Strings)返回首次出现"价格"的位置
int endPosition=mess.indexOf("元");
String priceMess=mess.substring(pricePosition+3,endPosition);
System.out.println("图书价格"+priceMess);
double price=Double.parseDouble(priceMess);
if(price>=29){
System.out.println("图书价格"+price+"大于或等于29元");
}else{
System.out.println("图书价格"+price+"小于29元");
}
index=mess.lastIndexOf(":");//【代码4】//mess调用laseIndexOf(Strings,intstart)返回最后1个冒号的位置
endPosition=mess.lastIndexOf("页");
String pageMess=mess.substring(index+1,endPosition);
int p=Integer.parseInt(pageMess);
if(p>=360){
System.out.println("图书价格"+p+"大于或等于360");
}else{
System.out.println("图书价格"+p+"小于360");
}

String java 便利 java string怎么用_字符串

3.2 购物小票

import java.util.*;
public class FoundPrice {
 public static void main(String args[]) {
 String s=" 牛奶 :89.8 元,香肠 :12.9 元 啤酒 :69 元 巧克力 :132 元";
 String regex="[^0123456789.]" ; // 匹配非数字的正则表达识
 String digitMess=s.replaceAll(regex,"*");
 StringTokenizer fenxi=new StringTokenizer(digitMess, "*");// 创建 fenxi, 用* 做分隔标记解析 digitMess 中的单词
 int number=fenxi.countTokens(); //fenxi 调用 countTokens() 方法返回单词数量
 double sum=0;
 while(fenxi.hasMoreTokens()) {
 String str=fenxi.nextToken(); //fenxi 调用 nextToken() 方法返回单词
 System.out.println(str);
 sum=sum+Double.parseDouble(str);
 }
 System.out.println(" 购物小票中的商品种类: "+number+" 种");
 System.out.println(" 购物小票中的价格总额: "+sum+"元");
 
 }
}

String java 便利 java string怎么用_System_02


觉得不错可以点赞哦~