Separate a string with multiple lines

Currently in my program after doing datascraping I am getting a string that is shown in the following way:

"FileName.pdf
September 25, 2121
49 KB
Share"

How can I split this string to just get the name of the pdf file (the first line of the string)? Taking into account that the file name can have any amount and type of symbols and characters.

Hi @WSay,

YOURSTRINGVARIABLE.Split(Environment.NewLine.ToArray, StringSplitOptions.RemoveEmptyEntries)(0)

* .Split method splits your variable at every NewLine and converts all the contents (both newline and strings) to an array and StringSplitOptions.RemoveEmptyEntries removes empty values in the array.

  • So, in the end your array only holds these items {"FileName.pdf", "September 25, 2121", "49 KB", "Share"}

  • (0) specifies fetch the first item ( index 0)

This will return : FileName.pdf

Hope this helps you understand how the split method works. Here is the link to more documentation : String.Split Method (System) | Microsoft Docs

2 Likes

Hi @WSay

Another way is use regex pattern apart from splliting

image

Regards,
Nived N

2 Likes

@WSay

If you have the fixed structure then an easy solution is what @jeevith proposed

Else

If this input changes every time then you can take the solution of @NIVED_NAMBIAR

Hope this may help you

Thanks

3 Likes

Thank you very much for the explanation, it made it work!

Thanks for the alternative!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.