按指定宽度填充文本内容,单词自适应换行

在我们想把文本按指定的宽度写到文件中时, 如作文本报表有几行几列,每列按一定的宽度输出内容,我们就可以用到下面操作了.

public class RptUtil {
    public static char fillChar = 32;
    private static void formatString(StringBuffer _cellBuf, String _value, int _size){
        int len = _value.length();
        int start = 0;
        int end = 0;
        String line = null;
        for(int i=0;i<len/_size+1;i++){
         start = i*_size;
            end = start + _size;
            if(end>len)
                end=len;
            line = _value.substring(start, end);
            int cut = line.indexOf('/n');
            if(cut>=0){
                line = line.substring(0,cut);
                if(cut==0){
                    _cellBuf.append(fillChar);
                }else{
                    _cellBuf.append(line);
                }
                fillSubCell(_cellBuf, _size);
                int ind = start+cut;
                if(ind<len)
                    ind+=1;
                _value=_value.substring(ind);
                len = _value.length();
                i=-1;        
            }else if(end==len){
                _cellBuf.append(line);
                fillSubCell(_cellBuf, _size);               
            }else if(_value.charAt(end)==32){
                _cellBuf.append(line);      
                fillSubCell(_cellBuf, _size);  
            }else{
                int l = line.lastIndexOf(32);
                if(l<0){
                 _cellBuf.append(line);                  
                }else{
                    line=line.substring(0,line.lastIndexOf(32)+1);
                 _cellBuf.append(line);      
                 fillSubCell(_cellBuf, _size);
                 _value=_value.substring(start+l+1);
                 len = _value.length();
                 i=-1;
                }
            }
        }
    }
    public static void fillSubCell(StringBuffer _strBuff, int _size){
        int length = _strBuff.length();
        while(length%_size!=0){
            _strBuff.append(fillChar);
            length++;
        }
    }
   
    public static void main(String []agrs){
        //String value = "Str = want to/n telltest you something tstst=important/n  h1111111111 about hmmy life=,but I am  afraid of the man dsf =sfsf sdfsf sfsf namd XXXX./n  you can th abou the away....do you know i am very hugslslfy";
        String value = "(continued) In the former part of this artical, i have mention that extensive reading i do not prefer to. ... The greates harm  done to students of english in china isIntensive reading,L,L.Ris";
        StringBuffer strBuf = new StringBuffer();
        int size = 30;
        formatString(strBuf, value, size);
        int len = strBuf.length();//209
        int row = len/size;//
        for(int i=0;i<=row;i++){
            int start = i*size;
            int end = start+size;
            if(end>len)
             end=len;
            System.out.println(strBuf.substring(start, end));
        }
       
    }

}

 

<think>好的,用户想知道如何设置表格的大小,使其能够自适应内容的多少。首先,我需要回忆一下常见的HTML和CSS方法。表格自适应内容通常涉及到控制表格的宽度,防止内容溢出或者表格过宽。 首先,用户可能需要让表格根据内容自动调整宽度,这时候使用`table-layout: auto`是个好方法。默认情况下,表格布局就是auto,但有时候可能被其他样式覆盖,所以显式设置一下比较好。这样每个列的宽度会根据内容自动调整,但要注意,如果有长文本可能会撑开表格,导致整体过宽。 接下来,用户可能遇到内容过长的问题,特别是没有空格的长单词或URL。这时候`word-break: break-all`或者`overflow-wrap: break-word`可以派上用场,强制换行,避免表格被撑开。需要解释这两个属性的区别,比如break-all更激进,而break-word只在必要时换行。 然后,用户可能希望表格整体不超过某个容器的宽度,比如父元素的100%。这时候设置`max-width: 100%`可以确保表格不会超出容器,尤其是在响应式布局中很重要。同时,结合`width: auto`可以让表格在内容较少时自然收缩,内容多时不超过最大宽度。 还有,用户可能希望表格的列宽根据内容动态调整,同时平均分配剩余空间。这时候`width: 1%`和`white-space: nowrap`的组合可以用于某些列固定宽度,而其他列自动填充剩余空间。比如序号列固定,内容列自适应。 此外,考虑使用Flexbox或Grid布局作为替代方案,特别是当需要更复杂的自适应布局时。但用户的问题是关于表格的,所以可能需要先介绍传统表格的方法,再提到这些现代方法作为补充。 最后,总结最佳实践,比如结合自动布局、换行控制和最大宽度限制,根据实际需求选择不同的方法组合。可能还需要提醒用户测试不同情况,比如长内容和混合内容的情况,确保表格在各种情况下都能良好显示。</think>设置表格自适应内容大小可通过以下方法实现(以HTML/CSS为例): 一、基础自适应方案 ```html <table style="table-layout: auto;"> <!-- 默认布局模式,根据内容自动调整列宽 --> </table> ``` 二、处理长内容溢出 ```css table { width: auto; /* 宽度自适应 */ max-width: 100%; /* 不超过父容器宽度 */ table-layout: auto; } td { word-break: break-all; /* 强制换行处理长单词 */ overflow-wrap: break-word; /* 优先保留单词完整性 */ } ``` 三、混合布局方案 ```css table { width: 100%; table-layout: fixed; /* 固定布局模式 */ } td:nth-child(1) { width: 1%; /* 固定最小宽度 */ white-space: nowrap; /* 禁止换行 */ } td:nth-child(2) { width: auto; /* 剩余空间自适应 */ } ``` 四、响应式进阶方案 ```css @media (max-width: 600px) { table { display: block; overflow-x: auto; /* 小屏幕添加横向滚动 */ } } ``` 最佳实践组合: 1. 使用`table-layout: auto`配合`width: auto`实现基础自适应 2. 通过`max-width: 100%`防止容器溢出 3. 添加`word-break`和`overflow-wrap`处理长文本 4. 对特殊列使用`white-space: nowrap`保持单行显示 5. 移动端添加横向滚动保持可读性 注:实际效果需根据具体内容结构调试,可通过浏览器开发者工具实时调整CSS参数观察变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值