Remove the last three characters from a string

前端 未结 14 1508
南方客
南方客 2021-01-30 15:22

I want to remove last three characters from a string:

string myString = \"abcdxxx\"; 

Note that the string is dynamic data.

相关标签:
14条回答
  • 2021-01-30 15:57

    Probably not exactly what you're looking for since you say it's "dynamic data" but given your example string, this also works:

    ? "abcdxxx".TrimEnd('x');
    "abc"
    
    0 讨论(0)
  • 2021-01-30 16:03

    Remove the last characters from a string

    TXTB_DateofReiumbursement.Text = (gvFinance.SelectedRow.FindControl("lblDate_of_Reimbursement") as Label).Text.Remove(10)
    

    .Text.Remove(10)// used to remove text starting from index 10 to end

    0 讨论(0)
  • 2021-01-30 16:06
    items.Remove(items.Length - 3)
    

    string.Remove() removes all items from that index to the end. items.length - 3 gets the index 3 chars from the end

    0 讨论(0)
  • 2021-01-30 16:08

    read last 3 characters from string [Initially asked question]

    You can use string.Substring and give it the starting index and it will get the substring starting from given index till end.

    myString.Substring(myString.Length-3)
    

    Retrieves a substring from this instance. The substring starts at a specified character position. MSDN

    Edit, for updated post

    Remove last 3 characters from string [Updated question]

    To remove the last three characters from the string you can use string.Substring(Int32, Int32) and give it the starting index 0 and end index three less than the string length. It will get the substring before last three characters.

    myString = myString.Substring(0, myString.Length-3);
    

    String.Substring Method (Int32, Int32)

    Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.

    You can also using String.Remove(Int32) method to remove the last three characters by passing start index as length - 3, it will remove from this point to end of string.

    myString = myString.Remove(myString.Length-3)
    

    String.Remove Method (Int32)

    Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted

    0 讨论(0)
  • 2021-01-30 16:12

    You can use String.Remove to delete from a specified position to the end of the string.

    myString = myString.Remove(myString.Length - 3);
    
    0 讨论(0)
  • 2021-01-30 16:12

    str= str.Remove(str.Length - 3);

    0 讨论(0)
提交回复
热议问题