C
C#2y ago
Aksword

Get the first letters of a string

Hello, Let's say that I have the string "tyranitar". With string[X], I can get the letter that I want, but is there a method or a fast way to get the first X chars of a string ? For exemple if I want "tyrani", how can I ask to substract from [0] to [6] ? Thanks
12 Replies
Angius
Angius2y ago
"Substract from 0 to 6"...? You mean get a range of letters?
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
"hello world"[1..5]
"hello world"[1..5]
Result: string
ello
ello
Compile: 352.181ms | Execution: 45.632ms | React with ❌ to remove this embed.
MODiX
MODiX2y ago
Doombox#1389
REPL Result: Success
"tyranitar".Substring(0,6);
"tyranitar".Substring(0,6);
Compile: 405.693ms | Execution: 25.563ms | React with ❌ to remove this embed.
Doombox
Doombox2y ago
damnit, semicolon every time
Angius
Angius2y ago
Use the range operator, less typing But sure, .Substring() also works
Doombox
Doombox2y ago
true, substring is the OG way a time before C#8
Aksword
Aksword2y ago
Thanks !
Doombox
Doombox2y ago
was curious if either was faster, not really
Doombox
Doombox2y ago
different implementations but it's such a simple op that it ends up the same anyways
Aaron
Aaron2y ago
this benchmark is completely noise, the range operator literally calls Substring
Doombox
Doombox2y ago
makes sense
reflectronic
reflectronic2y ago
you can also tell that it's noise because the confidence intervals are overlapping (718 ± 12 overlaps 699 ± 17) which means that the difference is not statistically significant
Want results from more Discord servers?
Add your server
More Posts