.NET string字符串的截取、移除、替換、插入


  在實際開發中經常要用到string的各種截取等操作,在這里總結自己認為經常出現的.NET 字符串的截取、移除、替換、插入操作,方面以后查閱。

前台代碼:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="string.aspx.cs" Inherits="demo._string" %>

<!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>無標題頁</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    
    </div>
    </form>
</body>
</html>


后台代碼:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace demo
{
    public partial class _string : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                string a = "123a4a4b5b6";
                TextBox1.Text = a;
            }
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string b = TextBox1.Text;

            ////截取前兩位 123a4a4b5b6   12
            //string c = b.Substring(0, 2);
            //TextBox1.Text = c;

            ////截取后兩位  123a4a4b5b6   56
            //string c = b.Substring(b.Length - 2, 2);
            //TextBox1.Text = c;

            ////截取最后一個字符“a”后面的3位字符 123a4a4b5b6     a4b
            //string c = b.Substring(b.LastIndexOf("a"), 3);
            //TextBox1.Text = c;

            ////截取最后一個字符“a”后面所有的字符  123a4a4b5b6     a4b5b6
            //string c = b.Substring(b.LastIndexOf("a"));
            //TextBox1.Text = c;

            ////截取最后一個字符“a”前面所有的字符  123a4a4b5b6    123a4 
            //string c = b.Remove(b.LastIndexOf("a"));
            //TextBox1.Text = c;
            //// 截取最后一個字符“a”前面所有的字符  123a4a4b5b6    123a4 
            //string c = b.Substring(0, b.LastIndexOf("a"));
            //TextBox1.Text = c;

            ////截取最后一個字符“a”前面的3位字符  123a4a4b5b6    123a4    3a4
            //string c = b.Remove(b.LastIndexOf("a"));
            //string d = c.Remove(0, c.Length - 3);
            //TextBox1.Text = d;
            //// 截取最后一個字符“a”前面的3位字符  123a4a4b5b6     3a4    
            //string c = b.Substring(2,b.LastIndexOf("a")-2);
            //TextBox1.Text = c;

            ////截取第一個字符"b"前面所有的字符  123a4a4b5b6  123a4a4
            //int index = b.IndexOf("b");
            //string c = b.Substring(0,index);
            //TextBox1.Text = c;

            ////截取第一個字符"b"前面的3位字符  123a4a4b5b6   4a4
            //string c = b.Substring(0, b.IndexOf("b"));
            //string d = c.Substring(c.Length - 3, 3);
            //TextBox1.Text = d;
            ////截取第一個字符"b"前面的3位字符  123a4a4b5b6   4a4
            //string c = b.Substring(b.IndexOf("b")-3,3);
            //TextBox1.Text = c;

            ////截取第一個字符"b"后面所有的字符  123a4a4b5b6   b5b6
            //int index = b.IndexOf("b");
            //string c = b.Substring(index,b.Length-index);
            //TextBox1.Text = c;
            //////截取第一個字符"b"后面3位字符  123a4a4b5b6   b5b
            //int index = b.IndexOf("b");
            //string c = b.Substring(index, 3);
            //TextBox1.Text = c;








            ////移除從第三位開始的3個字符 123a4a4b5b6   1234b5b6
            //string c = b.Remove(3, 3);
            //TextBox1.Text = c;
            ////移除字符串中的所有字符b;  123a4a4b5b6     123a4a456
            //string c = b.Replace("b", "");
            //TextBox1.Text = c;

            ////移除字符串中的第一個字符a   123a4a4b5b6  1234a4b5b6  
            //int index = b.IndexOf("a",1);
            //b=b.Remove(index, 1);
            //TextBox1.Text = b;

            ////移除字符串中的第一個字符a的后兩位   123a4a4b5b6  123a4b5b6  
            //int index = b.IndexOf("a", 1);
            //b = b.Remove(index, 2);
            //TextBox1.Text = b;

            ////移除字符串中的第二個字符a   123a4a4b5b6   123a4b5b6
            //int index = b.IndexOf("a", 2);
            //b = b.Remove(index, 2);
            //TextBox1.Text = b;

            ////移除字符串中的第二個字符a的后兩位   123a4a4b5b6    123a4b5b6 
            //int index = b.IndexOf("a", 1);
            //b = b.Remove(index, 2);
            //TextBox1.Text = b;

            ////移除字符串中最后一個字符b   123a4a4b5b6      123a4a4b56 
            //int index = b.LastIndexOf("b");     
            //string c = b.Remove(b.LastIndexOf("b"),1);
            //TextBox1.Text = c;

            ////移除字符串中最后一個字符b的后兩位   123a4a4b5b6      123a4a4b5 
            //int index = b.LastIndexOf("b");
            //string c = b.Remove(b.LastIndexOf("b"), 2);
            //TextBox1.Text = c;










            ////替換字符串中所有的字符“a”,為“b”; 123a4a4b5b6     123b4b4b5b6
            //string c = b.Replace("a", "b");
            //TextBox1.Text = c;

            ////替換字符中的第一個字符a為R  123a4a4b5b6    123R4a4b5b6
            //int index = b.IndexOf("a");
            //string c= b.Remove(index, 1).Insert(index, "R");
            //TextBox1.Text = c;

            ////替換字符中的最后一個字符a為R  123a4a4b5b6    123a4R4b5b6
            //int index = b.LastIndexOf("a");
            //string c = b.Remove(index, 1).Insert(index, "R");
            //TextBox1.Text = c;










            ////插入I在第一個a之前  123a4a4b5b6  123Ia4a4b5b6
            //int index = b.IndexOf("a");
            //string c = b.Insert(index, "I");
            //TextBox1.Text = c;

            ////插入I在第一個a之后  123a4a4b5b6   123aI4a4b5b6
            //int index = b.IndexOf("a");
            //string c = b.Insert(index+1, "I");
            //TextBox1.Text = c;

            ////插入I在最后一個a之前  123a4a4b5b6  123a4Ia4b5b6
            //int index = b.LastIndexOf("a");
            //string c = b.Insert(index, "I");
            //TextBox1.Text = c;

            //插入I在最后一個a之后  123a4a4b5b6  123a4aI4b5b6
            int index = b.LastIndexOf("a");
            string c = b.Insert(index + 1, "I");
            TextBox1.Text = c;

        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM