Re: String

可以在API中查看详细内容,不明白的地方去收一收解释也是很不错的,强大的语言所要具备的帮助也很实用。虽然最近流行Python人工智能什么的,但Java又不是停滞不前了,还是有很多的人在学习中,为啥老有种牙广告弹出…
String类,常用类之一吧,适合各个场景。
常见构造方法

  • public String():空构造
  • public String(byte[] bytes):把字节数组转成字符串
  • public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
  • public String(char[] value):把字符数组转成字符串
  • public String(char[] value,int index,int count):把字符数组的一部分转成字符串
  • public String(String original):把字符串常量值转成字符串

String类的判断功能

  • boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
  • boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
  • boolean contains(String str):判断大字符串中是否包含小字符串
  • boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
  • boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
  • boolean isEmpty():判断字符串是否为空。

都是一些方法,String类所提供的,面向对象拿来使用就行,不需要自己造个轮子,听说3.14号π的值取到了约31万亿位,云计算啊。
String类的获取功能

  • int length():获取字符串的长度。
  • char charAt(int index):获取指定索引位置的字符
  • int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
  • int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
  • int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
  • int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
  • lastIndexOf
  • String substring(int start):从指定位置开始截取字符串,默认到末尾。
  • String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。

来个代码吧

public class Test1 {

public static void main(String[] args) {
	String s = "ABCDEabcd123456!@#$%^";
	int big = 0;
	int small = 0;
	int num = 0;
	int other = 0;
	//1,获取每一个字符,通过for循环遍历
	for(int i = 0; i < s.length(); i++) {
		char c = s.charAt(i);						//通过索引获取每一个字符
		//2,判断字符是否在这个范围内
		if(c >= 'A' && c <= 'Z') {
			big++;									//如果满足是大写字母,就让其对应的变量自增
		}else if(c >= 'a' && c <= 'z') {
			small++;
		}else if(c >= '0' && c <= '9') {
			num++;
		}else {
			other++;
		}
	}
	
	//3,打印每一个计数器的结果
	System.out.println(s + "中大写字母有:" + big + "个,小写字母有:" + small + "个,数字字符:" 
	+ num + "个,其他字符:" + other + "个");
	}

}

String的转换功能:

  • byte[] getBytes():把字符串转换为字节数组。

  • char[] toCharArray():把字符串转换为字符数组。

  • static String valueOf(char[] chs):把字符数组转成字符串。

  • static String valueOf(int i):把int类型的数据转成字符串。

  • 注意:String类的valueOf方法可以把任意类型的数据转成字符串。

  • String toLowerCase():把字符串转成小写。(了解)

  • String toUpperCase():把字符串转成大写。

  • String concat(String str):把字符串拼接。

升下级?当然了。

  • A:StringBuffer的构造方法:
    • public StringBuffer():无参构造方法
    • public StringBuffer(int capacity):指定容量的字符串缓冲区对象
    • public StringBuffer(String str):指定字符串内容的字符串缓冲区对象
  • B:StringBuffer的方法:
    • public int capacity():返回当前容量。 理论值(不掌握)
    • public int length():返回长度(字符数)。 实际值

还有是

  • A:StringBuffer的添加功能
    • public StringBuffer append(String str):
      • 可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
    • public StringBuffer insert(int offset,String str):
      • 在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
  • B:StringBuffer的删除功能
    • public StringBuffer deleteCharAt(int index):
      • 删除指定位置的字符,并返回本身
    • public StringBuffer delete(int start,int end):
      • 删除从指定位置开始指定位置结束的内容,并返回本身
  • C:StringBuffer的替换功能
    • public StringBuffer replace(int start,int end,String str):
      • 从start开始到end用str替换
  • D:StringBuffer的反转功能
    • public StringBuffer reverse():
      • 字符串反转
  • E:StringBuffer的截取功能
    • public String substring(int start):
      • 从指定位置截取到末尾
    • public String substring(int start,int end):
      • 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置

当然开发时会用StringBuilder,功能是一样的,不安全高效率,很实用。

ps:每日一词
满庭芳·练丹炉

十九芳龄,韶华年纪,翠玉弄柳折需。目光流恋,帛逸陌上屋。细水垂平绕户,鸡鸣夜、懒日东出。还识否,中间花树,黄发小时修。

扶扶,扶骨木,扶门笑汝,扶不兮乎。几凭借秋风,一睹谁如。云乱影来故处,持敲朴,鞭使江湖。当年我,行游如梦,唤作练丹炉。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值