Home  >  Article  >  Java  >  Detailed tutorial on splitting text strings

Detailed tutorial on splitting text strings

零下一度
零下一度Original
2017-06-27 09:39:051183browse

Question: In the project, when the saved data exceeds the database field column length limit, how to solve it?

A common solution is: string truncation and access. As the name suggests, large text data is intercepted according to the specified length, and the returned result set is stored in a new table in the order of interception. And by creating a type field in the new table to identify that the intercepted content in the new table corresponds to the field name in the old table, the corresponding fields in the old table no longer directly store large text data, but store identifiers.

Here, a tool class is provided to split text strings and return a List result set for the next step.

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;
    }
}

The above is the detailed content of Detailed tutorial on splitting text strings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn