C#中处理字符串对象的函数

1.Substring

  Substring(startIndex);

  startIndex
Type: System.Int32
The zero-based starting character position of a substring in this instance. 

   string str="ABCDEFG";

   str.Substring(2); // returns "CDEFG"

   str.Substring(7);//returns an empty string

   str.Substring(8);//Causes an ArgumentOutOfRangeException 

  

  Substring(startIndex,length);

     startIndex
Type: System.Int32
The zero-based starting character position of a substring in this instance.
     length
Type: System.Int32
The number of characters in the substring. 
str.Substring(2,1); // returns "C"

        str.Substring(7,0);//returns an empty string

        str.Substring(8,1);//This throws ArgumentOutOfRangeException  

2.LastIndexOf

  value

Type: System.String
The string to seek.  

string str="<b>This is bold text</b>";
int endTagStartPosition = item.LastIndexOf("</"); // Remove the identified section, if it is valid.
if (endTagStartPosition >= 0 ) item = item.Substring(0, endTagStartPosition);

int openTagEndPosition = item.IndexOf(">"); // Remove the identified section, if it is valid.
if (openTagEndPosition >= 0) item = item.Substring(openTagEndPosition + 1);
Console.WriteLine("str: " + item); //This is bold text

   

posted @ 2011-11-29 23:15  sulin  阅读(278)  评论(0编辑  收藏  举报