Substring-changes (#36213)
This commit is contained in:
committed by
Randell Dawson
parent
d7b5bf796b
commit
10a7823d68
@ -1,31 +1,46 @@
|
||||
---
|
||||
title: Substring
|
||||
title: Substring
|
||||
---
|
||||
|
||||
# Substring
|
||||
# String.Substring Method
|
||||
|
||||
`Substring` extracts a portion of a string value. It is used with 2 integer parameters, the first is location of the first character(starts with index 0) and the second is the desired character length. If only one parameter is used, it will be the location
|
||||
of the first character, and the rest of the string is returned from the starting position.
|
||||
A substring is a contiguous sequence of characters within a string. The `String.Substring` method retrieves those characters. In c# string characters are zero-indexed which means that the first character of a string starts at the 0th position.
|
||||
|
||||
## Overloads
|
||||
|
||||
* Substring(int32)
|
||||
* Substring(int32, int32)
|
||||
|
||||
The first overload retrieves a substring that starts at a specified character position and continues to the end of the string. The second overload retrieves a substring that starts at a specified character position and has a specified length.
|
||||
|
||||
## Example
|
||||
```
|
||||
|
||||
```csharp
|
||||
string firstSentence = "Apple, I have.";
|
||||
string secondSentence = "I have a Pen.";
|
||||
string thirdSentence = "I am having Fun";
|
||||
|
||||
string apple = firstSentence.Substring(0,5);
|
||||
string pen = secondSentence.Substring(9,3);
|
||||
string fun = thirdSentence.Substring(12);
|
||||
string apple1 = firstSentence.Substring(9);
|
||||
string pen1 = secondSentence.Substring(7);
|
||||
|
||||
Console.WriteLine(apple);
|
||||
Console.WriteLine(pen);
|
||||
Console.WriteLine(fun)
|
||||
string apple2 = firstSentence.Substring(0,5);
|
||||
string pen2 = secondSentence.Substring(9,3);
|
||||
|
||||
|
||||
Console.WriteLine(apple1);
|
||||
Console.WriteLine(pen1);
|
||||
Console.WriteLine(apple2);
|
||||
Console.WriteLine(pen2);
|
||||
Console.ReadLine();
|
||||
|
||||
```
|
||||
|
||||
## Output:
|
||||
|
||||
```
|
||||
>have.
|
||||
>a Pen.
|
||||
>Apple
|
||||
>Pen
|
||||
>Fun
|
||||
|
||||
```
|
||||
|
Reference in New Issue
Block a user