Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Trimming a string short syntax

Tags:

c#

I am reading some code and I am struggling with the short syntax of trimming a short string readStudentData = line.Split(':')[1].Trim().Split(' '). (readStudentData is a string array). Can I get a little bit of explaination(the "[1]" part is the one that loses me)

like image 457
user7431575 Avatar asked Jun 24 '26 14:06

user7431575


1 Answers

It splits the string on the :. This returns an array. The [1] is an array indexing operation which returns the second item in that array. It then trims that item, and splits that on a space.

Let's consider a basic example. Say you have the string line = "title:hi bob "

line.Split(':')                       --> ["title", "hi bob "]
               [1]                    --> "hi bob "
                  .Trim()             --> "hi bob"
                         .Split(' ')  --> ["hi", "bob"]

which gets assigned to the readStudentData variable.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!