blog.Toby

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            130 隨筆 :: 2 文章 :: 150 評(píng)論 :: 0 Trackbacks

          由于接到一任務(wù),大致是這樣子的:一個(gè)彈頁(yè)面里面要顯示一段文字(多國(guó)字符),彈出頁(yè)面的寬度是定死的。客戶(hù)不希望文字長(zhǎng)度過(guò)長(zhǎng)時(shí),下面出現(xiàn)水平的拉條。這就關(guān)系到一個(gè)自動(dòng)換行的問(wèn)題。

          由于中文,日文,韓文等一般占2個(gè)字節(jié),英文一般占1個(gè)字節(jié),所以要換行,首先要計(jì)算長(zhǎng)度。只有在字節(jié)長(zhǎng)度相同的情況下?lián)Q行,同時(shí)又要注意不能將中文字拆開(kāi)了,否則就會(huì)是亂碼了。

          經(jīng)過(guò)一番努力,這個(gè)問(wèn)題終于搞定了。具體函數(shù)如下

          /*判斷是否雙字節(jié)字符*/?

          public boolean isDoublebyteWord(String str){
          ??byte[] b;
          ??int temp;
          ??for (int i = 0; i < str.length(); i++) {
          ??b = str.substring(i, i + 1).getBytes();
          ??temp = b[0];
          ??if (temp > 0) {
          ??return false;
          ??}
          ??}
          ??return true;
          ?}

          /*給字符串添加換行符,其中l(wèi)inepos是需要換行的位置,按字節(jié)算的*/?

          public String lineStr(String s,int linePos){
          ??String new_str="";
          ??int total_len=0;
          ??int brNum=0;
          ??for(int i=1;i<=s.length();i++){
          ???if(isDoublebyteWord(s.substring(i-1,i))){
          ????total_len+=2;
          ????if(total_len>=(linePos*(brNum+1))){
          ?????new_str+=s.substring(i-1,i)+"<br/>";
          ?????brNum++;
          ????}else{
          ?????new_str+=s.substring(i-1,i);
          ????}????
          ???}else{
          ????total_len+=1;
          ????if(total_len>=(linePos*(brNum+1))){
          ?????new_str+=s.substring(i-1,i)+"<br/>";
          ?????brNum++;?????
          ????}else{
          ?????new_str+=s.substring(i-1,i);
          ????}
          ???}
          ???
          ??}
          ??return new_str;
          ?}




          ?

          在上一篇文章《將中英文混合的字符串換行》中提到了一個(gè)換行的算法,后來(lái)在實(shí)際應(yīng)用中發(fā)現(xiàn),還是有些問(wèn)題的,比如,一行全是英文,或者全是中文,排出來(lái)的長(zhǎng)度是不一樣的。所以。后來(lái)不得不借用表格以及css來(lái)控制,其實(shí)也很簡(jiǎn)單,就加一句話 style="word-break:break-all"。這樣自動(dòng)換行沒(méi)問(wèn)題。

          但是,我們還要實(shí)現(xiàn)的另外一個(gè)功能是:截取字符串的一部分,作為圖片的簡(jiǎn)介,比如說(shuō):在一個(gè)iframe中顯示最新的消息,配有圖片,但是給出的位置不可能全部顯示所有數(shù)據(jù),因此要截取一部分。而且字符串中有 <br/>這樣的換行符,因?yàn)椴荒芎?jiǎn)單的截取。再有就是顯示的行數(shù)不能超過(guò)三行。

          經(jīng)過(guò)一番努力,將于寫(xiě)了個(gè)截取函數(shù),還能將就這樣。函數(shù)如下

          ?public String getShortString(String source,int cutPos){
          ??String shortStr = "";
          ??int brNum=0;
          ??String tmp="";
          ??String tmp2 = "";
          ??String tmpShort="";
          ??int pos = 0;
          ??int total_len = 0;

          ? try{
          ??pos = source.indexOf("<br/>");
          ??System.out.println("1 pos = "+pos);
          ??if(pos>0){
          ???if(source.length()<=cutPos){
          ???
          ????tmpShort = source;
          ???}else{
          ????//判斷在截取位置是否有<br/>出現(xiàn),所以往前往后各取4位,判斷是否有<br/>出現(xiàn)
          ????//為什么取四,那是因?yàn)?lt;br/>長(zhǎng)度是5,而要<br/>中一個(gè)字符出現(xiàn)在cutPos位置,
          ????//取四最可能是cutPos位置是> 或者 < ;所以取四包括了所有可能出現(xiàn)情況
          ????//1:當(dāng)原字符串長(zhǎng)度大于截取位置+<br/>的長(zhǎng)度
          ????if ( (source.length()-cutPos) >=4){
          ?????if(cutPos>=4){
          ??????tmp = source.substring(cutPos-4,cutPos+4);
          ?????}else{
          ??????tmp= source.substring(0,cutPos+4);
          ?????}
          ????}else{
          ?????if(cutPos>=4){
          ??????tmp = source.substring(cutPos-4,source.length());
          ?????}else{
          ??????tmp= source.substring(0,source.length());
          ?????}
          ????}
          ????System.out.println("1 tmp = "+tmp);
          ????int ipos = tmp.indexOf("<br/>");
          ????if (ipos>0){
          ??????tmpShort = source.substring(0,cutPos-4)+tmp.substring(0,ipos)+"<br/>";?????
          ????}else{
          ?????tmpShort = source.substring(0,cutPos);
          ????}

          ???}
          ???System.out.println("1 tmpShort = "+tmpShort);
          ????tmp = tmpShort;
          ????tmp2 = tmpShort;
          ????while(pos>0){
          ?????brNum+=1;
          ?????tmp = tmp2.substring(0,pos);
          ????
          ?????if(brNum>2){
          ??????
          ??????tmp = tmp2.substring(0,pos);
          ??????System.out.println("tmp="+tmp);
          ??????//shortStr+=tmp;
          ??????pos = 0;
          ??????//tmp2 = tmp;
          ?????}else{
          ??????shortStr+=tmp+"<br/>";
          ??????tmp = tmp2.substring(pos+5);
          ??????System.out.println("tmp 2 ="+tmp);
          ??????pos = tmp.indexOf("<br/>");
          ??????System.out.println("pos="+pos);
          ??????tmp2 = tmp;??
          ?????}
          ??????
          ????}
          ????if (brNum==1){?
          ?????System.out.println("1");
          ?????if(tmp.length()>cutPos/2){
          ??????shortStr+=tmp.substring(0,cutPos/2)+" ...";//當(dāng)有一個(gè)<br/>時(shí) 后面再也沒(méi)有的時(shí)候,第二行截取設(shè)定長(zhǎng)度的一半。
          ?????}else{
          ??????shortStr+=tmp+" ...";
          ?????}
          ????}else if(brNum==2){
          ?????System.out.println("2");
          ?????if(tmp.length()>cutPos/3){
          ??????shortStr+=tmp.substring(0,cutPos/3)+" ...";//當(dāng)有二個(gè)<br/>時(shí) 后面再也沒(méi)有的時(shí)候,第三行截取設(shè)定長(zhǎng)度的1/3。
          ?????}else{
          ??????shortStr+=tmp+" ...";
          ?????}
          ????}else if(brNum==3){
          ?????System.out.println("3");
          ?????if(tmp.length()>cutPos/4){
          ??????shortStr+=tmp.substring(0,cutPos/4)+" ...";//當(dāng)有三個(gè)<br/>時(shí) 后面再也沒(méi)有的時(shí)候,第三行截取設(shè)定長(zhǎng)度的1/4
          ?????}else{
          ??????shortStr+=tmp+" ...";
          ?????}?????
          ?????//shortStr+=tmp+"<br/>"+" ...";
          ????}
          ??
          ???}else{
          ????if(source.length()<=cutPos){
          ???
          ?????tmpShort = source;
          ????}else{
          ?????tmpShort = source.substring(0,cutPos)+" ...";
          ????}
          /*????if(source.length()>cutPos){
          ?????if(tmpShort.length()>cutPos){
          ??????shortStr = tmpShort.substring(0,cutPos)+" ...";
          ?????
          ?????}else{
          ??????shortStr = tmpShort+" ...";
          ?????}
          ????}else{
          ?????shortStr = tmpShort;
          ????}*/
          ????shortStr = tmpShort;
          ?????
          ??}
          ??}catch(Exception e){
          ???if(source.length()<=cutPos){
          ???
          ????tmpShort = source;
          ???}else{
          ????tmpShort = source.substring(0,cutPos)+" ...";
          ???}
          ???e.printStackTrace();
          ???shortStr= tmpShort;
          ??}
          ??
          ??return shortStr;
          ??
          ?}



          作者Blog:http://blog.csdn.net/kasam/

          posted on 2006-03-25 11:03 渠上月 閱讀(949) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): java tips
          主站蜘蛛池模板: 永州市| 灌南县| 营山县| 葫芦岛市| 同心县| 凤台县| 庆阳市| 邢台县| 徐水县| 齐齐哈尔市| 呼伦贝尔市| 黄石市| 霍林郭勒市| 海安县| 镇沅| 乐业县| 乐昌市| 台江县| 芒康县| 遵义市| 天峨县| 和林格尔县| 贵德县| 大兴区| 大理市| 武穴市| 金昌市| 桑日县| 漠河县| 四平市| 溧阳市| 赫章县| 壶关县| 佳木斯市| 行唐县| 广水市| 呼玛县| 印江| 吉林市| 宁强县| 平凉市|