Reputation: 9307
I can get the first three characters with the function below.
However, how can I get the output of the last five characters ("Three") with the Substring()
function? Or will another string function have to be used?
static void Main()
{
string input = "OneTwoThree";
// Get first three characters
string sub = input.Substring(0, 3);
Console.WriteLine("Substring: {0}", sub); // Output One.
}
Upvotes: 57
Views: 239160
Reputation: 111
In C#
8.0 and later you can use [^5..]
to get the last five characters combined with a ?
operator to avoid a potential ArgumentOutOfRangeException
.
string input1 = "0123456789";
string input2 = "0123";
Console.WriteLine(input1.Length >= 5 ? input1[^5..] : input1); //returns 56789
Console.WriteLine(input2.Length >= 5 ? input2[^5..] : input2); //returns 0123
index-from-end-operator and range-operator
Upvotes: 0
Reputation: 1428
Here is a quick extension method you can use that mimics PHP syntax. Include AssemblyName.Extensions
to the code file you are using the extension in.
Then you could call:
input.SubstringReverse(-5) and it will return "Three".
namespace AssemblyName.Extensions {
public static class StringExtensions
{
/// <summary>
/// Takes a negative integer - counts back from the end of the string.
/// </summary>
/// <param name="str"></param>
/// <param name="length"></param>
public static string SubstringReverse(this string str, int length)
{
if (length > 0)
{
throw new ArgumentOutOfRangeException("Length must be less than zero.");
}
if (str.Length < Math.Abs(length))
{
throw new ArgumentOutOfRangeException("Length cannot be greater than the length of the string.");
}
return str.Substring((str.Length + length), Math.Abs(length));
}
}
}
Upvotes: 2
Reputation: 1
// Get first three characters
string sub = input.Substring(0, 3);
Console.WriteLine("Substring: {0}", sub); // Output One.
string sub = input.Substring(6, 5);
Console.WriteLine("Substring: {0}", sub); //You'll get output: Three
Upvotes: -2
Reputation: 11
e.g.
string str = null;
string retString = null;
str = "This is substring test";
retString = str.Substring(8, 9);
This return "substring"
Upvotes: 0
Reputation: 147
static void Main()
{
string input = "OneTwoThree";
//Get last 5 characters
string sub = input.Substring(6);
Console.WriteLine("Substring: {0}", sub); // Output Three.
}
Substring(0, 3)
- Returns substring of first 3 chars. //One
Substring(3, 3)
- Returns substring of second 3 chars. //Two
Substring(6)
- Returns substring of all chars after first 6. //Three
Upvotes: 10
Reputation: 306
Substring. This method extracts strings. It requires the location of the substring (a start index, a length). It then returns a new string with the characters in that range.
See a small example :
string input = "OneTwoThree";
// Get first three characters.
string sub = input.Substring(0, 3);
Console.WriteLine("Substring: {0}", sub);
Output : Substring: One
Upvotes: 1
Reputation: 3
string input = "OneTwoThree";
(if input.length >5)
{
string str=input.substring(input.length-5,5);
}
Upvotes: 0
Reputation: 5420
simple way to do this in one line of code would be this
string sub = input.Substring(input.Length > 5 ? input.Length - 5 : 0);
and here some informations about Operator ? :
Upvotes: 0
Reputation: 838736
If your input string could be less than five characters long then you should be aware that string.Substring
will throw an ArgumentOutOfRangeException
if the startIndex
argument is negative.
To solve this potential problem you can use the following code:
string sub = input.Substring(Math.Max(0, input.Length - 5));
Or more explicitly:
public static string Right(string input, int length)
{
if (length >= input.Length)
{
return input;
}
else
{
return input.Substring(input.Length - length);
}
}
Upvotes: 78
Reputation: 1731
If you can use extension methods, this will do it in a safe way regardless of string length:
public static string Right(this string text, int maxLength)
{
if (string.IsNullOrEmpty(text) || maxLength <= 0)
{
return string.Empty;
}
if (maxLength < text.Length)
{
return text.Substring(text.Length - maxLength);
}
return text;
}
And to use it:
string sub = input.Right(5);
Upvotes: 10
Reputation: 16509
One way is to use the Length
property of the string as part of the input to Substring
:
string sub = input.Substring(input.Length - 5); // Retrieves the last 5 characters of input
Upvotes: 2