在实际开发中经常要用到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 + , "I");
TextBox1.Text = c; }
}
}

.NET string字符串的截取、移除、替换、插入的更多相关文章

  1. String字符串的截取

    根据某个字段将字符串分割成绩部分 String str = "string number one 1/9/0"; //将字符串由/ 截取成绩部分 String[] strs = s ...

  2. C++ 标准库string字符串的截取

    标准库的string有一个substr函数用来截取子字符串.一般使用时传入两个参数,第一个是开始的坐标(第一个字符是0),第二个是截取的长度. #include <iostream> #i ...

  3. String 字符串递归截取字节字符串

    public static String idgui(String s,int num)throws Exception{ int changdu = s.getBytes("UTF-8&q ...

  4. String字符串截取跟替换经典案例

    分享下今天的一个面试题吧!不算有难度,但是没做出来 题目:将String  str="姓名:武亚伟,年龄:27,地址:西安市": 输出结果为:姓名=武亚伟 年龄=27 地址=西安市 ...

  5. String常用使用方法,1.创建string的常用3+1种方式,2.引用类型使用==比较地址值,3.String当中获取相关的常用方法,4.字符串的截取方法,5.String转换常用方法,6.切割字符串----java

    一个知识点使用一个代码块方便查看 1.创建string的常用3+1种方式 /* 创建string的常用3+1种方式 三种构造方法 public String():创建一个空字符串,不含有任何内容: p ...

  6. python中字符串操作--截取,查找,替换

    python中,对字符串的操作是最常见的,python对字符串操作有自己特殊的处理方式. 字符串的截取 python中对于字符串的索引是比较特别的,来感受一下: s = '123456789' #截取 ...

  7. 2shell中处理字符串,字符串的截取、替换

    0.字符串的小知识点 1.字符串的截取 1.1从指定位置开始截取 1.2 从指定字符(子字符串)开始截取 1.3字符串截取的总结 1.4 按指定要求截取 2.字符串的拼接 3.字符串的替换 0.字符串 ...

  8. go语言学习--string、int、int64互相转换,字符串的截取,数组和字符串的转换

    下面总结了go中常用的转换 #string到int int,err:=strconv.Atoi(string) #string到int64 int64, err := strconv.ParseInt ...

  9. 【转载】C#中string类使用Replace方法来替换字符串

    在C#的字符串操作过程中,有时候需要替换字符串中的某个子字符串,此时就可以使用到字符串类自带的Replace方法来实现,Replace方法将查找到所有符合被替换的子字符串,然后将之全部替换为目标字符串 ...

随机推荐

  1. Netty入门学习

    一.他山之石 Netty实现原理浅析 http://www.importnew.com/15656.html netty线程模型 http://www.infoq.com/cn/articles/ne ...

  2. 常用linux 命令 -字符串相关

    参考网络文章,个人工作总结 题记:一般对字符串的操作有以下几种:求长度,截取字符串,拼接字符串,找字符串中某个字符的索引 1 expr 命令 1.1 定义 man 手册 Print the value ...

  3. nodejs学习笔记一

    一.node版本的更新命令 node有一个模块叫n,是专门用来管理node.js的版本的. 首先安装n模块: npm install -g n 第二步: 升级node.js到最新稳定版 n stabl ...

  4. 分页查询和分页缓存查询,List<Map<String, Object>>遍历和Map遍历

    分页查询 String sql = "返回所有符合条件记录的待分页SQL语句"; int start = (page - 1) * limit + 1; int end = pag ...

  5. 项目vue2.0仿外卖APP(四)

    组件拆分 先把项目搭建时生成的代码给清了吧 现在static目录下引入reset.css 接着在index.html引入,并且设置<meta> 有时候呢,为了让代码符合我们平时的编码习惯, ...

  6. MongoDB数据库未授权访问漏洞及加固

    1.漏洞危害 开启MongoDB服务时不添加任何参数时,默认是没有权限验证的,登录的用户可以通过默认端口无需密码对数据库任意操作(增删改高危动作)而且可以远程访问数据库. 2.漏洞成因 在刚安装完毕的 ...

  7. 理解 Delphi 的类(八) - 关于类的定义

      //标准语法   TMyClass1 = class(TObject)   end;   //如果是继承自 TObject 可以省略   TMyClass2 = class   end;   // ...

  8. 2016 Google code jam 答案

    二,RoundC import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundE ...

  9. iOS6_自动约束 Constraints

    取消Constraints(约束) 问题描述:xib文件设计的时候控件已经摆好位置,但是每次调试的时候控件的位置又乱了 解决方法:选中 xib文件,在右侧第一项(Identity and type)的 ...

  10. ffmpeg+x264 Windows MSVC 静态编译

    尝试ubuntu和win下mingw编译版本,但都在Vistual Studio链接时因为依赖 libgcc.a, libmingw.a, libmingwex.a 会与mscrt 有符号冲突. 最后 ...