<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    blog.Toby

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      130 隨筆 :: 2 文章 :: 150 評論 :: 0 Trackbacks

    由于接到一任務,大致是這樣子的:一個彈頁面里面要顯示一段文字(多國字符),彈出頁面的寬度是定死的。客戶不希望文字長度過長時,下面出現水平的拉條。這就關系到一個自動換行的問題。

    由于中文,日文,韓文等一般占2個字節,英文一般占1個字節,所以要換行,首先要計算長度。只有在字節長度相同的情況下換行,同時又要注意不能將中文字拆開了,否則就會是亂碼了。

    經過一番努力,這個問題終于搞定了。具體函數如下

    /*判斷是否雙字節字符*/?

    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;
    ?}

    /*給字符串添加換行符,其中linepos是需要換行的位置,按字節算的*/?

    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;
    ?}




    ?

    在上一篇文章《將中英文混合的字符串換行》中提到了一個換行的算法,后來在實際應用中發現,還是有些問題的,比如,一行全是英文,或者全是中文,排出來的長度是不一樣的。所以。后來不得不借用表格以及css來控制,其實也很簡單,就加一句話 style="word-break:break-all"。這樣自動換行沒問題。

    但是,我們還要實現的另外一個功能是:截取字符串的一部分,作為圖片的簡介,比如說:在一個iframe中顯示最新的消息,配有圖片,但是給出的位置不可能全部顯示所有數據,因此要截取一部分。而且字符串中有 <br/>這樣的換行符,因為不能簡單的截取。再有就是顯示的行數不能超過三行。

    經過一番努力,將于寫了個截取函數,還能將就這樣。函數如下

    ?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/>出現,所以往前往后各取4位,判斷是否有<br/>出現
    ????//為什么取四,那是因為<br/>長度是5,而要<br/>中一個字符出現在cutPos位置,
    ????//取四最可能是cutPos位置是> 或者 < ;所以取四包括了所有可能出現情況
    ????//1:當原字符串長度大于截取位置+<br/>的長度
    ????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)+" ...";//當有一個<br/>時 后面再也沒有的時候,第二行截取設定長度的一半。
    ?????}else{
    ??????shortStr+=tmp+" ...";
    ?????}
    ????}else if(brNum==2){
    ?????System.out.println("2");
    ?????if(tmp.length()>cutPos/3){
    ??????shortStr+=tmp.substring(0,cutPos/3)+" ...";//當有二個<br/>時 后面再也沒有的時候,第三行截取設定長度的1/3。
    ?????}else{
    ??????shortStr+=tmp+" ...";
    ?????}
    ????}else if(brNum==3){
    ?????System.out.println("3");
    ?????if(tmp.length()>cutPos/4){
    ??????shortStr+=tmp.substring(0,cutPos/4)+" ...";//當有三個<br/>時 后面再也沒有的時候,第三行截取設定長度的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 渠上月 閱讀(947) 評論(0)  編輯  收藏 所屬分類: java tips
    主站蜘蛛池模板: 大地影院MV在线观看视频免费| 亚洲激情视频图片| 永久免费精品影视网站| 韩国欧洲一级毛片免费| 亚洲色大成网站www尤物| 无码日韩人妻av一区免费| 亚洲fuli在线观看| 最近最好的中文字幕2019免费| 亚洲一区无码中文字幕乱码| 皇色在线视频免费网站| 亚洲熟女精品中文字幕| 在线日韩av永久免费观看| 亚洲精品无码中文久久字幕| 国产精品视频免费一区二区三区 | 国产女高清在线看免费观看| 亚洲JIZZJIZZ妇女| 伊在人亚洲香蕉精品区麻豆| 免费大片av手机看片| 国产av无码专区亚洲av果冻传媒| 国产日韩精品无码区免费专区国产| 中文字幕精品亚洲无线码二区 | 国产亚洲精品AA片在线观看不加载| 一区二区三区免费视频观看| 亚洲av永久无码精品国产精品| 91高清免费国产自产拍2021| 亚洲va久久久久| 亚洲av高清在线观看一区二区| 99在线热播精品免费99热| 亚洲精品第一国产综合精品| 女人18毛片水最多免费观看| 男男gay做爽爽免费视频| 国产亚洲精品一品区99热| 亚洲AV无码乱码在线观看裸奔| 99国产精品免费视频观看| 亚洲精品动漫免费二区| 久久精品国产亚洲5555| 97精品免费视频| 一级美国片免费看| 亚洲人成日本在线观看| 亚洲人成影院在线观看| 成人免费视频77777|