String_split with NewLine() returning extra Records as Output.

String_split with NewLine() returning extra Records as Output.

  

Hi Team,

I have a text file with 4 lines, when I split it with String_split using NewLine() delimiter , I am getting one extra records in between two records

Please help me out for this?


Regrads 

Dileep

Hi Deep,

Can you please send me the the string in notepad that you are trying to split. I have used this function many times and get expected result every time.


Regards

-PJ-



Please send me the same file that you are using.


Regards

-PJ-

Hi PJ, please find the file below

Hi Dileep,

Please try this file now (same file you have given i just removed the new line separator from last line)..seems like your last row is having a new line . 


Regards

-PJ-


Hi PJ, Nothing help I am getting the 8 Records ...


Pramod Jain wrote

Hi Dileep,

Please try this file now (same file you have given i just removed the new line separator from last line)..seems like your last row is having a new line . 


Regards

-PJ-



Hi Dileep,

What i saw in your last file that the last row is ending with a newline character and thus when you were splitting it it is giving you one additional blank line.

Can you please try one more thing try to read  File_ReadTextLines  action from FileSystem extension , it will return you all lines and you don't need to split it than.


Regards

-PJ-

Hello Dileep,

It seems that you are getting empty items, right? This usually happens when there are multiple delimiters together, like multiple new lines.

As the String_Split available lacks the option to remove empty items from the result list, when using it (usually in a ForEach), it's a best practice to check if the item is empty against "" and in this case, skip it.

Cheers,

Eduardo Jauch

@Edyuardo,

Yes if you open the file in notepad which Dileep shared you will see that last data line is ending with a new line character and thus it is returning the blank line while splitting.

This can we ignored by putting a check for empty line as you suggested .


Regards

-PJ-

Solution

Hi Dileep,

On Windows, text file lines are delimited by both a Carriage Return (code 13) and a New Line (code 10), as opposed to UNIX files, which only have a New Line (code 10). I'll spare you the historical reason behind this (it has someting to do with line printers). I think that NewLine() matches both characters, meaning you get one empty line. What you could do is either run a Replace() on the original text replacing Chr(13) with the empty string (""), then use String_Split, or use a ListFilter to filter out the empty lines.

That said, if you are dealing with CSV files, I'd suggest you take a look at the CSVUtil Forge component, which can make your life a lot easier.

Solution

Hi Dileep,

As Pramod said, use Filesystem component in Forge with at least two methods:

where:

and server action like this:

where:

and 


regards,

A

Though the above is a better solution than reading the file into memory and then use String_Split(), I would still recommend looking at CSVUtil, if your goal is reading CSV files.