前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【蓝桥杯Java_C组·从零开始卷】第四节(附)、字符串常用函数

【蓝桥杯Java_C组·从零开始卷】第四节(附)、字符串常用函数

作者头像
红目香薰
发布2022-11-29 18:27:05
2120
发布2022-11-29 18:27:05
举报
文章被收录于专栏:CSDNToQQCode

导读

本文章将java中字符串常用的字符串进行罗列与对应demo的示例,帮助java初学者与蓝桥杯参赛的选手提升对JavaSE的理解。

目录

字符串由来

字符串转成byte数组

常用字符串函数列表:

substring

replace

trim

toCharArray

toLowerCase与toUpperCase

indexOf

split

字符串由来

通过以下编码可以看出,字符串实际就是字符数组。

代码语言:javascript
复制
char chars[]={'a','b','c'};
String s=new String(chars);
System.out.println(s);
int len=s.length();
System.out.println(len);

字符串转成byte数组

代码语言:javascript
复制
String s = "Hello world";
byte[] bytes = s.getBytes();
for (byte b : bytes) {
	System.out.print((char)b);
}

常用字符串函数列表:

length()//取得字符串的长度 substring()//字符串截取 concat() //连接两个字符串 replace()//替换 trim()//去掉起始和结尾的空格 valueOf()//转换为字符串 toLowerCase()//转换为小写 toUpperCase()//转换为大写 toCharArray()//转char数组 equals()//比较两个字符串区分大小写 equalsIgnoreCase()//比较两个字符串不区分大小写 indexOf()//查找字符或者子串第一次出现的地方 lastIndexOf()//查找字符或者子串是后一次出现的地方 split()//字符串分割

substring

代码语言:javascript
复制
package Action;

public class demo {
	public static void main(String[] args) {
		String str="I HAVE A DREAM!";
		String s = str.substring(2, 2+4);
		System.out.println(s);
	}
}

replace

代码语言:javascript
复制
package Action;

public class demo {
	public static void main(String[] args) {
		String str="I HAVE A DREAM!";
		String s = str.replace("DREAM", "GOOD IDEA");
		System.out.println(s);
	}
}

trim

代码语言:javascript
复制
package Action;

public class demo {
	public static void main(String[] args) {
		String str="\tI HAVE A DREAM!\t";	
		String s = str.trim();
		System.out.println(s);
	}
}

toCharArray

代码语言:javascript
复制
package Action;

public class demo {
	public static void main(String[] args) {
		String str = "89dsa dady8)ILuhd9usa)(*YGIUhdusa hoi";
		char[] array = str.toCharArray();
		int low = 0;
		int up = 0;
		int num = 0;
		int other = 0;
		for (char c : array) {
			if (c >= 'a' && c <= 'z') {
				low++;
			} else if (c >= 'A' && c <= 'Z') {
				up++;
			} else if (c >= '0' && c <= '9') {
				num++;
			} else {
				other++;
			}
		}
		System.out.println(str.length());
		System.out.println(low);
		System.out.println(up);
		System.out.println(num);
		System.out.println(other);
	}
}

toLowerCase与toUpperCase

代码语言:javascript
复制
package Action;

public class demo {
	public static void main(String[] args) {
		String str="I HAVE A DREAM!";
		String lowerCase = str.toLowerCase();
		System.out.println(lowerCase);
		String upperCase = str.toUpperCase();
		System.out.println(upperCase);
	}
}

indexOf

代码语言:javascript
复制
package Action;

import java.util.UUID;

public class demo {
	public static void main(String[] args) {
		String str = UUID.randomUUID().toString().replaceAll("-", "");
		String fileName = str.concat(".jpg");
		int indexOf = fileName.indexOf(".jpg");
		System.out.println(indexOf);
		System.out.println(fileName.substring(indexOf,fileName.length()));
	}
}

split

代码语言:javascript
复制
package Action;

import java.util.UUID;

public class demo {
	public static void main(String[] args) {
		String str = UUID.randomUUID().toString();
		System.out.println(str);
		String[] split = str.split("-",str.length());
		for (String string : split) {
			System.out.println(string);
		}
	}
}

希望能对大家有一定的帮助。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-01-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 字符串由来
  • 字符串转成byte数组
  • 常用字符串函数列表:
  • substring
  • replace
  • trim
  • toCharArray
  • toLowerCase与toUpperCase
  • indexOf
  • split
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档