Java字符串的使用

String类字符串

字符串有两种类型:String类型和StringBuffer类型。
①String类型建立的字符串不能更改,适合用于需要使用字符串常数 的程序。
常见创建方式:

String s="hello";   //直接创建
String s=new String("hello"); //利用new操作符来声明

字符串常见方法:

1)length方法:s.length可以计算字符串的长度

在这里插入图片描述

3)isUpperCase&&islowerCase方法//判断字符的大小写
public class Fibonacci {
	public static void main(String[] args) {
		char str[]= {'j'};
        if (Character.isUpperCase(str[0])) 
        System.out.println(1);      
        else
        System.out.println(2);
	}
}4)toUpperCase&&tolowerCase方法//将字符化为大/小写

(5)compareTo方法//用于字符串的比较

//比较字符串的大小
eg:public class Fibonacci {
	public static void main(String[] args) {
		String temp;
		String str[]= {"VS","Java","C","Python"};
		for(int i=0;i<str.length-1;i++) 
			for(int j=i+1;j<str.length;j++) {
				if(str[i].compareTo(str[j])>0) {
					temp=str[i];
					str[i]=str[j];
					str[j]=temp;
				}
			}
		for(int k=0;k<str.length;k++)
			System.out.println(str[k]);
	}
}

(6)concat方法:

C=A.concat(B);//将B连接到A上

(7)substring方法:

B=A.substring(int begin,int end)//将字符串B的begin~end字符提取

(8)replace方法:

String s1="Javayyds";
String s2=s1.replace(J,T);//s2=Tavayyds

可以看出Java很多方法使用起来是很方便的,不用去定义函数,直接进行调用就可以实现目的。

StringBuffer类字符串

与string类型不同,StringBuffer对象的创建语法只有一种,即使用new操作符。

StringBuffer 字符串名称=new StringBuffer()

它的基本方法与上面很类似,但也有它的特殊方法。

eg:
StringBuffer A=new StringBuffer12);
StringBuffer B=new StringBuffer"abc");
StringBuffer C=new StringBuffer"def");//创建StringBuffer对象
int x=A.capacity;//方法一:计算字符串长度
A=B.append(C);//把B加到A上,输出abcdef
B=C.insert(1,B);//将B插入到C中,输出adefbc
B.delete(1,3);//删除B中索引为1-2的字符,输出a

字符串大致就是这样,有错误敬请指正。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值