String基本类型原来是这么使用的(详解)

首先说一下String类在哪一个包中?
答:java.lang包中。
在java中使用String类创建一个字符串变量,字符串变量属于什么?
答:对象。
关于在java中String类对象的创建:
字符串的声明方式:String stringName;(驼峰命名法)。
字符串的创建:stringName = new String(“张三”) 或者直接给变量赋值 stringName = “张三”,这里需要注意的是当字符串用final修饰的时候,字符串不可改变变为常量
java中String类的构造方法有哪些?
答:①public String()
无参构造方法,用来创建空字符串的String对象。
例如:String str = new String();
②public String(String value)
用已知的字符串value创建一个String对象。
例如:String str1 = new String(“asdf”);
③public String(char[] value)
用char[]数组value创建一个String对象。
例如: char[] value = {“a”,“b”,“c”};
String str2 = new String(value);
注: 用value接收到的值当作属性放入刚创建好的字符串中。
④public String(char chars[], int numStart, int numEnd)
用字符数组chars[]的numStart开始到numEnd结束,创建一个String对象。
例如: char[] value = {“a”,“b”,“c”};
String str3 = new String(value, 1, 2);
注:从下标为1的地方b开始截取两个字符长度的数据存储到str3的字符串的对象中。
⑤public String(byte[] values)
用字节数组byte[]values创建一个String对象。
例如: byte[] strb = new byte[]{65,66};
String str6 = new String(strb);
注: 用strb接收到的字节数组的值当作属性放入刚创建好的新的空字符串中。
在Java类中String类常用方法:
①求字符串长度str.length();
public int length()//返回的值是字符串的长度
例如:String str = new String(“qwertyuiop”);
int strlength = str.length();
qwertyuiop的长度为10
②字符串中某一个下标的内容str.charAt();
public char charAt(int index)//返回字符串中指定的下标;注意字符串中第一个下标为0,最后一个是length()-1,不然会报数组下表越界异常。
例如:String str = new String(“qwertyuiop”);
char car = str.charAt(1);
注:car的值为w
③截取字符串substring;
java中用String类的substring()可以截取一个字符串中的一部分内容,
例如:String str = new String(“qwertyuiop”);
String str1 = str1.substring(1);
注: str1 = “wertyuiop"从下标为1开始截取
String str2 = str1.substring(1,3);
注: str2 = “wertyuiop"从下标为1开始截取截取长度为3
str2 = “wer”
④字符串的内容或值的对比
例如:String str = new String(“qwe”);
String str1 = new String(“QWE”);
boolean a = str.equals(str1);
注:.equals()对比的是文本内容,==比较的是地址,所以a=false
boolean b = str.equalsIgnoreCase(str1);
注:.equalsIgnoreCase=true下面有对这两个方法的解释
释:使用equals( )方法比较两个字符串是否相等。它具有如下的几种情况:
boolean equals(Object str)
这里str是一个用来与调用字符串(String)对象做比较的字符串(String)对象。
如果两个字符串具有相同的字符和长度,它返回true,否则返回false。这种比较是区分大小写的。
但是为了执行忽略大小写的比较,可以调用equalsIgnoreCase( )方法。当比较两个字符串时,它会认为A-Z和a-z是一样的。其一般情况如下:
boolean equalsIgnoreCase(String str)
这里,str是一个用来与调用字符串(String)对象做比较的字符串(String)对象。如果两个字符串具有相同的字符和长度忽略大小写,它也返回true,否则返回false。
⑤字符串拼接
public String concat(String str)//将参数中的字符串str连接到当前字符串的后面,效果等价于”+”。
例如:String str = “aa”.concat(“abc”).concat(“def”);
str的值在此时变为abcdef;
⑥字符串中查找单个字符
public int indexOf(int ch/String str)//用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。
public int indexOf(int ch/String str, int fromIndex)//改方法与第一种类似,区别在于该方法从fromIndex位置向后查找。
public int lastIndexOf(int ch/String str)//该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。
public int lastIndexOf(int ch/String str, int fromIndex)//该方法与第二种方法类似,区别于该方法从fromIndex位置向前查找。
例如:String str = “Hallo World”;
int a = str.indexOf(‘a’); a = 1
int b = str.indexOf(“llo”); b = 2
int c = str.indexOf(“o”,2); c = 4
int d = str.indexOf(“oooo”,2); d =-1
int e = str.lastIndexOf(“l”); e = 9
int f = str.lastIndexOf(“l”,10); f = 9
⑦字符串中字符的大小写转换
public String toLowerCase()//返回将当前字符串中所有字符转换成小写后的新串
public String toUpperCase()//返回将当前字符串中所有字符转换成大写后的新串
String str = new String(“goMAIN”);
String str1 = str.toLowerCase();//str1 = “gomain”
String str2 = str.toUpperCase();//str2 = “GOMAIN”
⑧字符串中字符的替换
public String replace(char oldChar, char newChar)//用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。
public String replaceFirst(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。
public String replaceAll(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。
例: String str = “qwertyuqwer”;
String str1 = str.replace(‘q’,‘a’);
str1 = “awertyuqwer”
String str2 = str.replace(“qwer”,“abcd”);
str2 = “abcdtyuqwer”
String str3 = str.replaceFirst(“qwer”,“abcd”);
str3 = “abcdtyuqwer”
String str4 = str.replaceAll(“qwer”,“abcd”);
str4 = “abcdtyuabcd”
⑨trim()去两边空格
String trim()//截去字符串两端的空格,但对于中间的空格不处理。
例: String str = " qw er “;
String str1 = str.trim();
int a = str.length();
a = 7
int b = str1.length();
b = 5
⑩statWith()
boolean statWith(String prefix)或boolean endWith(String suffix)//用来比较当前字符串的起始字符或子字符串prefix和终止字符或子字符串suffix是否和当前字符串相同,重载方法中同时还可以指定比较的开始位置offset。
String str = “abcdef”;
boolean a = str.statWith(“ab”);
a = true
boolean b = str.endWith(“ef”);
b = true
⑩①String[] split(String str)//将str作为分隔符进行字符串分解,分解后的字字符串在字符串数组中返回。
例: String str = “asd!qwe|zxc#”;
String[] str1 = str.split(”!|#");//str1[0] = “asd”;str1[1] = “qwe”;str1[2] = “zxc”;


作者:qq_40001362
来源:CSDN
原文:https://blog.csdn.net/qq_40001362/article/details/82186173
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/hjmlyj/article/details/83615043