1.字符串与字符
在jdk1.9之前提供string数组实现了包装处理,在String类里面提供相应的转换处理方法,这些方法包含构造方法与普通方法两类。
public String(char[] value) | (构造)将全部传入的字符数组变为字符串 |
---|---|
public String(char[] value,int offset, int count) | (构造)将部分传入的字符数组变为字符串 |
public char charAt(int index) | (普通)获取指定索引位置的字符 |
public char[] toCharArray() | (普通)将字符串中的数据以字符数组返回 |
public class Str {
public static void main(String args[]) {
String a="123.456.789";
//获取指定索引位置的字符
char b=a.charAt(5);
System.out.println(b);
//以数组形式返回字符串的数值
char c []=a.toCharArray();
for(char x:c) {
System.out.print(x+"、");
}
System.out.println();//换行
//将全部字符数组变成字符串
String d=new String(c);
System.out.println(d);
//将部分字符数组变为字符串
String e=new String(c,0,6);
System.out.println(e);
}
}
输出:
5
1、2、3、.、4、5、6、.、7、8、9、
123.456.789
123.45
例子:判断某一字符串中数据是否全部为数字组成。
·如果想判断字符串中的每一位,最好将其变为字符数组;
·可以判断每一个字符是否在数字范围之内(‘0’~’9’);
·如果有一位不是数字,验证失败。
public class Str{
public static void main (String args []){
String a ="hellojava";
System.out.println(isNumber(a) ? "是" :"否");
System.out.println(isNumber("123456") ? "是" :"否");
}
//该方法用于判断字符串由数字组成
public static boolean isNumber(String a){
char [] result=a.toCharArray();//字符串变为字符数组输出
for (int x=0; x<result.length; x++){
if(result[x]<'0' ||result[x]>'9'){
return false;
}
}
return true;
}
}
输出结果:
否
是
2.字符串与字节
public class Str {
public static void main(String args[]) {
String a="hellojava";
//字符串转换成字节数组
byte b []=a.getBytes();
for(int x=0;x<b.length;x++) {
b[x]-=32;
System.out.print(b[x]+"、");
}
System.out.println();//换行
//将全部字节数组变成字符串
String d=new String(b);
System.out.println(d);
//将部分字节数组变为字符串
String e=new String(b,0,6);
System.out.println(e);
}
}
输出:
72、69、76、76、79、74、65、86、65、
HELLOJAVA
HELLOJ
3.字符串比较
字符串比较常用的是equals()方法,比较时要注意大小写。
例子:区分大小写相等判断(普通方法),public boolean equals(String anObject)
public class Str{
public static void main (String args []){
String a ="hello";
String b ="HELLO";
System.out.println(a.equals(b));//false
}
}
例子:不区分大小写相等判断(普通方法),public boolean equalsIgnoreCase(String anotherString)
public class Str{
public static void main (String args []){
String a ="hello";
String b ="HELLO";
System.out.println(a.equalsIgnoreCase(b));//ture
}
}
其他比较方式:
·进行字符串大小比较,该方法返回一个int数据(普通方法):public int compareTo(String another),该数据有三种取值:<0;>0;=0。
·不区分大小写进行字符串大小比较(普通方法):public int compareTognoreCase(String Str)
例子:
public class Str{
public static void main (String args []){
String a ="hello";
String b ="HELLO";
System.out.print(a.compareTo(b)+",");
//忽略大小写比较,此时两者内容一样,再不计较大小写情况下比较结果相同。
System.out.print(a.compareToIgnoreCase(b)+",");
System.out.print("hello".compareTo(b));
}
}
输出结果:32,0,32
4.字符串查找
例子:
public class Str{
public static void main (String args []){
String a ="hello,java";
System.out.print(a.contains("hello")+"、");
System.out.print(a.indexOf("java")+"、");
System.out.print(a.indexOf("j",3)+"、");
System.out.print(a.lastIndexOf("h")+"、");
System.out.print(a.lastIndexOf("h",3)+"、");
System.out.print(a.startsWith("java")+"、");
System.out.print(a.startsWith("java",6)+"、");
System.out.print(a.endsWith("java"));
}
}
输出:true、6、6、0、0、false、true、true
5.字符串替换
public class Stra{
public static void main (String args []){
String a ="hello";
System.out.println(a.replaceAll("l","p"));//heppo
System.out.println(a.replaceFirst("l","p"));//heplo
}
}
输出:
heppo
heplo
6.字符串拆分
public class Stra{
public static void main (String args []){
String a ="12 3 456 78 9";
String b []=a.split(" ");//空格拆分,全部拆分:12、3、456、78、9、
for (int x=0;x<b.length ;x++ ){
System.out.print(b[x]+"、");//12、3、456、78、9、
}
System.out.print("**");
String c []=a.split(" ",2);//按照空格拆分2次:12、3 456 78 9、
for (int x=0;x<c.length ;x++ ){
System.out.print(c[x]+"、");
}
System.out.print("**");
String d []=a.split(" ",4);//按照空格拆分4次:12、3、456、78 9、
for (int x=0;x<d.length ;x++ ){
System.out.print(d[x]+"、");
}
System.out.print("**");
String e []=a.split(" ",300);//按照空格拆分300次:12、3、456、78、9、
for (int x=0;x<e.length ;x++ ){
System.out.print(e[x]+"、");
}
}
}
说明:当拆分次数大于实际次数时候,只会按照拆分位置以最大的次数拆分。
7.字符串截取
public class Stra{
public static void main (String args []){
String a ="123456789";
System.out.println(a.substring(2));//3456789
System.out.println(a.substring(2,5));//345
}
}
输出:
3456789
345
8.字符串结构:indexOf(String str)
public class Stra{
public static void main (String args []){
String a ="12-34-546789";
int begin=a.indexOf("-",a.indexOf("4"))+1;
int end =a.lastIndexOf("7");
System.out.println(a.substring(begin,end));//546
}
}
输出:546
9.字符串格式化
JAVA提供格式化处理操作,通过占位符实现。常用的有:字符串(%s)、字符(%c)、数字(%d)、小数(%f)
public class Stra{
public static void main (String args []){
String name="张三丰";
int age=19;
double credit=98.9876;
String a =String.format("姓名:%s、年龄:%d、成绩:%5.2f",name,age,credit);
//姓名:张三丰、年龄:19、成绩:98.99(共五位,小数两位)
System.out.println(a);//546
}
}
10.其他操作方法
1·public String concat(String str):字符串连接
2·public String intern():字符串入池
3·public boolean isEmpty():判断是否为空字符串(不是null),””表示实例化对象,null没有实例化对象。
4·public int length():计算字符串长度
5·public String trim():驱除左右空格信息,不能消除中间空格。
6·public String toUpperCase():转大写
7·public String toLowerCase():转小写
11.字符串首字母大写转换方法
思路:
截取字符串首位,进行判断是否为大写字母;
如果是大写字母则直接输出字符串;
如果不是大写字母则转为大写字母,然后截取首位之后的部分与其相加。
根据这个思路可以完成任意字符串任意位置的字母大小写转换。
class A{
public static String aa(String a){
if(a==null || "".equals(a)){ //判断字符串是否为空
return "字符串为空";
}
else if (a.length()== 1){ //若非空,判断字符串是否只有一位
return a.toUpperCase();
}
else{ //若非一位,执行以下
return a.substring(0,1).toUpperCase()+a.substring(1);
}
}
}
public class zhuan{
public static void main (String args []){
System.out.println(A.aa("hello"));//Hello
System.out.println(A.aa("m"));//M
}
}
输出:
Hello
M