使用List把一个长字符串分解成若干个短字符串

把一个长字符串分解成若干个固定长度的短字符串,由于事先不知道长字符串的长度,以及短字符串的数量,只能使用List。

	public static void get_list_sbody(String s){
		
		// 计数变量
		int num = 0;
		// 每行的字符数
		int r_num = 27;
		// 字符串
		String sx =  new String("");
		
		lst_sbody = new ArrayList();
		char[] cr =s.toCharArray();
		
		for(int i=0; i<cr.length; i++){
			// 换行或字符足够时换行
			if ((cr[i] == '
') || (num == r_num)){
				sx = sx + cr[i];
				lst_sbody.add(sx);
				num = 0;
				sx = "";
			}
			// 不换行
			else{
				sx = sx + cr[i];
				num++;
			}
		}
		
	}

其中,s是需要转换的长字符串。首先使用toCharArray()将s转换成字符串数组。然后,对每一个字符,如果该字符是回车符,或者字符数量已达到规定的数量,则生成一个新字符串;否则,将该字符加入当前的字符串。

原文地址:https://www.cnblogs.com/mstk/p/3585306.html