Java中的String类方法

String类是我们在Java中十分常用的类,String类中有许多方法需要我们去掌握。

String类的概念
多个字符组成的字符序列,也可以理解为字符数组。
特点
1.Java程序中的所有字符串字面值如(“abc”)都作为此类的实例实现。
2.字符串是常量,它们的值在创建之后不能更改;字符串缓冲区支持可变的字符串,因为String对象是不可变的,所以可以共享。
构造方法

String s = "好好学习";
s += "天天向上";	
System.out.println(s);//好好学习天天向上

//public String(String original)
String s = new String("abc");
System.out.println(s3);//abc

// public String(char[] value)拼接输出
char[] chs = {'a', 'b', 'c', 'd'};
String s1 = new String(chs);
System.out.println(s1);//abcd

char[] chs2 = {'1', '2', '3', '4'};
String s2 = new String(chs2);
System.out.println(s2);//1234

// public String(char[] value, int offset, int count)选择部分拼接输出
char[] chs3 = {'a', 'b', 'c', 'd'};
String s3 = new String(chs3, 0, 2);//左闭右开
System.out.println(s6);//ab

// public String(byte[] bytes)转换输出
System.out.println(new String(new byte[] {97, 98, 99}));//abc
System.out.println(new byte[]{65 ,66 ,67});//[B@7852e922

// public String(byte[] bytes, int offset, int length)//左闭右开
System.out.println(new String(new byte[] {65,66,67,68,69},0,4 ));//ABCD
//从下标为0的元素开始到下标为4的元素结束,[0,4)
System.out.println(new String(new byte[] {97,98,99,100,101,102},1,4));//bcde

String s4 = "helloword";
byte[] by = s4.getBytes();
System.out.println(Arrays.toString(by));//[104, 101, 108, 108, 111, 119, 111, 114, 100]
for (byte c : by) {
			System.out.print((char)c);
		}//遍历输出helloword

//		Character.isLowerCase(ch);//判断是否小写
//		Character.isDigit(ch);//判断是否数字
//		Character.isUpperCase(ch);//判断是否大写
//返回值为boolean类型

String str = "ABCdE23fg157";
		// 定义统计变量
		int bigCount = 0;
		int smallCount = 0;
		int digitCount = 0;
		//遍历字符串获取到每一个字符
		for (int i = 0; i < str.length(); i++) {
			char ch = str.charAt(i);
			if (Character.isLowerCase(ch)) {
				smallCount++;
			} else if (Character.isUpperCase(ch)) {
				bigCount++;
			} else if (Character.isDigit(ch)) {
				digitCount++;
			}
		}
		System.out.println("大写字母有:" + bigCount + "个");
		System.out.println("小写字母有:" + smallCount + "个");
		System.out.println("数字字符有:" + digitCount + "个");
//大写字母有:4个
//小写字母有:3个
//数字字符有:5个
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值