Skip to main content
13-Aquamarine
November 18, 2013
Solved

In ACL language, how to remove line breaks in a characters string

  • November 18, 2013
  • 4 replies
  • 2504 views

In ACL language, how to remove line breaks in a characters string?

I try this command:

gsub("\r\n", "", $x);

But it doesn't work.

Can you help me?

    Best answer by dgopois

    The PTC technical support has solved the problem, see below.

    The first parameter to the gsub() function is a regular expression string and that does not support C syntax like \r and \n for carriage return and line feed. Instead you would have to put that string in a variable and then use the variable as the parameter to the function.

    $s = "\r\n";
    gsub($s, ' ', $x);

    Or you could take if further with the following:
    $s = "one\r\ntwo"
    gsub("\r\n", "x", $s)

    If instead your string only has a \r or \n then the character class

    gsub("[\r\n]", "x", $s)

    would work. The first regular expression matches two characters whereas the second matches only one. The first result would have one 'x' but the second would have 'xx'.

    4 replies

    November 18, 2013

    I believe what you are looking for is chop($st,[int])

    This is like the Perl func and removes the number of chars specified by [int] from $st. The integer is optional, default is 1 character, which is very useful for dropping the newline chars.

    1-Visitor
    November 18, 2013

    Are you sure that you're getting a match? Set your gsub function to a variable and test the result. If the result is 0, then no matches were found in the string. Otherwise, it returns the number of matches that it found.

    18-Opal
    November 18, 2013

    You might have better luck if you just search for "\n".

    dgopois13-AquamarineAuthorAnswer
    13-Aquamarine
    December 2, 2013

    The PTC technical support has solved the problem, see below.

    The first parameter to the gsub() function is a regular expression string and that does not support C syntax like \r and \n for carriage return and line feed. Instead you would have to put that string in a variable and then use the variable as the parameter to the function.

    $s = "\r\n";
    gsub($s, ' ', $x);

    Or you could take if further with the following:
    $s = "one\r\ntwo"
    gsub("\r\n", "x", $s)

    If instead your string only has a \r or \n then the character class

    gsub("[\r\n]", "x", $s)

    would work. The first regular expression matches two characters whereas the second matches only one. The first result would have one 'x' but the second would have 'xx'.