mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-04-17 12:13:55 +00:00
Initial commit
This commit is contained in:
parent
c0d4d11b54
commit
0e89590d92
46 changed files with 1741 additions and 0 deletions
28
vscode/Chapter08/WorkingWithRanges/Program.cs
Normal file
28
vscode/Chapter08/WorkingWithRanges/Program.cs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
string name = "Samantha Jones";
|
||||
|
||||
// getting the lengths of the first and last names
|
||||
|
||||
int lengthOfFirst = name.IndexOf(' ');
|
||||
int lengthOfLast = name.Length - lengthOfFirst - 1;
|
||||
|
||||
// Using Substring
|
||||
|
||||
string firstName = name.Substring(
|
||||
startIndex: 0,
|
||||
length: lengthOfFirst);
|
||||
|
||||
string lastName = name.Substring(
|
||||
startIndex: name.Length - lengthOfLast,
|
||||
length: lengthOfLast);
|
||||
|
||||
WriteLine($"First name: {firstName}, Last name: {lastName}");
|
||||
|
||||
// Using spans
|
||||
|
||||
ReadOnlySpan<char> nameAsSpan = name.AsSpan();
|
||||
ReadOnlySpan<char> firstNameSpan = nameAsSpan[0..lengthOfFirst];
|
||||
ReadOnlySpan<char> lastNameSpan = nameAsSpan[^lengthOfLast..^0];
|
||||
|
||||
WriteLine("First name: {0}, Last name: {1}",
|
||||
arg0: firstNameSpan.ToString(),
|
||||
arg1: lastNameSpan.ToString());
|
||||
Loading…
Add table
Add a link
Reference in a new issue