Java基础二
Java基础二
1.”==”在java中的作用:
"=="是Java提供的关系运算符,可进行数值比较,用在String实际比较的是两个对象(任意的引用类型)堆内存地址数值,而equals()是String自己定义的方法,属于数值内容的比较,由于内容是可控的因素,地址是不可控的,所以只要涉及到字符串的比较,都用equals方法: public boolean equals(String str);
2.String直接赋值和构造的区别
直接赋值(Stringstr="字符串"):只会开辟一串堆内存空间,且对象可以自动入迟,已供其它对象使用;
构造方法(Stringstr=new String(" 字符串")):会开辟两块堆内存空间,并且有一块对内存将成为垃圾空间,同时产生的实例化对象不会自动入池,需要利用intern()方法手工入池;
字符串一旦声明不可修改:String类对象的引用发生改变,而字符串的的内容没有改变
3.字符与字符串
构造:publicString(char[]value);将全部的字符数组作为String的内容;
pulicString(char[]value,int offset,int count) 将部分字符数组的内容设置成字符串,设置字符数组的开始索引与使用个数;
普通:publicchar charAt(int index) 返回指定索引位置上的字符;
public char[]toCharArray();将字符串义字符数组的形式输出;
为了判断方便可以单独定义一个isNumber()的方法,返回boolean
4.字节与字符串
构造:publicString(byte[] bytes); //通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
public String(byte[] bytes, int offset, int length) //通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的 String。
普通:publicbyte[] getBytes() //使用平台的默认字符集将此String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中
public byte[] getBytes(String charsetName)throwsUnsupportedEncodingException //编码转换
5.字符串比较:
普通:publicboolean equals(String anObject) //将此字符串与指定的对象比较。当且仅当该参数不为 null,并且是与此对象表示相同字符序列的 String 对象时,结果才为 true,为区分大小写的比价,区分大小写;
public boolean equalsIgnoreCase(String anotherString)//不区分大小写的比较;
public int compareTo(String anotherString)
比较字符串的大小,该比较基于字符串中各个字符的 Unicode 值。按字典顺序将此 String 对象表示的字符序列与参数字符串所表示的字符序列进行比较。如果按字典
顺序此 String 对象位于参数字符串之前,则比较结果为一个负整数。如果按字典顺序此 String 对象位于参数字符串之后,则比较结果为一个正整数。如果这两个字符串相等,则结果为 0; compareTo 只在方法 equals(Object) 返回 true 时才返回 0。
public int compareIgnoreCase(String str) //不区分大小写比较字符串大小
6.字符串查找
普通:publicboolean contains(String s) //当且仅当此字符串包含指定的 char 值序列时,返回 true,否则返回false
public int indexOf(String str) //如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回 -1。
public int indexOf(String str,int fromIndex) //指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始,找不到返回-1;
public int lastIndexOf(String str) //如果字符串参数作为一个子字符串在此对象中出现一次或多次,则返回最后一个这种子字符串的第一个字符。如果它不作为一个子字符串出现,则返回 -1。
public int lastIndexOf(String str,int fromIndex)//指定子字符串在此字符串中最后一次出现处的索引,无则返回-1;
public boolean startsWith(String str) //测试此字符串是否以指定的字符串开始,有则ture,无则False
public boolean endsWith(String str,int toffset) //测试从指定位置判断此字符串是否以指定的字符串结束,有则ture,无则False
7.字符串截取:
普通:publicString substring(int beginIndex)//返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。
public String substring(int beginIndex,int endIndex)//返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。
8.字符串的替换
普通:publicString replaceAll(String regex,String replacement)//使用给定的replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
regex - 用来匹配此字符串的正则表达式
replacement - 用来替换每个匹配项的字符串
public String replaceFirst(String regex,String replacement) //替换第一个内容
9.字符串的拆分
普通:publicString[] split(String regex)//全拆分根据给定正则表达式的匹配拆分此字符串。
public String[] split(String regex,int limit)//根据匹配给定的 正则表达式来拆分此字符串。
regex - 定界正则表达式
limit - 结果阈值,如上所述
10.其它操作方法:
普通:public intlength() //取得字符串的长度;Java使用Unicode编码,中文与英文的长度相同;
public boolean isEmpty()//如果 length() 为 0,则返回 true;否则返回 false。
public String toLowerCase()//使用默认语言环境的规则将此 String 中的所有字符都转换为小写。这等效于调用 toLowerCase(Locale.getDefault())。
public String toUpperCase()//使用默认语言环境的规则将此 String 中的所有字符都转换为大写
public String trim()//去掉字符串前面和后面空白,但中间不去掉
public String concat(String str)//将指定字符串连接到此字符串的结尾,eg."cares".concat("s") returns "caress"
11.Java运算符
算术运算符
关系运算符:==,!=,
位运算符:&,|,^,~
逻辑运算符:&&,||,!
赋值运算符
条件运算符:(?:):
12.this关键字
当方法中参数和成员变量相同时,成全变量可用this.属性调用本类属性;
this()调用本类无参构造方法,必须放在构造方法首行;
13.程序举例
public class Demo{
publicstatic void main(String[] args)
{
Stringstr1="hello";
Stringstr="HELLO";
System.out.println(str1.equals(str));
System.out.println(str1.equalsIgnoreCase(str));
charc=str1.charAt(2); //返回指定索引位置上的字符;
System.out.println(c+"\n");
char[]data1=str1.toCharArray(); //将字符串已字符数组的形式输出;
for(inti=0;i<data1.length;i++){
System.out.println(data1[i]);
}
//publicString(char[]value); //将全部的字符数组作为String的内容;
char[]data2={'h','e','l','l','o'};
Stringstr2=new String(data2,1,3); //将部分字符数组的内容设置成字符串,设置字符数组的开始索引与使用个数;
System.out.println(str2);
//public String(byte[] bytes); //通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
byte []byte1={97,98,99,100,101,102};
String str3=new String(byte1);
System.out.println(str3);
String str4=new String("HELLO WORLD!");
System.out.println(str4);
}
}

浙公网安备 33010602011771号