-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIndicesAndRanges.cs
69 lines (53 loc) · 1.86 KB
/
IndicesAndRanges.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Linq;
using static System.Console;
namespace ConsoleApp
{
public static class IndicesAndRanges
{
const string TearsInRain =
"I've seen things you people wouldn't believe. " + // 7
"Attack ships on fire off the shoulder of Orion. " + // 9
"I watched C-beams glitter in the dark near the Tannhäuser Gate. " + // 11
"All those moments will be lost in time, like tears in rain. " + // 12
"Time to die."; // 3
public static void Demo () => Demo5 (); // of 5
static void Demo3 ()
{
string[] words =
{
"I've", "seen", "things", "you", "people", "wouldn't", "believe."
};
string[] range = words[..3]; // play here
WriteLine (string.Join (' ', range));
}
static void Demo2 ()
{
string[] words = TearsInRain.Split (' ');
for (int index = 0; index < words.Length; index++)
WriteLine (words[^index]);
}
static void Demo1 ()
{
string[] words = TearsInRain.Split (' ');
WriteLine (words[3]); // you
WriteLine (words[5]); // wouldn't
WriteLine (words[^1]); // die
}
static void Demo4 ()
{
Span<string> words = TearsInRain.Split(' ').AsSpan();
Span<string> lastWords = words[^4..];
foreach (var word in lastWords)
WriteLine (word);
}
static void Demo5 ()
{
Span<string> words = TearsInRain.Split(' ').AsSpan();
var lastWords = words[^3..];
lastWords[^1] = "live.";
foreach (var word in words)
WriteLine(word);
}
}
}