JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 技巧文件  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 textarea 斷行符號處理方法 [精華]
browser

戀香

版主

發文: 3570
積分: 1
於 2003-06-20 11:32 user profilesend a private message to usersend email to browserreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<%@ page language="java" %>
<html>
<head>
  <title>CH7 Example:Textarea</title>
</head>
 
<body>
<%
       // 將從Textarea的資料,存入msg的字串當中
       String msg = request.getParameter("Message");
  
       // 宣告一新字串,表示修改過字串後的結果
       String Message = "";
 
       // msgLength 表示Textarea 的字串長度
       int msgLength = msg.length();
 
       // Position 表示目前游標的位置
       int Position = 0;
 
       while (true) {
 
  // 表示0x0D 的位置
                int index = msg.indexOf(0x0D, Position);
 
                // 假設都沒有換行時,直接離開while 迴圈
                if (index == -1) {
                break;
        }
 
        // 假設有換行時,將換行之前的字串,放置到新的Message字串上,
        // 做完再加上HTML格式的換行tag,<br>
        if (index > Position) {
    Message += msg.substring(Position, index);
        }
        Message += "<br>";
        Position = index + 1;
      }
 
      if (Position >= 0) {
        Message += msg.substring(Position);
      }
     out.println(Message);    
%>
 
</body>
</html>


browser edited on 2003-10-22 15:01
reply to postreply to post
作者 Re:textarea 斷行符號處理方法 [Re:browser]
iampoya

Speculator

版主

發文: 169
積分: 8
於 2003-06-20 12:57 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不好意思,請教一下
像這種斷行或過濾字串的東東
是不是用javascript在client處理即可
還是說回server端處理,是有特別的用意嗎?


reply to postreply to post
Japan Adult Video Album
作者 Re:textarea 斷行符號處理方法 [Re:iampoya]
browser

戀香

版主

發文: 3570
積分: 1
於 2003-06-20 13:04 user profilesend a private message to usersend email to browserreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
iampoya wrote:
不好意思,請教一下
像這種斷行或過濾字串的東東
是不是用javascript在client處理即可
還是說回server端處理,是有特別的用意嗎?


皆可以 ...
只不過 .. 像我不會寫 JavaScript
所以會想用JSP來寫 ..


reply to postreply to post
作者 Re:textarea 斷行符號處理方法 [Re:browser]
Person



發文: 0
積分: 0
於 2003-06-22 11:33 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我建議可以考慮用 String.replaceAll()
若要切字串改用 String.split() 或 StringTokenizer

其實至於Range Check,我覺得如果一個專業的網站
應該client和server端都要check這才是比較安全的做法~
因為做在client很容易遭到竄改~server端做反應時間需要比較長~
所以兩邊都做比較好..Smile


reply to postreply to post
作者 Re:textarea 斷行符號處理方法 [Re:Person]
browser

戀香

版主

發文: 3570
積分: 1
於 2003-06-22 11:53 user profilesend a private message to usersend email to browserreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Person wrote:
我建議可以考慮用 String.replaceAll()
若要切字串改用 String.split() 或 StringTokenizer

其實至於Range Check,我覺得如果一個專業的網站
應該client和server端都要check這才是比較安全的做法~
因為做在client很容易遭到竄改~server端做反應時間需要比較長~
所以兩邊都做比較好..Smile


沒錯 .. 第一篇post的是從JSP 技術手冊上copy下來 ..
現在 jdk 1.4 .. 可以直接使用 String.replaceAll( ) 的方法
將會在新版中加入 ...

不過 String.replaceAll( ) 和 String.split() 都是JDK 1.4
我相信還是有很多公司都是用 JDK 1.3 ...
所以那個範例程式還是有參考價值 ...


reply to postreply to post
作者 Re:textarea 斷行符號處理方法 [Re:browser]
sdargon





