[Solved] Issue searching for newline character with StrFind

Hi,
I often use StrFind function to look for a string in a text, (this text I often load from a .txt file in folders).
It works fine, except sometimes, with the return (line break) character, that cannot be found (sometimes).

Is there anything specific to know about this character? Does gdevelop StrFind function works in a specific way for this character?

Thanks!

1 Like

Have you tried StrFind(VariableString(someString), NewLine())?

1 Like

Thanks, Iā€™ve never tried that, didnā€™t know that ā€œNewLine()ā€.
Iā€™ll do, and will give more information if I understand why is my issue happening.

Hi again, I run some tests to understand what doesnā€™t work well. The problem doesnā€™t seem to be in the newline character, but in a star ā€œ*ā€ followed by a newline ā€¦ ! I donā€™t unerstand why, here are the description and results of my tests :

Tests:

In the 3 tests, I look for the same string that is a star followed by a new line
What change in each test is in what string we are looking for.

Test 1 : weā€™re looking inside a string that is a variable where a text from a .txt file has been previously loaded.
Test 2 : we donā€™t use a variable, weā€™re looking inside a string directly, this string is a copy of the text in my .txt file
Test 3 : weā€™re looking inside a variable that has been set to a copy of the text in my .txt file

Result of the tests :

I run 3 preview : one looking for the star alone ("*"), one looking for the newline alone, and then one looking for a star and a newline.

  • if I look for a star only ā€œ*ā€, or if I look for a new line only , it is found in the 3 tests.

  • if I look for star and new line together, itā€™s not found in the first test.

Note : I opened debugger to look at the string loaded in test 1. I converted this string to binary, to compare with my .txt file. But it seems to be the same.

So, the problem seems to be :
looking for a star followed by a newline doesnā€™t work when the main text has been loaded from a text file.

I just share the result of my test, and if I did no mistake, it might be consider as a bugā€¦ ?
Itā€™s kind of a specific issue though, a few people only might encounter this problem.

I just run a new test, replacing the star by another character, ā€œlā€. The issue is the same.
So it is not about the star character.

As far as I know, the problem can be generalised as:

  • " Looking for a character followed by a new line doesnā€™t work when the main text weā€™re looking in has been loaded from a .txt file. "

So the issue is larger and can be encounter by more people, since you might want to look for things in a .txt file in paragraphs separated by new lines ā€¦

I just replaced in test 1 "*
" by : ā€œ*ā€+NewLine().
The result is the same, it cannot be found.

I donā€™t know what youā€™re doing, but hereā€™s my test, looking for ā€œ*ā€+NewLine(), and it works. My variable setup :


The value of str_pos in the debugger inspector :

image


Have you checked the characters in your file (a hex editor would be ideal)? It could be the line is terminated with the old CR + NL (Carriage Return and New Line), so you may need to search for CR (ASCII 13) instead.


Alternatively, regular expressions (regex) may be of some use.

Thanks for your reply :slight_smile:

What you did is my ā€œtest 3ā€. It works fine on my side too. What doesnā€™t work is what I did in test 1, which is, load a text from a file into a scene variable.

I can try that yes, but Iā€™ve never used a hex editor before. What I did though is to compare the binary translation of the string I look for, and the actual string Iā€™m looking in, as shown by the debugger, and the binary octets are the same.
So I do look for a string (character + NewLine) that should be here since the debugger shows it in the variable, but that is not found for a mysterious reasonā€¦

Thanks for the regex advice! Iā€™ll give a try.
Nevertheless my tests make me think the problem appears only when loading from a txt fileā€¦ So the trick might be in the loading event and not in the StrFind functionā€¦

This is the variable holding the text that was read in? Then maybe someone from the GDdvelop developers might be able to shine some light on tā€¦


One other option may be to use a javascript event to process the input string. Not sure how or what, but that may be able to handle the input a bit better.

Yes it is. I just checked again.
The picture here shows the comparaison I made :

  • on left side, the condition, lookin for string ā€œ* newlineā€
  • on right side the actual text variable where Iā€™m looking in
    The binary translation of the text looked for and the binary translation of the variable where the search is run, shows that the strings are the same. However the StrFind doesnā€™t find the string.

( I donā€™t know if to translate in binary is a good practice, but I did that copying the strings in an online translator, in order to compare what the characters actually are)

So I guess yesā€¦ I checked a few times and it seems that thereā€™s a strange thing here.

Yes

Are you trying to search for wildcards or the actual asterisk character? Because wildcards arenā€™t supported in any way within expressions or variables. They are just treated as asterisks.

(Edit: Wanting to confirm this first before I do testing tomorrow)

No Iā€™m not looking for wildcards. The asterisk ā€œ*ā€ was an example. But I tried with the letter ā€œlā€ and the result is the same.
When I load into a variable a txt file that contains only a character followed by a new line, the variable can be seen in the debugger, well loaded.
But looking for this variable string value, in this variable string value return - 1 (strange!?!)

Okay, Iā€™ll test tomorrow. 99% sure you cannot use return/new lines in expressions without the format MrMen showed, and Iā€™m using some loaded texts in my test projects that search for special characters including newlines.

Iā€™ll see if I can reproduce it and report back, if so weā€™ll get an issue on the GitHub

I tried a few times, trying to isolate the issue with testsā€¦ I can do some more, I donā€™t want to make you loose time with this, but I donā€™t know what to test moreā€¦

I did one more test, this time using NewLine() format instad of the newline character. It does the same.

My .txt file is nothing more that the letter m and a new line
I load it into a variable ā€œtextā€

  • if I look in this variable text for the letter m alone, it works, my condition compares 2 numbers : StrFind(VariableString(text), ā€œmā€) =/ - 1 is true
  • if I look for NewLine() alone, the condition StrFind(VariableString(text), NewLine()) =/ - 1 is true too
  • the problem occures when I look for the letter m followed by a new line. The condition StrFind(VariableString(text), ā€œmā€+NewLine()) =/ - 1 is false ā€¦

( I wrote " =/ " for " not equal to " )

One more test I just did :

  • The problem is specific to any string where a character is followed by a newline.

NewLine() +ā€œmā€ works fine
ā€œmā€ + NewLine() doesnā€™t
ā€œmā€ + NewLine() + ā€œsomethingā€ doesnā€™t either

Woke up a way too early and did some testing. I can reproduce this a few different ways, so Iā€™ve opened up a Github issue with a test project:

As with any issue, there isnā€™t a timeline on when/if itā€™ll be fixed, but hopefully the sample project can assist narrow it down for the devs or contributors that look at it.

1 Like

Just had time to look at it. I read your github post, thatā€™s exactly what the issue is about.
Thanks a lot!

Hi,
I found an explanation for this issue (See New Lines/NewLine() Expression are treated differently if stored in variables loaded from external text files. Ā· Issue #4246 Ā· 4ian/GDevelop Ā· GitHub).

I think the best is to add a new expression that would be NewLineForWindows or something similar.
Or I could create an expression that cleans a string from the \r characters.

Do you have an opinion on this?

I think something that cleans \r (or replaces it with newlines) may be the most flexible option? Especially since someone could use it on a string that doesnā€™t contain \r (if theyā€™re unsure) and it wouldnā€™t cause any issues, correct?

1 Like