Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

c# substring from end

// C# 8 introduced indices and ranges which allow you to write
str[^2..] = str.Substring(str.Length - 2, 2)
str[..^2] = str.Substring(2, str.Length - 2)

// In fact, this is almost exactly what the compiler will generate, 
//  so there's no overhead.
// Note that you will get an ArgumentOutOfRangeException if 
// the range isn't within the string.
 
PREVIOUS NEXT
Tagged: #substring
ADD COMMENT
Topic
Name
6+5 =