2015年1月26日 星期一

VB.NET 字數限制



        Dim str_temp As String = String.Empty, maxlength As Integer = 50, a1 As Integer = 0, a2 As Integer = 0
        Dim n As System.Text.ASCIIEncoding = New System.Text.ASCIIEncoding()
        Dim b() As Byte = Nothing
        b = n.GetBytes(TextBox2.Text)
        For i As Integer = 0 To b.Length - 1
            If b(i) = 63 Then
                a1 = a1 + 1
            Else
                a2 = a2 + 1
            End If
        Next

        Response.Write("byte長度:" & b.Length & ";全形(包含中文字、標點符號)=" & a1 & ";半形(英文字、標點符號)=" & a2)


以上有錯誤請指正,謝謝

2015年1月23日 星期五

ASP.NET DropDownList 程式碼新增Item



            Dim dt1 As DataTable=Nothing
            ....假設dt1已經從資料庫中取回項目資料....
            DropDownList1.DataTextField = "html_text" '顯示在網頁上的項目名稱
            DropDownList1.DataValueField = "value"  '項目的值
            DropDownList1.DataSource = dt1.DefaultView
            DropDownList1.DataBind()  '資料繫結
            '資料繫結後在DropDownList第一個項目前,再插入新的項目
            DropDownList1.Items.Insert(0, New ListItem("請選擇審核人", "0"))

2015年1月20日 星期二

[ASP.NET for VB.NET] String.Trim 無法刪除空白字元

參考資料1.Trimming Character Strings
參考資料2.魔鬼般的細節:使用 C# 的 String.Trim() 方法刪除空白字元

有時候會因為複製Excel檔裡的資料,貼到自己的程式中執行,在複製過來的資料中可能因為排版的關係,而產生一些特殊字元,使得您再怎麼刪除空白字元都無法刪除,解決辦法如下。

        Dim str_data As String = "   ABCDE      "
        Dim whiteSpaceDelimiters() As Char = _
        {ChrW(&H9),ChrW(&HA),ChrW(&HB),ChrW(&HC),ChrW(&HD),ChrW(&H20), _
          ChrW(&HA0),ChrW(&H2000),ChrW(&H2001),ChrW(&H2002),ChrW(&H2003), _
          ChrW(&H2004),ChrW(&H2005),ChrW(&H2006),ChrW(&H2007),ChrW(&H2008), _
          ChrW(&H2009),ChrW(&H200A),ChrW(&H200B),ChrW(&H3000),ChrW(&HFEFF) _
        }
        str_data = str_data.Trim(whiteSpaceDelimiters)
        Response.Write("[" & str_data & "]")


        '如果是C#
        'char[] whiteSpaceDelimiters = new char[]
        '{
        ''\u0009’,  // CHARACTER TABULATION
        ''\u000A’,  // LINE FEED
        ''\u000B’,  // LINE TABULATION
        ''\u000C’,  // FORM FEED
        ''\u000D’,  // CARRIAGE RETURN
        ''\u0020’,  // SPACE
        ''\u00A0’,  // NO-BREAK SPACE
        ''\u2000’,  // EN QUAD
        ''\u2001’,  // EM QUAD
        ''\u2002’,  // EN SPACE
        ''\u2003’,  // EM SPACE
        ''\u2004’,  // THREE-PER-EM SPACE
        ''\u2005’,  // FOUR-PER-EM SPACE
        ''\u2006’,  // SIX-PER-EM SPACE
        ''\u2007’,  // FIGURE SPACE
        ''\u2008’,  // PUNCTUATION SPACE
        ''\u2009’,  // THIN SPACE
        ''\u200A’,  // HAIR SPACE
        ''\u200B’,  // ZERO WIDTH SPACE
        ''\u3000’,  // IDEOGRAPHIC SPACE
        ''\uFEFF’  // ZERO WIDTH NO-BREAK SPACE
        '};

2015年1月10日 星期六

JavaScript VB.NET 修改html 標籤

參考資料:Regular expression 抓 HTML 標籤屬性與內容

說明:因有個需求必須從某個網頁,跨網域到某一台Server取資料,但資料的內容含有html標籤,而我又想要能針對a、img標籤可以正常顯示,因此運用字串搜尋的方式,將我想修改的標簽進行修改,主要的作法是搜尋到a標籤修改href、img標籤修改src。

如您有更好的寫法,請建議,謝謝。

ASP.NET-VB.NET語法版本:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim str_cont As String = String.Empty
                '假設str_cont是取得後的資料
str_cont = "附件:<a href=""/boardfile/file/osa/%E6%B4%BB%E5%8B%95%E8%AA%AA%E6%98%8E1031231.pdf"" target=""_blank"">活動說明</a>、<a href=""/boardfile/file/cc/%E5%AF%A6%E6%96%BD%E8%BE%A6%E6%B3%951030930.docx"" target=""_blank"">實施辦法</a><br/><img alt=""活動海報"" src=""/boardfile/file/cc/poster1031003.jpg"" width=""640"" height=""902"" border=""0"" title=""活動海報"" /><br/>" & _
"一、為推廣並落實資訊教育,以及展示政府推動行政電子化之成效,臺北市電腦商業同業公會結合國內相關公會共同組成資訊月活動委員會,規劃辦理103年資訊月全國巡迴展示活動。"
str_cont = funbody(str_cont, "<a(.*?)>(.*?)<\/a\s*>", "href=") 'a標籤
str_cont = funbody(str_cont, "<img(.*?)>", "src=") 'img標籤
Response.Write(str_cont)
End If
End Sub

