java2实用教程 第七章 常用实用类 复习

String类

final类,不能扩展
1.常量对象:放入常量池
“你好”
2.String对象:动态区,非常量池,堆
String s = new String("你好“);//s存放引用
构造方法:
char a[]={‘J’,‘A’,‘V’,‘A’};

String s = new String(a);

or

String s = new String(a,0,1);//J

3.引用常量对象
String s,s2;
s = “你好”;
s2 = “你好”;
s,s2具有相同引用s==s2为true

String常用方法
  • public int length();
  • public bolean equals();
  • public boolean startsWith(String s);
  • public int compare(String s)
  • public Booleancontains(String s)
  • public int indexOf(String s)/lastIndexOf(String s)
  • public String subString(int start,int end)
  • public String trim()
  • public void getChars(int start,int end,cha ch[],int offset)//将当前String对象字符串序列一部分复制到c数据中
  • public char[] toCharArray()

-public byte[] getBytes(String charsetName)//使用参数指定字符编码

字符串和数据类型基本转换

Integer.parseInt();
String.valueOf()

String、String 、BuilderStringBuffer区别

String 不可变字符串(如果操作少量数据)
String Builder 可变字符串,效率高,线程不安全(单线程操作大量数据可使用)
StringBuffer 可变字符串,效率低,线程安全(多线程操作大量数据可使用)
初始容量16,
方法:append(String s/int n/Object o),charAt(int n);setCharAt(int a,char ch);insert(int index,String str);reverse()delete(int start,int end) ;replace(int start,int end,String str)

正则表达式及字符串替换与分解

. 任何字符
\d 数字字符
\D 非数字字符
\s 空格类字符
\S
\w 可用于标识符(不包括美元)
\W
\P{Lower}

用方括号括起若干字符表示一个元字符
[abc]表示a b c任意一个
[.]或\56表示.字符
限定修饰符
X? X出现0次或1次
X* X出现0次或多次
X+ X出现1次或多次
X{n}
X{n,}
X{n,m}
XY X的后缀是Y
X|Y X或Y

import java.util.Scanner;
public class Example8_ 9 {
	public static void main (String args[ ]) {
		String regex = "[a-zA-Z|0-9|_]+";
		Scanner scanner = new Scanner (System.in) ;
		String str = scanner.nextLine () ;
		if (str.matches(regex)){
			System,out.printIn(str+"是由英文字母、数字或下画线构成");
		}
		else{
			System,out.printIn(str+"有非法字符");
		}
	}
}
字符串替换

不改变当前兑现字符序列

String s = "12hello".replaceAll("[a-zA-Z]+","你好");
字符串序列分解

public String[] split(String regex)

String digitWord[]= str.split(regex);
for(int i = 0;i<digitWord.length;i++){
	System.out.println(digitWord[i]);
}

String Tokenizer(String s)//默认分割标记
String Tokenizer(String s,String delim)
字符串分析器
nextToken() hsdMoreTokens() countTokens()

Scanner类

将正则表达式作为分割标记
Scanner scanner = new Scanner(“你好“);
s.useDelimiter(”[^012456789.]+");


class Example8_ 13{
public static void main(String args[]) (
	String cost = "市话.6.8元,长:167.38元,短信2.8元";
	double priceSum = GetPrice.givePriceSum (cost);
	system.out.printf ("&s\n总价:%.2f元\n",cost,priceSum);
	cost = "牛奶:8.5元,香蕉3.6元,酱油:2.8元";
	priceSum= GetPrice.givePriceSum (cost);
	System.out.printf ("&s\n总价:8.2f元\n",cost,priceSum);
	}
}
	

public class GetPrice(stringcost){	
	public static double givePriceSum(cost){//static方法,类名可调用
	Scanner scanner = new Scanner(cost)//scanner 设置分隔标记
	scanner.useDelimiter("[^0123456789.]+");
	double sum=0;
	while (scanner.hasNext()){
		try{
			double price = scanner.nextDouble();
			sum += price;
		}
		catch(InputMismatchException e){
			scanner.next();
		}
	}
	return sum;
}
Random类
Random random = new Random();
random.nextInt();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值