C#中一些字串操作的常用用法,c#編碼和解碼

來源:互聯網
上載者:User

//獲得漢字的區位碼
  byte[] array = new byte[2];
  array = System.Text.Encoding.Default.GetBytes("啊");

 

int i1 = (short)(array[0] - ''/0'');
  int i2 = (short)(array[1] - ''/0'');

//unicode解碼方式下的漢字碼
  array = System.Text.Encoding.Unicode.GetBytes("啊");
  i1 = (short)(array[0] - ''/0'');
  i2 = (short)(array[1] - ''/0'');

//unicode反解碼為漢字
  string str = "4a55";
  string s1 = str.Substring(0,2);
  string s2 = str.Substring(2,2);

int t1 = Convert.ToInt32(s1,16);
  int t2 = Convert.ToInt32(s2,16);

array[0] = (byte)t1;
  array[1] = (byte)t2;

string s = System.Text.Encoding.Unicode.GetString(array);

//default方式反解碼為漢字
  array[0] = (byte)196;
  array[1] = (byte)207;
  s = System.Text.Encoding.Default.GetString(array);

//取字串長度
  s = "iam方槍槍";
  int len = s.Length;//will output as 6
  byte[] sarr = System.Text.Encoding.Default.GetBytes(s);
  len = sarr.Length;//will output as 3+3*2=9

//字串相加
  System.Text.StringBuilder sb = new System.Text.StringBuilder("");
  sb.Append("i ");
  sb.Append("am ");
  sb.Append("方槍槍");

/////////////////////////////////////////////////////////////////////

 

string --> byte array

byte[] data=Syste.Text.Encoding.ASCII.GetBytes(string);

string --> byte

byte data = Convert.ToByte(string);

byte[]-->string

string string = Encoding.ASCII.GetString( bytes, 0, nBytesSize );

 
C# URL編碼與解碼

--------------------------------------------------------------------------------
 
最近做了一個網站統計分析系統,特別在搜尋引擎分析那一部分費了不少時間,從已編碼的URL中提取搜尋關鍵詞再進行解碼。主流的搜尋引擎中,URL所使用的字元編碼都不同,比如,baidu預設是gb2312,google預設是utf-8。而百度使用者可以指定utf-8編碼。以這兩個搜尋引擎分析,URL可能是gb2312,也可能是utf-8。頭大了!經過幾番琢磨。得出一個暫時可以對付的簡單方法。

以下是引用片段:
//oStr是UrlEncode編碼字串
Encoding gb2312 = Encoding.GetEncoding("gb2312"); 
Encoding utf8 = Encoding.UTF8; 
//首先用utf-8進行解碼
string key = HttpUtility.UrlDecode(oStr, utf8);
// 將已經解碼的字元再次進行編碼.
string encode = HttpUtility.UrlEncode(key, utf8).ToLower();
//與原來編碼進行對比,如果不一致說明解碼未正確,用gb2312進行解碼
if (oStr != encode)
    key = HttpUtility.UrlDecode(oStr, gb2312);
 
用這樣的方法基本上可以解決了URL的編碼解碼問題了。

asp.net(C#實現) 編碼解碼(HtmlEncode與HtmlEncode)時間:2009-10-16 10:37來源:未知 作者:admin 點擊:246次我要投稿
Default.aspx:

<%@ Page ValidateRequest="false" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="test_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>asp.net(C#) 編碼解碼(HtmlEncode與HtmlEncode)</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblShow" runat="server" Text="Label"></asp:Label>
        <asp:TextBox ID="txtInput" runat="server" Height="194px" TextMode="MultiLine" Width="305px"></asp:TextBox>
        <asp:Button ID="btnOk" runat="server" Text="提交" OnClick="btnOk_Click" /></div>
    </form>
</body>
</html>

Default.aspx.cs:
 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/***********************編碼研究***********************
 * 1.預設情況是不允許使用者在TextBox中輸入html標籤的,
 *   如果需要輸入,設定Page的ValidateRequest="false"
 * 2.可以把輸入的html標籤,比如<input>直接存放在資料庫中,
 *   只是在輸出的時候編碼,防止原樣輸出打亂頁面配置.或者呈現html元素.
 *****************************************************/
public partial class test_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        lblShow.Text = htmlEncode(txtInput.Text);
    }
    /// <summary>
    /// 對輸入的html編碼,同時對斷行符號與空格進行轉換
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public string htmlEncode(string str)
    {
        return Server.HtmlEncode(str).Replace("/n", "<br/>").Replace(" ", " ");
    }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.