如何得到一个字符串的右9位?

解决方案 »

  1.   

    private void button1_Click(object sender, System.EventArgs e)
    {
    string s = "abcdefghijklmn";
    string s2 = s.Substring(s.Length - 9);
    MessageBox.Show(s2);
    }
      

  2.   

    [VB.net]
    strNew = Right(strSrc,9)

    strNew = strSrc.SubString(strSrc.Length - 9)[C#]
    strNew = strSrc.SubString(strSrc.Length - 9);注意:SubString方法会在当strSrc为空或长度小于9时抛出异常,而VB.net的Right函数则不会
    所以,完善一下应该是这样:[VB.net]
    If strSrc = "" OrElse strSrc.Length < 9 then _
        strNew = String.Empty Else _
        strNew = strSrc.SubString(strSrc.Length - 9)[C#]
    if (strSrc == "" || strSrc.Length < 9) 
        strNew = string.Empty else
        strNew = strSrc.SubString(strSrc.Length - 9);
      

  3.   

    String.Substring 方法 (Int32, Int32)  [C#]
    从此实例检索子字符串。子字符串从指定的字符位置开始且具有指定的长度。[Visual Basic]
    Overloads Public Function Substring( _
       ByVal startIndex As Integer, _
       ByVal length As Integer _
    ) As String[C#]
    public string Substring(
       int startIndex,
       int length
    );[C++]
    public: String* Substring(
       int startIndex,
       int length
    );[JScript]
    public function Substring(
       startIndex : int,
       length : int
    ) : String;参数
    startIndex 
    子字符串的起始位置的索引。 
    length 
    子字符串中的字符数。 
    返回值
    一个 String,等效于此实例中从 startIndex 开始的、长度为 length 的子字符串。- 或 -如果 startIndex 与此实例的长度相等且 length 为零,则为 Empty。备注
    startIndex 从零开始。
    示例
    [Visual Basic] 
    Dim myString As String = "abc"
    Dim test1 As Boolean = String.Compare(myString.Substring(2, 1), "c") = 0 ' This is true.
    myString.Substring(3, 1) ' This throws ArgumentOutOfRangeException.
    Dim test2 As Boolean = String.Compare(myString.Substring(3, 0), String.Empty) = 0 ' This is true.
    [C#] 
    String myString = "abc";
    bool test1 = String.Compare(myString.Substring(2, 1), "c") == 0; // This is true.
    myString.Substring(3, 1); // This throws ArgumentOutOfRangeException.
    bool test2 = String.Compare(myString.Substring(3, 0), String.Empty) == 0; // This is true.