C# Substring的用法

方法1   Substring(Int32)       从此实例检索子字符串。 子字符串在指定的字符位置开始并一直到该字符串的末尾。

方法2   Substring(Int32, Int32)   从此实例检索子字符串。 子字符串从指定的字符位置开始且具有指定的长度。

参数一:起始位置(从0开始)

参数二:指定长度

用法:string变量名.Substring(参数一, 参数二);

举例:

string s = "hello world";

string ss;

例子1 //从指定位置开始到结尾的字符串(0位开始)

int i=1;

ss = s.Substring(i);

ss = "ello world"

例子2 //从指定位置开始取固定长度的字符串

int i=1;

ss = s.Substring(i,3);

ss = "ell"

例子3 //返回左边的i个字符

int i=5;

ss = s.Substring(0,i)

ss = "hello"

例子4 //返回右边的i个字符

int i=5;

ss = s.Substring(s.Length-i,i);

ss = "world"

例子5 //返回两个特定字符之间的字符串

int IndexofA = s.IndexOf(‘e‘); //字符串的话总以第一位为指定位置

int IndexofB = s.IndexOf(‘r‘);

ss = s.Substring(IndexofA + 1, IndexofB - IndexofA -1);

ss = "llo wo"

时间: 2024-08-30 07:59:09

C# Substring的用法的相关文章

substring、sunstr、Substring的用法

一:在js中截取字符串的方法有两个:substring和sunstr 1.方法: substring(int stringIndex,[int endIndex]) 截取从索引为stringIndex到索引为endIndex之间的字符 substr(int stringIndex,[int Length]) 截取从索引为2开始后面的4个字符 2.注意事项: (1).这里的索引是从0开始的 (2).substring后的参数starIndex是必须填写的,tendIndex是可选参数 (3).su

JAVA中字符串函数subString的用法小结

本篇文章主要是对JAVA中字符串函数subString的用法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 String str; str=str.substring(int beginIndex);截取掉str从首字母起长度为beginIndex的字符串,将剩余字符串赋值给str: str=str.substring(int beginIndex,int endIndex);截取str中从beginIndex开始至endIndex结束时的字符串,并将其赋值给str; demo:

IndexOf、LastIndexOf、Substring的用法(有些名字大小写写错了)

IndexOf.LastIndexOf.Substring的用法 今天遇到截取字符串的问题,在网上查了IndexOf.LastIndexOf.Substring这三种截取字符串的使用总结如下:   String.IndexOf String.IndexOf 方法 (Char, Int32, Int32)报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置.String.IndexOf(value, startIndex, count) 参数value:要查

开发路程(4):C#中的SubString()的用法

先看语法: String.SubString(int index,int length)     index:开始位置,从0开始       length:你要取的子字符串的长度 例子: 1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 namespace str_sub 6 { 7 class Program 8 { 9 static void Main(string[] args) 10

substring()的用法

例一: <script type="text/javascript"> var str="Hello world!" document.write(str.substring(3)) </script> 输出: lo world! 例二: <script type="text/javascript"> var str="Hello world!" document.write(str.sub

java中substring的用法

substring 1.public String substring(int beginIndex).     返回一个新的字符串,它是此字符串的一个子字符串.该子字符串始于指定索引处的字符,一直到此字符串末尾. 参数: beginIndex - 开始处的索引(包括). 返回: 指定的子字符串. 例如: "unhappy".substring(2) returns "happy" "Harbison".substring(3) returns

substring的用法

public String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串.该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符.因此,该子字符串的长度为 endIndex-beginIndex. 示例: "hamburger".substring(4, 8) returns "urge" "smiles".substring(

sql server中substring的用法

SQL 中的 substring 函数是用来截取一个栏位资料中的其中一部分. 例如,我们需要将字符串'abdcsef'中的'abd'给提取出来,则可用substring 来实现: select substring('abdcsef',1,3) 结果: 'abd' 括号中数字'1'表示截取的起始位置是从该字符串第一个字符开始,'3'表示截取后得到的字符串长度为3个字符. 这是'substring'最基础的语法,当然,我们的需求有时候会变得比较复杂,例如以下例子: 我们只想要得到'roomno'中的

IndexOf、LastIndexOf、Substring的用法

今天遇到截取字符串的问题,在网上查了IndexOf.LastIndexOf.Substring这三种截取字符串的使用总结如下:   String.IndexOf String.IndexOf 方法 (Char, Int32, Int32)报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置.String.IndexOf(value, startIndex, count) 参数value:要查找的 Unicode 字符. startIndex:搜索起始位置.