java分割文本字符串的方法
作者:一只笨笨鸟 时间:2023-11-13 03:52:24
问题:在项目中,当保存数据超过数据库字段列长度限制时,如何解决?
一种常见的解决办法是:截串存取。顾名思义,就是对大文本数据按指定长度进行截取,返回结果集依截取顺序存储在新表中。并通过在新表中创建一个type字段来标识新表中截取的内容对应旧表中的字段名,而旧表中相应的字段不再直接存放大文本数据,而是存放标识符。
这里,提供一种工具类,用来分割文本字符串,返回List结果集供下一步使用。
public class StringUtil {
/**
* @Name: getContentByList
* @Description: 字符串切割
* @Author: 张(作者)
* @Version: V1.00 (版本号)
* @Create Date: 2017-6-26(创建日期)
* @Parameters: wholecontent:传递的文本字符串;
cutcount:切割字符串的长度
* @Return: List:切割字符串形成的集合,存放结果集
*/
public static List<String> getContentByList(String wholecontent,int cutcount){
List<String> list = new ArrayList<String>();
//获取完整内容字符串的总长度
int contentlen = wholecontent.length();
//内容截取,用内容总长和截取长度进行比较,无须截取的话直接插入
if (contentlen < cutcount){
list.add(wholecontent);
}
//内容长度超过截取长度
else{
//定义并初始化内容段落
String contentpart ="";
//定义并初始化被截取的段落数量
int contentround =0;
//开始截取的位置
int begincount = 0;
//判断截取的段落数
int contentcutpart = contentlen/cutcount;
int contentcutparts = contentlen%cutcount; //求余数
//若余数为0,说明被整除,内容的长度正好是截取长度的倍数。
if (contentcutparts==0){
contentround = contentcutpart;
}
else{
contentround = contentcutpart+1;
}
//循环截取内容
for (int i = 1; i <= contentround; i++) {
//如果不是最后一个截取部分
if (i != contentround){
//按照截断长度截取内容
contentpart = wholecontent.substring(begincount, cutcount*i);
}
else{
//截取最后一部分内容
contentpart = wholecontent.substring(begincount, contentlen);
}
//赋值下一截取部分的起点位置
begincount = cutcount*i;
list.add(contentpart);
}
}
return list;
}
}
标签:java,分割,字符串
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java判断http地址是否连通(示例代码)
2023-08-05 03:24:05
详解Java 中的三种代理模式
2023-06-25 07:49:17
springboot2.x整合tkmapper的示例代码
2021-09-19 11:56:59
Android提高之MediaPlayer播放网络音频的实现方法
2022-01-17 02:39:56
![](https://img.aspxhome.com/file/2023/1/102601_0s.png)
Android 应用签名的两种方法
2023-07-29 14:02:04
Android studio 添加assets文件夹的方法
2023-02-20 10:09:37
![](https://img.aspxhome.com/file/2023/2/137342_0s.png)
浅谈web服务器项目中静态请求和动态请求处理
2022-03-01 21:26:12
![](https://img.aspxhome.com/file/2023/6/89466_0s.png)
C#使用iTextSharp封装的PDF文件操作类实例
2023-05-16 22:26:52
C#中static的详细用法实例
2021-05-27 21:37:00
关于C#数强转会不会抛出异常详解
2021-11-09 05:44:48
C#串口通信实现方法
2023-04-15 06:31:11
C#程序中类数量对程序启动的影响详解
2022-09-02 11:35:02
![](https://img.aspxhome.com/file/2023/5/106395_0s.jpg)
Android Button按钮的四种点击事件
2021-12-16 04:02:41
Spring Boot中如何使用断路器详解
2022-03-03 06:34:49
![](https://img.aspxhome.com/file/2023/6/93066_0s.gif)
Android使用ViewPager实现屏幕滑动效果
2023-01-18 20:00:14
Flyway详解及Springboot集成Flyway的详细教程
2022-09-21 10:37:49
![](https://img.aspxhome.com/file/2023/4/70304_0s.jpg)
SpringCloudAlibaba整合Feign实现远程HTTP调用的简单示例
2023-11-19 16:16:05
![](https://img.aspxhome.com/file/2023/9/59669_0s.png)
SpringBoot返回多种格式的数据的实现示例
2023-11-24 14:22:52
![](https://img.aspxhome.com/file/2023/0/59700_0s.png)
ubuntu环境下反编译android apk的方法
2022-08-09 13:41:41
![](https://img.aspxhome.com/file/2023/4/129054_0s.png)
C#8.0中的模式匹配
2023-07-19 13:27:39