ITeye博客

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

旋风小胖神 · 2011-03-02 16:42 · 编程语言 · Markdown

前一段时间有个朋友问我怎么把字符串居中对齐,于是我写了一个例子给他!现在和大家分享一下,有什么不对的,请指教!


//取得字符串数组中最长的字符串的长度
	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));
		}
	}

评论(5)

  1. #5 楼 · li463968565 · 2011-03-04 17:16

    抓挖鸟 writes:

    [quote=sdh5724]看到getBytes(), 我笑了。

    如果要显示的话, 不是这么做的。 如果是等宽字符, 也不是这么算的。 你这个最多算个当前os字符集下的字节对齐。 不知道你的业务要求是什么。[/quote] 他玩他的,你笑你的。和谐社会。

    +1

  2. #4 楼 · 抓挖鸟 · 2011-03-04 16:43

    sdh5724 writes:

    看到getBytes(), 我笑了。

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

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

  3. #3 楼 · sdh5724 · 2011-03-04 00:49

    看到getBytes(), 我笑了。

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

  4. #2 楼 · mahu456 · 2011-03-03 17:02

    里面没有必要搞那么多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;
    	}
    
    
    
  5. #1 楼 · cjshbao · 2011-03-02 17:27

    没居中,2边不对称

评论功能维护中,当前仅展示历史评论。