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 = " ... 
随机推荐
- 【转】你需要知道的Python用法
			在使用Python多年以后,我偶然发现了一些我们过去不知道的功能和特性.一些可以说是非常有用,但却没有充分利用.考虑到这一点,我编辑了一些的你应该了解的Pyghon功能特色. 带任意数量参数的函数 你 ... 
- WPF-控件-将ListBox条目水平排列
			<Grid Margin="6"> <ListBox> <!--ItemsPanel--> <ListBox.ItemsPanel> ... 
- android LayoutInflater.inflate()的参数介绍
			LayoutInflater.inflate()的作用就是将一个xml定义的布局文件实例化为view控件对象: 与findViewById区别: LayoutInflater.inflate是加载一个 ... 
- CSS3 transition 属性 过渡效果
			<!DOCTYPE html> <html> <head> <style> div { width:100px; height:100px; backg ... 
- 【quartz】 入门
			把技术债务给还了,首先来一个最简单的demo: 2.x版比1.x有很多改进,1.x基于fw1.2: 2.x基于fw3.5以上:语法上有很大的不同,摒弃了很多1.x的很多东西: 直接以2.x来demo ... 
- win2008修改最大远程桌面连接数
			win2008修改最大远程桌面连接数 运行——gredit.msc——管理模板——windows组件——远程桌面服务——远程桌面回话主机——连接——限制连接的数量——修改为999999 
- range,shuffle,str_shuffle
			print_r(range(1,20)); 输出,range产生 Array( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ... 
- 如何用js获取当前url的参数值
			<script language = javascript> function request(paras){ var url = location.href; var paraStrin ... 
- Careercup - Google面试题 - 4716965625069568
			2014-05-06 00:17 题目链接 原题: Given a -D matrix represents the room, obstacle and guard like the followi ... 
- java集合类(三)About Iterator & Vector(Stack)
			接上篇:java集合类学习(二) Talk about “Iterator”: 任何容器类,在插入元素后,还需要取回元素,因为这是容器的最基本工作.对于一般的容器,插入有add()相关方法(List, ... 
