361
Views
10
Comments
How to find Total "\n" in a string
Question
Application Type
Reactive

Hi all

How to find Total Number of "\n" in  string 

"\n"= NewLine

string:  aaaaaaaaaaa\naaaaaaaaaaaaaaaaaa\nqqqqqqqqq\n;


Thanks


2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

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).

UserImage.jpg
Md Mansur

Outsystms have any Inbuilt Function like this:

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Yes of course, it's Reactive so if there's no function for it, you turn to JavaScript 😄.

UserImage.jpg
Md Mansur

@Kilian Hekhuis 

I have a string like this:

string: 

q

q

q


 Index(NoteDesc,"\n",StartIndex)=-1 (this logic is not working to find index of this String

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

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)).

UserImage.jpg
Md Mansur

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??

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

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:

  • "abc" + NewLine() + "def":
    • String_Split: three records, "abc", "", "def";
    • Index(): 3;
    • Regex_Search.FirstIndex: 4
  • "abc" + Chr(10) + "def":
    • String_Split: two records, "abc", "def";
    • Index(): -1 (not found);
    • Regex_Search.FirstIndex: 3
  • "abc" + Chr(13) + Chr(10) + "def":
    • String_Split: three records, "abc", "", "def";
    • Index(): 3
    • Regex_Search.FirstIndex: 4

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.

2018-06-01 14-37-59
Rebecca Hall

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.

UserImage.jpg
Md Mansur

How to apply (length -1)

strings Like this:

qqqqqqqqqqqqqqqq

qqqqqqqqqqqqqqqq

qqqqqqqqqqqqqqqqq

qqqqqqqqqqqqqqqqq



2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Otoh, depending on how large the text is, and how many newlines there are, performance could be suboptimal.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.