论坛首页 Java企业应用论坛

多个字符串居中对齐,算法!

浏览 7010 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (1)
作者 正文
   发表时间:2011-03-02  
    前一段时间有个朋友问我怎么把字符串居中对齐,于是我写了一个例子给他!现在和大家分享一下,有什么不对的,请指教!

//取得字符串数组中最长的字符串的长度
	public static int  findWord(String[] word){
		List<Integer> list=new ArrayList<Integer>();
		for(int i=0;i<word.length;i++){
			int wordLength=word[i].length();
			list.add(wordLength);
		}
		Collections.sort(list);
		int max=list.get((list.size()-1));
		return max;
	}

//取得字符传音长度的一半
	public static List findSubString(String[] word){
		List list=new ArrayList();
		for(int i=0;i<word.length;i++){
			String str=word[i];
			//取得字符串数组中字符串长度的一半
			int wordLength=word[i].length()/2;
			//并把长度和字符串放到一个数组中
			Object[] o={wordLength,str};
			//把这个数组放到List集合中
			list.add(o);
		}
		return list;
	}

//取得居中对齐后的字符串集合
     public static List findString(String[] word){
		List list=new ArrayList();
		//最大长度的一半
		int maxban=findWord(word)/2;
		List list1=findSubString(word);
		for(int i=0;i<list1.size();i++){
			//从List中取得数组
			Object[] ost=(Object[])list1.get(i);
			int x= (Integer)ost[0] ;
			String str1=(String)ost[1];
			//求每个字符串长度的一半 和最大长度的一半的差值
			int t=maxban-x;
			String xx="";
			//长度相同
			if(t==0){
				list.add(str1);
			}else{
				//计算前面有多少个空格
				for(int j=0;j<t;j++){
					xx=xx+" ";
				}
				//前面加多少个空格
				xx=xx+str1;
				list.add(xx);
			}
		}
		return list;
	}

//测试
	public static void main(String[] args) {
		String[] word={"Konwledge","is","Powerd"};
		List list=findString(word);
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
		}
	}

  • 大小: 2.4 KB
   发表时间:2011-03-02  
没居中,2边不对称
0 请登录后投票
   发表时间:2011-03-03  
里面没有必要搞那么多List。简单点好。
我改了一下,你看是否好一点。
	public static int findWord(String[] word) {
		int max = 0;
		if (null == word) {
			return max;
		}
		int temp = 0;
		for (int i = 0; i < word.length; i++) {
			if (null != word[i]) {
				temp = word[i].getBytes().length;
				if (max < temp) {
					max = temp;
				}
			}
		}
		return max;
	}

	// 取得居中对齐后的字符串集合
	public static List findString(String[] word) {
		List list = new ArrayList();
		// 最大长度
		int maxban = findWord(word);
		// 如果是奇数,就加1
		if (maxban % 2 != 0) {
			maxban++;
		}
		String str1 = null;
		for (int i = 0; i < word.length; i++) {
			int t = maxban;
			// 取得字符串
			str1 = word[i];
			if (null == str1 || str1.length() == 0) { //如果字符串为空,直接不用添加空格
				str1 = "";
				t = 0;
			} else {
				// 求每个字符串长度和最大长度的差值的一半
				t = (maxban - str1.getBytes().length) / 2; //这里是取整
			}
			String xx = "";
			// 长度相同
			if (t > 0) {
				// 计算前面有多少个空格
				for (int j = 0; j < t; j++) {
					xx = xx + " ";
				}
				// 前面加多少个空格
				str1 = xx + str1;
			}
			list.add(str1);
		}
		return list;
	}

0 请登录后投票
   发表时间:2011-03-04   最后修改:2011-03-04
看到getBytes(),  我笑了。

如果要显示的话, 不是这么做的。 如果是等宽字符, 也不是这么算的。  你这个最多算个当前os字符集下的字节对齐。
不知道你的业务要求是什么。
0 请登录后投票
   发表时间:2011-03-04  
sdh5724 写道
看到getBytes(),  我笑了。

如果要显示的话, 不是这么做的。 如果是等宽字符, 也不是这么算的。  你这个最多算个当前os字符集下的字节对齐。
不知道你的业务要求是什么。

他玩他的,你笑你的。和谐社会。
0 请登录后投票
   发表时间:2011-03-04  
抓挖鸟 写道
sdh5724 写道
看到getBytes(),  我笑了。

如果要显示的话, 不是这么做的。 如果是等宽字符, 也不是这么算的。  你这个最多算个当前os字符集下的字节对齐。
不知道你的业务要求是什么。

他玩他的,你笑你的。和谐社会。

+1
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics