java 实现截取字符串并按字节分别输出实例代码
作者:lqh 时间:2021-08-28 08:10:44
java 实现截取字符串并按字节分别输出实例代码
前言:
请编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”6,应该输出“我ABC”,而不是“我ABC”+“汉”字的半个。
2、解析思想
本题容易产生困惑的是中文字符和英文字符如何处理,在这里需要考虑汉字和英文字符的占用字节数问题,中文字符占两个字节,英文字符占一个字节,了解这个关键点后,那么编写代码就容易啦!
3、Java代码
import java.util.Scanner;
public class Interception {
static String ss; //要进行截取操作的字符串
static int n; //截取的字符串的字节数
public static void main(String[] args) {
System.out.println("请输入字符串:");
Scanner scStr = new Scanner(System.in); //从键盘获取字符串
ss = scStr.next(); //将Scanner对象中的内容以字符串的形式取出来
System.out.println("请输入字节数:");
Scanner scByte = new Scanner(System.in); //从键盘获取字符串
n = scByte.nextInt(); //将Scanner对象中的内容以数值的形式取出来
Interception(setValue()); //方法与方法间的套用
}
public static String[] setValue() { //此方法的作用是将字符串转换成字符串数组
String[] string = new String[ss.length()]; //创建一个字符数组string
for (int i = 0; i < string.length; i++) {
string[i] = ss.substring(i, i + 1); //将字符串ss中的第i个字符取出,放入字符数组中string中
}
return string; //将这个字符数组返回
}
public static void Interception(String[] string) {
int count = 0;
String m = "[\u4e00-\u9fa5]"; //汉字的正则表达试
System.out.println("每" + n + "字节进行划分的字符串如下所示:");
for (int i = 0; i < string.length; i++) {
if (string[i].matches(m)) { //将字符数组中的每一个元素与表则表达式进行匹配,如果相同则返回true
count = count + 2; //如果当前字符是汉字,计数器count就加2
} else {
count = count + 1; //如果当前字符不是汉字,计数器count就加1
}
if (count < n) { //如果当前计数器count的值小于n,则输出当前字符
System.out.print(string[i]);
} else if (count == n) { //如果当前计数器count的值等于n,则输出当前字符
System.out.print(string[i]);
count = 0;
System.out.println(); //内循环结果,则需要换行,起到控制打印格式的作用
} else {
count = 0; //如果当前计数器count的值大于n,则计数器count清零,接着执行外部循环
System.out.println();
}
}
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/qq_35246620/article/details/53455639
标签:java,截取字符串
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
hutool实战:IoUtil 流操作工具类(将内容写到流中)
2022-11-16 09:17:47
![](https://img.aspxhome.com/file/2023/6/83086_0s.png)
Java 8新增的方法参数反射实例分析
2021-11-20 05:55:30
C#中程序自删除实现方法
2021-06-01 19:47:29
SpringBoot LocalDateTime格式转换方案详解(前端入参)
2022-01-05 06:48:03
![](https://img.aspxhome.com/file/2023/9/84269_0s.png)
Unity实现游戏卡牌滚动效果
2023-09-20 10:54:23
![](https://img.aspxhome.com/file/2023/2/80092_0s.jpg)
基于hibernate框架在eclipse下的配置方法(必看篇)
2022-11-09 20:30:26
![](https://img.aspxhome.com/file/2023/1/83141_0s.jpg)
Android组件Glide实现图片平滑滚动效果
2022-02-24 22:53:14
![](https://img.aspxhome.com/file/2023/2/101832_0s.jpg)
Spring事务失效的场景梳理总结
2023-02-23 16:23:24
Netty分布式获取异线程释放对象源码剖析
2021-12-28 09:45:42
![](https://img.aspxhome.com/file/2023/8/96118_0s.png)
Spring Boot2.3 新特性分层JAR的使用
2021-08-03 12:55:50
![](https://img.aspxhome.com/file/2023/2/65602_0s.jpg)
android初学者必须掌握的Activity状态的四大知识点(必读)
2022-08-31 13:26:31
![](https://img.aspxhome.com/file/2023/2/110752_0s.png)
Spring 报错:元素 "context:component-scan" 的前缀 "context" 未绑定的问题解决
2023-09-10 07:45:34
图形学之Unity渲染管线流程分析
2023-09-25 05:27:36
![](https://img.aspxhome.com/file/2023/9/87029_0s.jpg)
详解c# 委托链
2021-10-06 23:17:59
![](https://img.aspxhome.com/file/2023/0/77300_0s.png)
详解spring cloud eureka注册中心
2023-11-10 17:54:10
![](https://img.aspxhome.com/file/2023/3/59163_0s.png)
Android Button按钮的四种点击事件
2021-12-16 04:02:41
java框架之maven是用来做什么的
2023-04-20 21:59:43
![](https://img.aspxhome.com/file/2023/1/102641_0s.png)
浅谈Spring Cloud Ribbon的原理
2023-07-23 04:11:25
![](https://img.aspxhome.com/file/2023/6/76506_0s.png)
RocketMQ生产者如何规避故障Broker方式详解
2022-06-23 04:36:10
Idea打包springboot项目没有.original文件解决方案
2022-07-12 20:06:08
![](https://img.aspxhome.com/file/2023/6/62746_0s.png)