好像是第二种效率高一点,取str字符串中123左边的所有字符:第一种Between(str,"","123"),而第二种是Between(str,null,"123")。

public static string Between(string str, string strLeft, string strRight) //取文本中间
{
if (str == null || str.Length == 0) return "";
if (strLeft != "")
{
int indexLeft = str.IndexOf(strLeft);//左边字符串位置
if (indexLeft < 0) return "";
indexLeft = indexLeft + strLeft.Length;//左边字符串长度
if (strRight != "")
{
int indexRight = str.IndexOf(strRight, indexLeft);//右边字符串位置
if (indexRight < 0) return "";
return str.Substring(indexLeft, indexRight - indexLeft);//indexRight - indexLeft是取中间字符串长度
}
else return str.Substring(indexLeft, str.Length - indexLeft);//取字符串右边
}
else//取字符串左边
{
int indexRight = str.IndexOf(strRight);
if (indexRight <= 0) return "";
else return str.Substring(0, indexRight);
}
}

  

public static string Between2(string str, string strLeft, string strRight) //取文本中间
{
if (str == null || str.Length == 0) return "";
if (strLeft != null)
{
int indexLeft = str.IndexOf(strLeft);//左边字符串位置
if (indexLeft < 0) return "";
indexLeft = indexLeft + strLeft.Length;//左边字符串长度
if (strRight != null)
{
int indexRight = str.IndexOf(strRight, indexLeft);//右边字符串位置
if (indexRight < 0) return "";
return str.Substring(indexLeft, indexRight - indexLeft);//indexRight - indexLeft是取中间字符串长度
}
else return str.Substring(indexLeft, str.Length - indexLeft);//取字符串右边
}
else//取字符串左边
{
if (strRight == null) return "";
int indexRight = str.IndexOf(strRight);
if (indexRight <= 0) return "";
else return str.Substring(0, indexRight);
}
}

C# 取字符串中间文本 取字符串左边 取字符串右边的更多相关文章

  1. SQL server 存储过程 C#调用Windows CMD命令并返回输出结果 Mysql删除重复数据保留最小的id C# 取字符串中间文本 取字符串左边 取字符串右边 C# JSON格式数据高级用法

    create proc insertLog@Title nvarchar(50),@Contents nvarchar(max),@UserId int,@CreateTime datetimeasi ...

  2. [Python] 糗事百科文本数据的抓取

    [Python] 糗事百科文本数据的抓取 源码 https://github.com/YouXianMing/QiuShiBaiKeText import sqlite3 import time im ...

  3. [CLR via C#]14. 字符、字符串和文本处理

    一.字符 在.NET Framewole中,字符总是表示成16位Unicode代码值,这简化了国际化应用程序的开发. 每个字符都表示成System.Char结构(一个值类型) 的一个实例.System ...

  4. SqlSever基础 ltrim函数 除去字符串左边的空格,右边的中间的不管

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  5. 《Python CookBook2》 第一章 文本 - 过滤字符串中不属于指定集合的字符 && 检查一个字符串是文本还是二进制

    过滤字符串中不属于指定集合的字符 任务: 给定一个需要保留的字符串的集合,构建一个过滤函数,并可将其应用于任何字符串s,函数返回一个s的拷贝,该拷贝只包含指定字符集合中的元素. 解决方案: impor ...

  6. CLR via C#字符串和文本处理

    一.字符   在.NET Framewole中,字符总是表示成16位Unicode代码值,这简化了国际化应用程序的开发.   每个字符都表示成System.Char结构(一个值类型) 的一个实例.Sy ...

  7. 读书笔记—CLR via C#字符串及文本

    前言 这本书这几年零零散散读过两三遍了,作为经典书籍,应该重复读反复读,既然我现在开始写博了,我也准备把以前觉得经典的好书重读细读一遍,并且将笔记整理到博客中,好记性不如烂笔头,同时也在写的过程中也可 ...

  8. Python3-Cookbook总结 - 第二章:字符串和文本

    第二章:字符串和文本 几乎所有有用的程序都会涉及到某些文本处理,不管是解析数据还是产生输出. 这一章将重点关注文本的操作处理,比如提取字符串,搜索,替换以及解析等. 大部分的问题都能简单的调用字符串的 ...

  9. [转]python3字符串与文本处理

    转自:python3字符串与文本处理 阅读目录 1.针对任意多的分隔符拆分字符串 2.在字符串的开头或结尾处做文本匹配 3.利用shell通配符做字符串匹配 4.文本模式的匹配和查找 5.查找和替换文 ...

  10. python3字符串与文本处理

    每个程序都回涉及到文本处理,如拆分字符串.搜索.替换.词法分析等.许多任务都可以通过内建的字符串方法来轻松解决,但更复杂的操作就需要正则表达式来解决. 1.针对任意多的分隔符拆分字符串 In [1]: ...

随机推荐

  1. 【构造】CDOJ1607 大学生足球联赛

    请自行百度逆时针轮转法 //单循环赛 逆时针轮转法 #include<cstdio> using namespace std; int n,a[70]; int main(){ scanf ...

  2. 【数论】【中国剩余定理】【LCM】hdu1788 Chinese remainder theorem again

    根据题目容易得到N%Mi=Mi-a. 那么可得N%Mi+a=Mi. 两侧同时对Mi取余,可得(N+a)%Mi=0. 将N+a看成一个变量,就可以把原问题转化成求Mi的LCM,最后减去a即可. #inc ...

  3. 【Trie模板】HDU1251-统计难题

    [题意] n统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). [思路] 裸题,不过G++好像会超内存,C++就不会. #include<iostream> #include& ...

  4. Problem J: 零起点学算法105——C语言合法标识符

    #include<stdio.h> #include<ctype.h>//调用isalpha函数 int main() { int n; ]; while(scanf(&quo ...

  5. Hibernate的QBC检索方式

    Hibernate的QBC检索方式 一直习惯了Hibernate的HQL查询,一直也觉得挺方便,对于最近项目里出现的QBC(org.hibernate.Criteria接口)也是报着一种看看的心理,因 ...

  6. Cargo, Rust’s Package Manager

    http://doc.crates.io/ Installing The easiest way to get Cargo is to get the current stable release o ...

  7. FIS常用功能之资源压缩

    fis server start后 资源压缩,只需要使用命令,不需要添加任何配置 fis release --optimize 或: fis release -o 在浏览器访问按F12,观看压缩前后文 ...

  8. 通用Json的处理办法

    1.Json的格式: 对象{"name": "value", "name1": "value1"} 对象包含对象数组{& ...

  9. 怎样用Jenkins触发还有一个Jenkins---Global build solution

    由于上次发的帖子太受欢迎,导致有非常多人问也有很多其它的人想知道.2个不同地域位置的Jenkins怎样自己主动触发相互的Job.当今非常多公司做的产品仅仅是全球化工作的一部分.须要这部分做好以后去做另 ...

  10. animateBackground-plugin

    (function ($) { if (!document.defaultView || !document.defaultView.getComputedStyle) { var oldCurCSS ...