C#实现的ReplaceFirst和ReplaceLast
原文:C#实现的ReplaceFirst和ReplaceLast
ReplaceFirst:
public static string ReplaceFirst(string input, string oldValue, string newValue)
{
Regex regEx = new Regex(oldValue, RegexOptions.Multiline);
return regEx.Replace(input, newValue==null?"":newValue, 1);
}
注意:如果替换的旧值中有特殊符号,替换将会失败,解决办法 例如特殊符号是“(”: 要在调用本方法前加oldValue=oldValue.Replace("(","//(");
ReplaceLast:
public static string ReplaceLast(string input, string oldValue, string newValue)
{
int index = input.LastIndexOf(oldValue);
if (index < 0)
{
return input;
}
else
{
StringBuilder sb = new StringBuilder(input.Length - oldValue.Length + newValue.Length);
sb.Append(input.Substring(0, index));
sb.Append(newValue);
sb.Append(input.Substring(index + oldValue.Length,
input.Length - index - oldValue.Length));
return sb.ToString();
}
}
C#实现的ReplaceFirst和ReplaceLast的更多相关文章
- replace、replaceAll、replaceFirst的区别详解
String s = "my.test.txt"; System.out.println(s.replace(".", "#")); Sys ...
- java--字符串替换replace,replaceAll,replaceFirst
1.字符串替换,replace string s="abcdfersabcdsgacabc"; 将字符串中的abc替换成bdf s.replace("abc", ...
- replace()、replaceFirst()和replaceAll()的区别
1.replace() String str= "mesquite in your cellar" str.replace('e', 'o') returns "mosq ...
- Matcher.replaceFirst(String replacement)
java.util.regex.Matcher.replaceFirst(String replacement)方法是用来进行字符串的替换操作. public String replaceFirst( ...
- java字符串的替换replace、replaceAll、replaceFirst的区别详解
如果不是刚刚复习了下正则表达式,我可能也不会注意到,原来String的replaceAll跟replaceFirst用到了正则表达式! 不多解释,看代码: String s = "my.te ...
- java String中的replace(oldChar,newChar) replace(CharSequence target,CharSequence replacement) replaceAll replaceFirst 面试题:输入英文语句,单词首字符大写后输出 char String int 相互转换
package com.swift; import java.util.Scanner; public class FirstChat_ToCaps_Test { public static void ...
- replace、replaceAll、replaceFirst
replace.replaceAll.replaceFirst这三个函数会java的同学估计都用过,笔者已经用了2年多,可是,我们真的懂他们吗? 概述一下他们三个的用法: · replace(Char ...
- java字符串的替换replace、replaceAll、replaceFirst的区别
看代码: String s = "my.test.txt"; System.out.println(s.replace(".", "#")) ...
- Java中替换字符串中特定字符,replaceAll,replace,replaceFirst的区别
使用“;”替换过字符串中的“,” public class Test01 {public static void main(String[] args) {String number = " ...
随机推荐
- innobackupex:Error:xtrabackup child process has died at /usr/bin/innobackupex
使用innobackupex进行数据库备份,报如下错误:innobackupex --compress --parallel=4 --user=root --password=yoon /expo ...
- 从Keil 4升级到Keil 5的工程,想返回来用Keil 4打开
情景描述: 笔者电脑程序Keil 4升级到Keil 5,相应地,原来项目上的工程也在第一次用Keil 5打开的时候进行了升级.之后,由于客户需要开发资料,其版本为Keil 4,我尝试着用Keil 4打 ...
- ASP.NET MVC 学习第二天
今天使用mvc完成简单的增删改,内容比较简单,来熟悉一下mvc,数据库操作是用前面的ef,也算是温习一下ef吧. 新建mvc项目,在项目中的Models内添加ef,我这里只操作一下简单的user表.里 ...
- inputstream和outputstream读写数据模板代码
//读写数据模板代码 byte buffer[] = new byte[1024]; int len=0; while((len=in.read(buffer))>0){ out.write(b ...
- Version of SQLite used in Android?
sing the emulators (adb shell sqlite3 --version): SQLite 3.7.11: 19-4.4-KitKat 18-4.3-Jelly Bean 17- ...
- OpenWrt固件刷入后串口终端没有反应的问题
[路由器开发板硬件固件配置] MTK双频:MT7620a + MT7612e 内存:256 MB 闪存:16 MB 固件:MTK自带SDK中的OpenWrt固件(mtksdk-openwrt-2.6. ...
- UVA 10954 Add All 哈夫曼编码
题目链接: 题目 Add All Time Limit:3000MS Memory Limit:0KB 问题描述 Yup!! The problem name reflects your task; ...
- BZOJ 4031: [HEOI2015]小Z的房间 Matrix-Tree定理
题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=4031 题解: Matrix-tree定理解决生成树计数问题,其中用到高斯消元法求上三角矩 ...
- [转载]点评阿里云、盛大云等国内IaaS产业
免责声明: 本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除. 原文作者:刘黎明 原文地址:http://www.chinacloud.org ...
- iOS7 兼容及部分细节
1:statusBar字体为白色 在plist里面设置View controller-based status bar appearance 为 NO:设置statusBarStyle 为 UISta ...