.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;

        }
    }
}

 

posted @ 2016-06-07 09:40  哪啊哪啊神去村  阅读(24850)  评论(0编辑  收藏  举报