Function funbody(ByVal cont As String, ByVal Regex_where As String, ByVal edit_pro As String) As String
Dim x1 As Integer = 0, str_temp1 As String = String.Empty, str_temp2 As String = String.Empty, str_path As String = String.Empty,
strhtml_tag As String = String.Empty, strhtml_pro As String = String.Empty, si1 As Integer = 0, si2 As Integer = 0, si3 As Integer = 0 ', str_path As String = String.Empty
Dim regex As Regex = New Regex(Regex_where) '"<a\s+([\s\S]*?)>([\s\S]*?)<\/a\s*>"
Do
x1 = regex.Match(cont).Index
If x1 = 0 Then
str_temp2 = fun_string1(str_temp2, cont)
Exit Do '如果找不到標籤就跳出
Else
str_temp1 = cont.Substring(0, x1) '如果使用Mid擷取字串,起始位置要設定在1,如:Mid(cont, 1, x1)
str_temp2 = fun_string1(str_temp2, str_temp1)
cont = cont.Replace(str_temp1, "")
End If
If regex.Match(cont).Groups.Count > 0 Then
strhtml_tag = regex.Match(cont).Groups(0).Value '標籤
strhtml_pro = regex.Match(cont).Groups(1).Value '屬性
cont = cont.Replace(strhtml_tag, "")

si1 = strhtml_pro.IndexOf(edit_pro)
si2 = (strhtml_pro.IndexOf(Chr(34), si1)) + 1 '第一個雙引號
si3 = strhtml_pro.IndexOf(Chr(34), si2) '第二個雙引號
str_path = strhtml_pro.Substring(si2, (si3 - si2)) '取得屬性值
'因跨網域取資料的關西,所以搜尋對方的boardfile資料夾,來判斷對方是上傳到自己的server
If str_path.IndexOf("/boardfile/") <> -1 Then
strhtml_tag = strhtml_tag.Replace(str_path, "http://????" + str_path) 'http://???? 請修改為你自己要替換的網址
End If
str_temp2 = fun_string1(str_temp2, strhtml_tag)
End If
Loop
Return str_temp2
End Function

Function fun_string1(ByVal ori_string As String, ByVal addstring As String) As String
If ori_string = "" Then
ori_string = addstring
Else
ori_string = ori_string & addstring
End If
Return ori_string
End Function

P.S 如果要清除所有Html標籤,可以寫成 Dim regex As Regex = New Regex("<[^>]*>")

JavaScript版本:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Pragma" content="No-cache" />
<title>Regular expression 抓 HTML 標籤屬性與內容</title>
<script type="text/javascript">
var r,str_cont;
//假設str_cont是取得後的資料
str_cont='附件:<a href="/boardfile/file/osa/%E6%B4%BB%E5%8B%95%E8%AA%AA%E6%98%8E1031231.pdf" target="_blank">活動說明</a>、<a href="/boardfile/file/cc/%E5%AF%A6%E6%96%BD%E8%BE%A6%E6%B3%951030930.docx" target="_blank">實施辦法</a><br/><img alt="活動海報" src="/boardfile/file/cc/poster1031003.jpg" width="640" height="902" border="0" title="活動海報" /><br/>一、為推廣並落實資訊教育,以及展示政府推動行政電子化之成效,臺北市電腦商業同業公會結合國內相關公會共同組成資訊月活動委員會,規劃辦理103年資訊月全國巡迴展示活動。';
r = /<a(.*?)>(.*?)<\/a\s*>/;
str_cont = funbody(str_cont,r,'href='); //a標籤
r = /<img(.*?)>/;
str_cont = funbody(str_cont,r,'src='); //img標籤
document.write(str_cont);
function funbody(cont,Regex_where,edit_pro){
    var x1=0, str_temp1='', str_temp2='', str_path='', strhtml_tag='', strhtml_pro='', si1=0, si2=0, si3=0, arr;
while (true) {
x1=cont.search(Regex_where);
if(x1 == -1){
   str_temp2 = fun_string1(str_temp2, cont);
break; //如果找不到a標籤就跳出
}else if (x1!=0){
str_temp1=cont.substring(0, x1); //第一次取出html標籤前的字串
str_temp2 = fun_string1(str_temp2, str_temp1);
cont=cont.replace(str_temp1,'');
   };
arr = Regex_where.exec(cont);
if (arr != null){
strhtml_tag=arr[0];
strhtml_pro=arr[1];//屬性
cont=cont.replace(strhtml_tag,'');

si1=strhtml_pro.indexOf(edit_pro);
si2=(strhtml_pro.indexOf('"',si1))+1; //第一個雙引號
si3=strhtml_pro.indexOf('"',si2); //第二個雙引號
str_path=strhtml_pro.substring(si2, si3);
                        //因跨網域取資料的關西,所以搜尋對方的boardfile資料夾,來判斷對方是上傳到自己的server
if (str_path.indexOf('/boardfile/')!=-1){
                                //http://???? 請修改為你自己要替換的網址
strhtml_tag=strhtml_tag.replace(str_path,'http://????'+str_path);
};
str_temp2 = fun_string1(str_temp2, strhtml_tag);
};
};
return str_temp2;
};

function fun_string1(ori_string,addstring){
if (ori_string =='') {
ori_string = addstring;
}else{
ori_string = ori_string + addstring;
};
return ori_string;
};
</script>
</head>
<body >

</body>
</html>