Hi all
How to find Total Number of "\n" in string
"\n"= NewLine
string: aaaaaaaaaaa\naaaaaaaaaaaaaaaaaa\nqqqqqqqqq\n;
Thanks
There's nothing out of the box that can do that. I think the easiest is to use the built-in Index() function, which allows you to find the position of a certain substring in a string, also allowing for an offset. So you create a loop that's finished when Index() returns -1 (not found), have a local variable that counts the number of occurences, and a local variable that keeps track of the current position (because if found, the starting point is that index + 1 for the next iteration).
Outsystms have any Inbuilt Function like this:
Yes of course, it's Reactive so if there's no function for it, you turn to JavaScript 😄.
@Kilian Hekhuis
I have a string like this:
string:
q
Index(NoteDesc,"\n",StartIndex)=-1 (this logic is not working to find index of this String
That's because in OutSystems, "\n" is not a newline. OutSystems doesn't know escape sequences like this. "\n" is just that, a two-character string with a backslash and an "n". You should use the built-in NewLine() function, and see if that works. If not, you could try Chr(13) or Chr(10) (or Chr(13)+Chr(10)).
Pattern always return false value
1. new line
2."\n"
3.chr(10)
4.chr(13)
5"(\n|\r|\r\n)"
can you please give me a relevant idea to find new line in regex search pattern??
Hi MD,
I did a quick test for the following three strings:
"abc" + NewLine() + "def" "abc" + Chr(10) + "def" "abc" + Chr(13) + Chr(10) + "def"
For all three I did a String_Split, Index() and Regex_Search, the former two with NewLine(), the latter with "\n". I found this:
So concluding, it seems that NewLine() and Chr(13) + Chr(10) yield the same results: String_Split seems to match both Chr(13) and Chr(10), resulting in the extra empty string, Index() wants to match both Chr(13) and Chr(10), hence the -1 (not found) in the second case, and Regex_Search matches only Chr(10) (as expected). When I change the search to "(\r\n|\n|\r)", the result is 3 in all cases.
There is an extension called "Text" that has an action called "String_Split" .. You split on "\n" character using the Char(#) action. Then the length-1 will tell you how many you have. It's very fast this way and code is simplified without having the parse the text.
How to apply (length -1)
strings Like this:
qqqqqqqqqqqqqqqq
qqqqqqqqqqqqqqqqq
Otoh, depending on how large the text is, and how many newlines there are, performance could be suboptimal.