In python there are ways that allow you to access the string using an interval(slicing):
Example Get the characters from position 2 to position 5 (not included):
JavaScript
x
3
1
b = "Hello, World!"
2
print(b[2:5])
3
Is there something similar in c#?
Advertisement
Answer
In C# 8.0 (or higher) you can use ranges, e.g
JavaScript
1
4
1
var b = "Hello, World!";
2
3
var result = b[2..5];
4
Or on old c# versions, Substring
:
JavaScript
1
5
1
var b = "Hello, World!";
2
3
// start from 2nd char (0-based), take 3 characters
4
var result = b.Substring(2, 3);
5
Finally, you can use Linq, but it’s overshoot here:
JavaScript
1
7
1
var b = "Hello, World!";
2
3
// Skip first 2 characters, take 3 chars and concat back to the string
4
var result = string.Concat(b
5
.Skip(2)
6
.Take(3));
7