發文: 109
積分: 0
於 2003-07-21 15:15 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我是用1.3版的
所以就自己寫一個取代全部字串的函數...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public String replace(String src, String astr, String bstr) {
  int index = 0;
  StringBuffer sb = new StringBuffer();
  
  do {
    index = src.indexOf(astr);
    if(index == -1) sb.append(src);
    else {
      sb.append(src.substring(0,index));
      sb.append(bstr);
      src = src.substring(index + astr.length());
    }
  } while(index != -1);
  
  return sb.toString();
}


browser edited on 2003-10-22 15:01
reply to postreply to post
作者 Re:textarea 斷行符號處理方法 [Re:browser]
jack1312





發文: 13
積分: 0
於 2003-07-23 16:09 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
想請問一下...
如何得知0x0D就是斷行呢?


reply to postreply to post
作者 Re:textarea 斷行符號處理方法 [Re:browser]
wind



發文: 0
積分: 0
於 2003-07-25 11:15 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
因為我才初學,當初不知道這裡就有討論,
花了好些時間才弄出解決方案... : (
不過有些不同,我是使用eplaceAll():

afterConvert = beforeConvert.replaceAll("\r\n","\" + \"\\\\n\" + \"");


reply to postreply to post
作者 Re:textarea 斷行符號處理方法 [Re:browser]
annhy

來呦~



發文: 45
積分: 2
於 2003-07-25 15:29 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
browser wrote:
沒錯 .. 第一篇post的是從JSP 技術手冊上copy下來 ..
現在 jdk 1.4 .. 可以直接使用 String.replaceAll( ) 的方法
將會在新版中加入 ...

不過 String.replaceAll( ) 和 String.split() 都是JDK 1.4
我相信還是有很多公司都是用 JDK 1.3 ...
所以那個範例程式還是有參考價值 ...


所以之前我就自己寫了個 split() 函式,
可以 demo 一下 StringTokenizer 的用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  /**
   * 傳入一個 String,傳回以 delimiter 為分隔符號來分割後的 String array。
   * @param source 欲分割的 String
   * @param delimiter 分隔符號 String
   * @return 分割後的 String array
   **/
  public static String[] mySplit(String source, String delimiter) {
    StringTokenizer st = new StringTokenizer(source, delimiter);
  
    // 因為長度不定,所以先放在 ArrayList 中
    List list = new ArrayList();
    while (st.hasMoreElements()) {
      list.add(st.nextToken().trim());
    }
  
    // 將 ArrayList 轉換為 String[]
    return (String[])list.toArray(new String[0]);
  }


reply to postreply to post
作者 Re:textarea 斷行符號處理方法 [Re:browser]
deepkh





發文: 7
積分: 2
於 2003-09-08 16:45 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我自己也寫過喔,不過是一個一個加上去的,所以效率不好 ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class MyString {
    public static String replaceAll(String source, String origin, String replace) {
        StringBuffer sb = new StringBuffer();
        sourcefor :
        for (int i=0, j=0; i<source.length(); i++, j=0) {
            while( source.charAt( j+i ) == origin.charAt( j ) )
            if ( j == (origin.length() -1) ) {
                sb.append( replace );
                i += (origin.length()-1);
                continue sourcefor;
            } else {
                j++;
            }
        sb.append( source.charAt( i ) );
        }
    return sb.toString();
    }
}


deepkh edited on 2003-09-08 16:49
reply to postreply to post
作者 Re:textarea 斷行符號處理方法 [Re:browser]
browser

戀香

版主

發文: 3570
積分: 1
於 2003-10-22 14:43 user profilesend a private message to usersend email to browserreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
現在程式只需要一行:

1
2
3
String msg = request.getParameter("Message");
String str = msg.replaceAll("\r\n","<br>");
out.println(str);


這必須在 JDK 1.4 之下才能執行 .....


browser edited on 2003-10-22 14:53
reply to postreply to post
作者 Re:textarea 斷行符號處理方法 [Re:browser]
Rowe





發文: 18
積分: 0
於 2004-03-27 13:32 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
真後悔沒有早點來這邊看..
存成文字檔的時後這隻程式就很好用了...雖然大概很少機會用到
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//data是一個textarea的資料
String msg = request.getParameter("data");
String realPath = request.getRealPath("/");
FileWriter fw = new FileWriter(realPath + "\\File.txt");
BufferedWriter bw = new BufferedWriter(fw);
 
int msgLen = msg.length();
int Position = 0;
int index = msg.indexOf(0x0D, Position);
 
while (index != -1) {
  bw.write(msg.substring(Position, index));
  bw.newLine();
  Position = index + 2;
  index = msg.indexOf(0x0D, Position);
}
bw.close();
fw.close();


reply to postreply to post
作者 Re:textarea 斷行符號處理方法 [Re:browser]
SPeeD





發文: 55
積分: 0
於 2004-08-12 11:40 user profilesend a private message to usersend email to SPeeDreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
為什麼我試出來是這樣 ?

String str = msg.replaceAll("\\\\r\\\\n","<br>");


reply to postreply to post
作者 Re:textarea 斷行符號處理方法 [Re:browser]
Julian13





發文: 1
積分: 0
於 2004-08-19 16:31 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
if you don't mind a bit ugly, use <PRE> will do.

1
2
3
<PRE>
<%=message%>
</PRE>


reply to postreply to post
作者 Re:textarea 斷行符號處理方法 [Re:browser]
carny





發文: 78
積分: 0
於 2004-09-29 18:27 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
個人是覺得用<pre>顯示出來的字型好像不太一樣
在這裡提供一個JavaScript的斷行方法
1
2
3
while(myString.indexOf("\n")>=0){
  myString = myString.replace("\n", "<br>");
} 


reply to postreply to post
作者 Re:textarea 斷行符號處理方法 [Re:Rowe]
vansan





發文: 21
積分: 0
於 2005-03-11 15:06 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
用jstl的taglib

1
2
3
4
5
6
7
<%@ taglib uri="/WEB-INF/taglibs-string.tld" prefix="s" %>
 
<s:replace replace="NL" with="<br />NL" newlineToken="NL">
         <c:out value="${flowForm.lpcVo.jobDesc}"/>
      </s:replace><br><br><br>
 

上頭
with="<br />NL"
的編碼是這樣的~~
要把<及>變成html的編碼


vansan edited on 2005-03-11 17:25
reply to postreply to post
作者 Re:textarea 斷行符號處理方法 [Re:carny]
ljason





發文: 4
積分: 0
於 2006-07-06 10:15 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
可以在textarea裡面顯示有底線的字嗎?可以直接顯示在螢幕上,而不出現tag的標籤

reply to postreply to post
作者 Re:textarea 斷行符號處理方法 [Re:browser]
kisskevin524





發文: 7
積分: 0
於 2007-01-12 01:03 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請問一下~
我用上面大大所說的jstl
prefix="s"那一串有錯~
請問正確引入的那一條要去哪裡抓?

都無法試出來~~
可以請大大教一下嗎??


kisskevin524 edited on 2007-01-12 01:07
reply to postreply to post
作者 Re:textarea 斷行符號處理方法 [Re:browser]
ra





發文: 41
積分: 0
於 2007-03-23 17:09 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
如果是在JSP上顯示有斷行的文字

1
2
3
4
5
6
7
8
9
10
11
12
13
<script>
function window.onload() {
   document.getElementById("txtMsg").value = document.getElementById("msg").value;
}
<script>
 
<body>
<input type="hidden" name="msg" value="<%=Msg%>">
 
<textarea name="txtMsg" ></textarea>
</body>
 


ra edited on 2007-10-24 14:40
reply to postreply to post
» JWorld@TW »  Java 技巧文件

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8