Qlik Community

QlikView Expressor

Discussion Board for collaboration related to QlikView Expressor.

Not applicable

Remove newline in string

Hi,

I would like to replace all newline characters by spaces in a string.

I have tried :

string.replace(input.my_string,'\r',' ')

or

string.replace(input.my_string,'\n',' ')

But it doesn't seem to work.

Am I missing something?

Thanks

Lionel

1 Solution

Accepted Solutions
Not applicable

Re: Remove newline in string

You have the correct approach, but you used the wrong pattern.  The return/new line combination should be specified as control characters not as their string representations.  Also, the actual entry may be a combination of return and new line, not just one of these control characters.  The correct function call would be

string.replace(input.my_string, "%c+", "").

The pattern "%c+" specifies 1 or more control characters.

There is an easy way to test your coding.

  • Open an Expressor command window.
  • Enter the command datascript to start the interactive command line interpreter.
  • Use the square braces to demarcate your string; this allows you to define a string value that includes new lines.  That is:
    my_string =
    [[hello
    there]]
  • Then try out your code.
    my_string2 = string.replace(my_string,"%c+"," ")
  • And print out the result
    print(my_string2)
5 Replies
sreenivas
Contributor III

Re: Remove newline in string

Add sample data

Not applicable

Re: Remove newline in string

I get the data from a SQL Server database.

I have a carriage return / new line in the data, and a way to eliminate them is to do it in the SQL query :

REPLACE(REPLACE(E_LIBELLE,CHAR(10),''),CHAR(13),''))

I was looking for a way to do it in datascript.

Lionel

Not applicable

Re: Remove newline in string

You have the correct approach, but you used the wrong pattern.  The return/new line combination should be specified as control characters not as their string representations.  Also, the actual entry may be a combination of return and new line, not just one of these control characters.  The correct function call would be

string.replace(input.my_string, "%c+", "").

The pattern "%c+" specifies 1 or more control characters.

There is an easy way to test your coding.

  • Open an Expressor command window.
  • Enter the command datascript to start the interactive command line interpreter.
  • Use the square braces to demarcate your string; this allows you to define a string value that includes new lines.  That is:
    my_string =
    [[hello
    there]]
  • Then try out your code.
    my_string2 = string.replace(my_string,"%c+"," ")
  • And print out the result
    print(my_string2)
admin1
New Contributor III

Re: Remove newline in string

Hi,

I have similar problem.

I would like to insert already in script some function that removes "enters".

Unfortunately something described here  - string.replace(input.my_string, "%c+", "") - doesn't work.

Could anybody help me?

Joanna

rospigliosi
Contributor

Re: Remove newline in string

Hi try this one

Replace(replace(my_string,chr(10),''),chr(13),'')

Community Browser