4.28
来源:互联网 发布:傲剑修行升级数据 编辑:程序博客网 时间:2024/06/05 02:20
JAVA
String 类及其重要方法
字符与字符串
字符是JAVA 语言中的一种基本数据类型,即使用单引号括起来的单个字符。字符串常量是用双引号括起来的多个字符
JAVA语言中,字符串对象一旦被创建,它的值就永远不能再改变,
String 类
String() 初始化一个新创建的String对象,它表示一个空字符序列
String(byte[] byte) 构造一个新的String对象,方法是使用平台的默认字符集解码字节的指定数组
String(byte[] byte,String charsetName) 构造一个新的String对象,方法是使用指定的字符集解码指定的字节数组
String(char[] value) 创建一个新的String 对象,表示当前字符数组参数中包含的字符序列
String(StringBuffer buffer) 创建一个新的字符串,包含当前在字符串缓冲区参数中的字符序列
字符串对象的创建
String str="This is a String";
或者是
String str=new String("This is a String");
上述两条语句都是创建一个内容为 This is a String 的字符串对象,但是 不是指向同一个对象的引用,
public class StringTest1 {
public static void main(String args[]) {
String s1="aaaa";
String s2=new String("aaaa");
if(s1==s2) {
System.out.println("s1,s2是同一个对象! ");
} else {
System.out.println("s1,s2不是同一个对象! ");
}
}
}
该示例首先声明了两个具有相同内容的字符串对象,然后通过if 语句判断这两个对象是否相等:
编译运行后得到:
F:\gz\JAVA\zfc>java StringTest1
s1,s2不是同一个对象!
可以看出比较两个引用返回的是 false, 即 s1 和s2 两个字符串对象虽然都相同,但是没有指向同一个对象。这里产生了两个字符串对象,所以等号只能用于比较两个引用是否指向同一个对象。
如果要比较两个对象的内容是否相同,则应该使用 equals() 方法
public class StringTest2 {
public static void main(String args[]) {
String s1="aaaa";
String s2=new String("aaaa");
if(s1.equals(s2)) {
System.out.println("s1,s2中的内容相同! ");
} else {
System.out.println("s1,s2中的内容不相同! ");
}
if(s2.equals(s1)) {
System.out.println("s1,s2中的内容相同! ");
} else {
System.out.println("s1,s2中的内容不相同! ");
}
}
}
首先声明两个具有相同内容的字符串对象,然后通过if 语句判断这两个对象是否相等,不同的是,使用的是 equals()方法而不是等号
编译运行后得到结果:
F:\gz\JAVA\zfc>java StringTest2
s1,s2中的内容相同!
s1,s2中的内容相同!
①equals() 方法只能用于比较对象
②equals() 方法的返回值非 true 即 false.
字符串对象一旦被创建将永远不能被改变:
public class StringTest3 {
public static void main(String args[]) {
//创建字符串对象
String s="StringObject";
//将所有字母都转换为大写
s.toUpperCase();
//打印s中的字符串内容
System.out.println("s="+s);
}
}
声明一个字符串对象,并调用对象上的 toUpperCase()方法将字母转换为大写,并打印字符串内容
编译运行结果如下:
F:\gz\JAVA\zfc>java StringTest3
s=StringObject
String 类的主要方法
① startsWith() 和 endsWith()方法
测试字符串是否以指定的前缀开始或结束
②public Boolean regionMatches(int firstStart,String other,int ortherStart,int length)方法
从当前字符串参数firstStart 指定的位置开始,取长度为 length 的一个子串,并将这个子串和参数other 指定的一个子串进行比较。其中 other 指定的子串是从参数 otherStart指定的位置开始,从 other 中取长度为 length 的一个子串,如果相同,就返回 true 否则返回 false
③public int indexOf(String s)方法
字符串调用该方法从当前字符串的头开始检索(从0开始计算)字符串s,并返回首次出现s 的位置,如果没有检索到字符串s ,该方法的返回值是-1
④public String substring(int start,int end)方法
字符串对象调用该方法获得一个当前字符串的子串,该子串是从当前字符串的star 处截取到end 处所得到的字符串,但不包括end 处所对应的字符
⑤public String concat(String str)方法
将指定字符串连到调用此方法的字符串对象的结尾
⑥public String replace(char oldChar,Char newChar)方法
返回一个新的字符串,通过用newChar 替换调此 replace()方法的字符串中出现的所有 oldChar而生成的
⑦public int length()方法
返回此字符串的长度,长度等于字符串中的16位Unicode字符数
⑧public boolean matches(String regex)方法
通知此字符串是否匹配给定的正则表达式。此方法调用的 str.matches(regex)形式与 Pattern.matches(regex,str)表达式产生完全相同的结果
⑨public String[] split(String regex)方法
根据给定的字符来拆分此字符串
//以逗号分隔
String s1="St,rin,g0b,je,ct;
//调用split(String regex方法
String[] s2=s1.split(",");
//循环打印
for(String ss:s2) {
System.out.println(ss);
}
程序的运行结果为:
St
rin
g0b
je
ct
⑩public String toUpperCase()方法
使用默认语言环境的规则,将此字符串中的所有字符都转换成大写
⑪public String toLowerCase()方法
使用默认语音环境的规则,将此字符串中的所有字符都转换为小写
⑫public String trim()方法
返回字符串的副本,忽略前导空白和尾部空白:
String s=" StringObject ";
System.out.println(s.trim());
程序的运行结果为:
StringObject
StringBuffer 类及其方法
StringBuffer 类用于创建和操作动态字符串信息,为该对象分配的内存会自动扩展以容纳新增的文本,从而避免了进行字符串连接操作时产生的垃圾对象
StringBuffer() 构造一个空的StringBuffer对象,初始容量为16个字符
StringBuffer(int capacity) 构造一个指定容量的StringBuffer对象
StringBuffer(String str) 构造一个初始值为 str 的StringBuffer对象
该类位于java.lang 基础包中,所以可以直接在程序中使用。一旦创建了StringBuffer类的对象,就可以使用StringBuffer 类提供的大量方法对字符串进行操作
最常用的为 append()方法
该方法将字符串文本追加到当前StringBuffer对象内容的结尾,但不会产生新的字符串对象
public class StringBufferTest {
public static void main(String args[]) {
StringBuffer strBuf=new StringBuffer();
strBuf.append("67890");
System.out.println("\""+ strBuf +"\" length: "+
strBuf.length()+",capacity: "+strBuf.capacity());
strBuf.insert(0,"12345");
System.out.println("\""+ strBuf +"\" length: "+
strBuf.length()+",capacity: "+strBuf.capacity());
strBuf.append("1234567");
System.out.println("\""+ strBuf +"\" length: "+
strBuf.length()+",capacity: "+strBuf.capacity());
}
}
以上代码声明了一个StringBuffer类的对象,然后调用append()方法并打印StringBuffer类的对象的内同
编译运行后得到结果:
F:\gz\JAVA\zfc>java StringBufferTest
"67890" length: 5,capacity: 16
"1234567890" length: 10,capacity: 16
"12345678901234567" length: 17,capacity: 34
StringBuffer 类的主要方法
通过StringBuffer对象的delete()方法可以对字符串进行截取操作:
public class StringBufferTest1 {
public static void main(String args[]) {
String str="this si original string";
String toDelete=" original";
System.out.println("Original string: \""+str+"\"");
System.out.println("String to be deleted: \""+toDelete+"\"");
int index=str.indexOf(toDelete);
if(index==-1) {
System.out.println("No string matched!");
} else {
StringBuffer bufStr=new StringBuffer(str);
bufStr.delete(index,index+toDelete.length() );
str=bufStr.toString();
System.out.println("New string: \""+str+"\"");
}
}
}
编译运行结果为:
F:\gz\JAVA\zfc>java StringBufferTest1
Original string: "this si original string"
String to be deleted: " original"
New string: "this si string"
Pattern类和 matcher 类
①static Pattern compile(String regex) 按照模式编译给出的正则表达式的字符串
②static boolean matches(String regex,CharSequence input) 编译给定的正则表达式,并且对输入的字串以该正则表达式为模开展匹配,该方法适合于该正则表达式只使用一次的情况,也就是只进行一次匹配,因为这种情况下并不需要生成一个Matcher实例
③String[] split(CharSequence nput) 将目标字符串以模式里所包含的正则表达式为模进行分割
Pattern 类是一个正则表达式经编译后的表现模式:
String re="[a-z]";
Pattern pattern=Pattern.compile(re);
对以上三个方法举例说明:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternTest {
public static void main(String args[]) {//匹配小写字母
Pattern pattern = Pattern.compile("[a-z]+");
String[] arrStr=pattern.split("Matcher,Pattern,String,System");
int length=arrStr.length;
for(int i=0;i<length;i++) {
System.out.println(arrStr[i]);
}
System.out.println("======");
Matcher matcher = pattern.matcher("sun.java.com");
while (matcher.find()) {
System.out.println("Match: "+matcher.group());
}
}
}
输出结果如下:
F:\gz\JAVA\zfc>java PatternTest
M
,P
,S
,S
======
Match: sun
Match: java
Match: com
import java.util.regex.*;
public class PatternTest1 {
public static void main(String args[]) {
Pattern patt = Pattern.compile("(\\w+)\\s(\\d+)");
Matcher matcher = patt.matcher("Orang 12");
matcher.lookingAt();
System.out.println("Name: " + matcher.group(1));
System.out.println("Number: " + matcher.group(2));
}
}
结果:
F:\gz\JAVA\zfc>java PatternTest1
Name: Orang
Number: 12
JAVA 数学运算
java.lang.Math 类
java.lang 包中定义的所有类都是 Java语言的基础,在程序中不用显示的声明导入,
Math类也包含在此包中,它用于执行基本的数学运算。
java.lang 包中也定义了两个数学常量PI 和E:
public static final double E
public static final double PI
public class MathPIE {
public static void main(String args[]) {
System.out.println("E = " + Math.E);
double radians = 0.45 * Math.PI/180;
System.out.println("cos = " + Math.cos(radians));
}
}
运行结果:
F:\gz\JAVA\zfc>java MathPIE
E = 2.718281828459045
cos = 0.9999691576447897
Math类中的所有方法都是静态的,因此在使用的时候不必创建实例,而且也不能创建对象,因为其构造函数是私有的,任何类也不能扩展自 Math 类,因为它是最终类
public class BigIntegerTest {
public static void main(String args[]) {
BigInteger bi1=new BigInteger("112233");
BigInteger bi2=new BigInteger("445566");
//加法
System.out.println("bi1+bi2= "+bi1.add(bi2));
//减法
System.out.println("bi1-bi2= "+bi2.subtract(bi1));
//乘法
System.out.println("bi1*bi2= "+bi1.multiply(bi2));
//除法
System.out.println("bi1/bi2= "+bi2.divide(bi1));
//求余
System.out.println("bi1%bi2= "+bi2.mod(bi1));
//判断bi3的符号
BigInteger bi3=bi1.subtract(bi2);
if(bi3.signum()==1) {
System.out.println("bi3为整数! ");
} else {
System.out.println("bi3为负数! ");
}
}
}
bi1+bi2= 557799
bi1-bi2= 333333
bi1*bi2= 50007208878
bi1/bi2= 3
bi1%bi2= 108867
bi3为负数!
public class BigDecimalTest {
public static void main(String args[]) {
BigDecimal bd1=new BigDecimal("8.8");
BigDecimal bd2=new BigDecimal("0.6");
//加法
System.out.println("bd1+bd2= "+bd1.add(bd2));
//减法
System.out.println("bd1-bd2= "+bd2.subtract(bd1));
//乘法
System.out.println("bd1*bd2= "+bd1.multiply(bd2));
//除法
System.out.println("bd1/bd2= "+bd2.divide(bd1,BigDecimal.ROUND_HALF_DOWN));
bd2=new BigDecimal("8.80");
if(bd1.compareTo(bd2)==0) {
System.out.println("bd1和bd2相等! ");
}
}
}
bd1+bd2= 9.4
bd1-bd2= -8.2
bd1*bd2= 5.28
bd1/bd2= 0.1
bd1和bd2相等!
- 4.28
- 4.28
- 4.28
- 4.28
- 4.28
- 4.28
- 4.28
- 4.28
- 4.28
- 4.28
- 4.28
- 4.28
- 4.28学习
- 2011/4.28
- 4.28总结
- 4.28总结
- 4.28练习
- update 4.28
- MQ的使用场景
- MiniXml
- android 5.1 如何打开 android.software.managed_users 功能
- 安卓基础TextView跑马灯效果简单实现
- 设计模式之【开篇】
- 4.28
- ubunt16.04安装awscli
- windows 7架设OpenSSH服务器实践
- Java Web自定义MVC框架详解
- neural networks学习笔记(二)
- Oracle 11g 开启归档模式
- 利用局域网,传输文件
- ZOJ3959-Problem Preparation
- 在Java中直接调用js代码