字符串

字符串

目录

声明
创建
连接
查找
替换
分隔

字符串的声明

String s;

字符串的创建
String str1,str2;
str1 = "we are student";
str2 = "we are student";
String s = new String("student");

  • 字符串的使用

连接多个字符串

String s1 =new String(“Hello”);
String s2 =new String(“World”);
String s =s1+”“+s2;

字符串查找

字符串长度

String str = “we are student”;
int size = str.length();

字符串查找

String str = “we are student”;
int size = str.indexOf(“a”); //变量size的为3

charAt()方法可以将指定索引处的字符返回

str.charAt(int index);//str 指任意字符串

substring()方法可对字符串进行截取

str.substring(int beginIndex);//beginIndex指定从某一处开始截取
substring(int “开始的位置” int “结束的位置”);

字符串的替换

str.replace(char oldChar,char newChar)

oldChar指要替换的字符或字符串
newChar指用于替换原来的字符串的内容

判断字符串的开始和结尾

str.startsWith();
str.endsWith();

判断字符串是否相等equals()

str.equals(String otherstr)//str otherstr是指参加比较的两个字符串

字符串分隔,大小写转换

str.toLowerCase(); 转换成小写字母
str.toUpperCase();转换成大写字母

拆分字符串
split();

package Arrays;

public class Test_5 {
    public static void main(String[] args) {
        String str= new String("abc,def,ghi,gkl");
        //使用split()方法对字符串进行拆分,返回字符数组
        String[]newstr= str.split(",");
        for (int i = 0; i < newstr.length; i++) {
            System.out.println(newstr[i]);
        }
        //对字符串进行拆分,并限定拆分次数
        String[]newstr2=str.split(",",2);
        for (int j = 0; j < newstr2.length; j++) {
            System.out.println(newstr2[j]);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值