welcome to java world

c#中字符串截取使用的方法

String substring(int beginIndex) 
String substring(int beginIndex, int endIndex) 
String.Substring (Int32)         子字符串从指定的字符位置开始。 
String.Substring (Int32, Int32) 子字符串从指定的字符位置开始且具有指定的长度。
举例如下:
             string s = "Hello C# World!";
             //s1为从s中截取的位置为3的字符以后的字符子串,3表示子字符串的起始字符位置
            string s1=s.Substring(3);
             //s2为从s中截取的位置为6的字符开始长度为2的字符串,6表示子字符的起始字符位置,2表示子字符长度
            string s2 = s.Substring(6, 2);
结果如下:
lo C# World!
C#
 
int indexOf(String str) 返回第一次出现的指定子字符串在此字符串中的索引。
int indexOf(String str, int fromIndex) 从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。
int lastIndexOf(String str) 返回在此字符串中最右边出现的指定子字符串的索引。 
int lastIndexOf(String str, int fromIndex) 从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。 
int length() 返回此字符串的长度。
boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开始。 
boolean startsWith(String prefix, int toffset) 测试此字符串是否以指定前缀开始,该前缀以指定索引开始。
例如:
   string  str= "C:\\Documents and Settings\\Administrator\\桌面\\new1.jpg" 
str.Substring(0,str.LastIndexOf("\\")+1)+"new"+str.Substring(str.LastIndexOf("\\")+1,
  str.LastIndexOf(".")-str.LastIndexOf("\\")-1)+str.Substring(str.LastIndexOf("."),str.Length-str.LastIndexOf(".")
 
 str.LastIndexOf("\\")——得到最后一个“\\”的索引值 
 str.Substring(0,str.LastIndexOf("\\")+1)——得到  C:\\Documents and Settings\\Administrator\\桌面\\
 str.Substring(str.LastIndexOf("\\")+1,str.LastIndexOf(".")-str.LastIndexOf("\\")-1)  ——得到 new1
str.Substring(str.LastIndexOf("."),str.Length-str.LastIndexOf(".") ——得到 .jpg

posted on 2012-04-25 17:19 SkyDream 阅读(42180) 评论(0)  编辑  收藏 所属分类: C# WinForm

c#中字符串截取使用的方法的更多相关文章

  1. javascript、jquery 、C#、sqlserveer、mysql、oracle中字符串截取的区别和用法

    下标从0开始 ,并且包括起始位 javascript 中字符串截取 : substring(Number start,Number end) var substr = "liuguangfa ...

  2. 【2017-06-27】Js中获取地址栏参数、Js中字符串截取

    一.Js中获取地址栏参数 //从地址栏获取想要的参数 function GetQueryString(name) { var reg = new RegExp("(^|&)" ...

  3. Java中字符串的一些常见方法

    1.Java中字符串的一些常见方法 /** * */ package com.you.model; /** * @author Administrator * @date 2014-02-24 */ ...

  4. JavaScript中字符串截取函数slice()、substring()、substr()

    在js中字符截取函数有常用的三个slice().substring().substr()了,下面我来给大家介绍slice().substring().substr()函数在字符截取时的一些用法与区别吧 ...

  5. javascript中字符串对象常用的方法和属性

    前言 字符串是一种非常重要的数据类型,在Java等面向对象编程语言中,它代表对象类型,而在javascript中它却是一种基本数据类型,在开发的领域中,我们经常会碰到,无论是前端还是后台.比如后台验证 ...

  6. Thinkphp 3.2中字符串截取

    将此方法放到Thinkphp/Common/function.php里/* * 字符串截取函数 * 大白驴 * 2016-11-29 qq 675835721 * */function msubstr ...

  7. Java中字符串indexof() 的使用方法

    Java中字符串中子串的查找共有四种方法(indexof())indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置.如果没有找到子字符串,则返回-1.如果 startind ...

  8. shell中字符串截取

    Linux 的字符串截取很有用.有八种方法. 假设有变量 var="User:123//321:/home/dir" 1. # 号截取,删除左边字符,保留右边字符. [root@z ...

  9. Mysql中字符串正确的连接方法

    虽然SQL server和My sql的语句基本都一致,但是仍然存在一些小区别.就如字符串的连接来说,SQL server中的字符串连接是使用“+”来连接,不带引号sql server是做加法运算.而 ...

随机推荐

  1. OpenJudge / Poj 1044 Date bugs C++

    链接地址: Poj:http://poj.org/problem?id=1044 OpenJudge:http://bailian.openjudge.cn/practice/1044/ 题目: 总时 ...

  2. GCC编译器入门

    GCC(GNU Compiler Collection,GNU编译器套件),是由 GNU 开发的编程语言编译器.它是以GPL许可证所发行的自由软件,也是 GNU计划的关键部分.GCC原本作为GNU操作 ...

  3. Codeforces 612E - Square Root of Permutation

    E. Square Root of Permutation A permutation of length n is an array containing each integer from 1 t ...

  4. Net中的AOP

    .Net中的AOP系列之<单元测试切面>   返回<.Net中的AOP>系列学习总目录 本篇目录 使用NUnit编写测试 编写和运行NUnit测试 切面的测试策略 Castle ...

  5. python multiprocessing 多进程

    ''' 如果要启动大量的子进程,可以用进程池的方式批量创建子进程: ''' def test_task(name): print 'Run task %s (%s)...' % (name, os.g ...

  6. iOS 页面间传值 之 单例传值 , block 传值

    ios 页面间传值有许多,前边已经分享过属性传值和代理传值,今天主要说一下单例传值和 block 传值 单例传值:单例模式一种常用的开发的模式,单例因为在整个程序中无论在何时初始化对象,获取到的都是同 ...

  7. eclipse 安装git插件

    Eclipse上安装GIT插件EGit及使用 博客分类: GIT   一.Eclipse上安装GIT插件EGit Eclipse的版本eclipse-java-helios-SR2-win32.zip ...

  8. Autodesk 2014全套密钥

    AUTODESK 2014通用安装序列号: 666-69696969 667-98989898 400-45454545 066-66666666 AUTODESK 2014全系列产品密钥 001F1 ...

  9. MySQL 性能调优的10个方法

    Mysql的优化方面,一般我们很少去考虑它,即使想到优化一般也更多是程序级别的,比如不要写过于消耗资源的SQL语句,但是除此以外,在整个系统上其实仍然有很多可以优化的地方. 1. 选择合适的存储引擎: ...

  10. 164. Maximum Gap

    题目: Given an unsorted array, find the maximum difference between the successive elements in its sort ...