.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 + , "I");
TextBox1.Text = c; }
}
}
.NET string字符串的截取、移除、替换、插入的更多相关文章
- String字符串的截取
根据某个字段将字符串分割成绩部分 String str = "string number one 1/9/0"; //将字符串由/ 截取成绩部分 String[] strs = s ...
- C++ 标准库string字符串的截取
标准库的string有一个substr函数用来截取子字符串.一般使用时传入两个参数,第一个是开始的坐标(第一个字符是0),第二个是截取的长度. #include <iostream> #i ...
- String 字符串递归截取字节字符串
public static String idgui(String s,int num)throws Exception{ int changdu = s.getBytes("UTF-8&q ...
- String字符串截取跟替换经典案例
分享下今天的一个面试题吧!不算有难度,但是没做出来 题目:将String str="姓名:武亚伟,年龄:27,地址:西安市": 输出结果为:姓名=武亚伟 年龄=27 地址=西安市 ...
- String常用使用方法,1.创建string的常用3+1种方式,2.引用类型使用==比较地址值,3.String当中获取相关的常用方法,4.字符串的截取方法,5.String转换常用方法,6.切割字符串----java
一个知识点使用一个代码块方便查看 1.创建string的常用3+1种方式 /* 创建string的常用3+1种方式 三种构造方法 public String():创建一个空字符串,不含有任何内容: p ...
- python中字符串操作--截取,查找,替换
python中,对字符串的操作是最常见的,python对字符串操作有自己特殊的处理方式. 字符串的截取 python中对于字符串的索引是比较特别的,来感受一下: s = '123456789' #截取 ...
- 2shell中处理字符串,字符串的截取、替换
0.字符串的小知识点 1.字符串的截取 1.1从指定位置开始截取 1.2 从指定字符(子字符串)开始截取 1.3字符串截取的总结 1.4 按指定要求截取 2.字符串的拼接 3.字符串的替换 0.字符串 ...
- go语言学习--string、int、int64互相转换,字符串的截取,数组和字符串的转换
下面总结了go中常用的转换 #string到int int,err:=strconv.Atoi(string) #string到int64 int64, err := strconv.ParseInt ...
- 【转载】C#中string类使用Replace方法来替换字符串
在C#的字符串操作过程中,有时候需要替换字符串中的某个子字符串,此时就可以使用到字符串类自带的Replace方法来实现,Replace方法将查找到所有符合被替换的子字符串,然后将之全部替换为目标字符串 ...
随机推荐
- Ubuntu 手动更新firefox的flash插件
Ubuntu下 Firefox更新flash插件老是提示失败,自己动手丰衣足食啊. 1.下载tar文件,地址:http://get.adobe.com/cn/flashplayer/?no_redir ...
- python 多环境安装
1.pyenv 安装 curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer ...
- file xxx from install of xxx conflicts with file from xxx
执行安装 rpm -ivh lib64stdc++6-4.6.1-2-mdv2011.0.x86_64.rpm 时提示以下错误: warning: lib64stdc++6-4.6.1-2-mdv20 ...
- JQuery EasyUI DataGrid根据条件设置表格行样式(背景色)
1.javascript定义函数返回样式 <script type="text/javascript"> //根据条件设置表格行背景颜色 function setRow ...
- Ubuntu16.04安装Screenlets
通过添加软件源的方式安装装 sudo add-apt-repository ppa:screenlets/ppa sudo apt-get update sudo apt-get install sc ...
- 各种Android手机Root方法
Root的介绍 谷歌的android系统管理员用户就叫做root,该帐户拥有整个系统至高无上的权利,它可以访问和修改你手机几乎所有的文件,只有root才具备最高级别的管理权限.我们root手机的过程 ...
- 如何设置redis中hash的field的expire ?
redis > hset expire:me name tom (integer) redis > hget expire:me name "tom" redis &g ...
- 安装第三方APP好的站点及解除安全与隐私限制
一.解除安全与隐私限制的任何来源. http://bbs.feng.com/read-htm-tid-10714286.html 接下来,我们就打开终端,然后输入以下命令: sudo spctl ...
- NetBeans建立跳过测试构建的快捷方式
在项目浏览器中右键项目->属性,如图进行设置: 此后按下图即可运行自定义行为:
- 使用Visual Leak Detector for Visual C++ 捕捉内存泄露
什么是内存泄漏? 内存泄漏(memory leak),指由于疏忽或错误造成程序未能释放已经不再使用的内存的情况.内存泄漏并非指内存在物理上的消失,而是应用程序分配某段内存后,由于设计错误,失去了对该段 ...