XQuery: removing newline/return character from end of a string stored in a variable in query

StackOverflow https://stackoverflow.com/questions/12485969

  •  02-07-2021
  •  | 
  •  

Domanda

How do I remove a new-line/return from the end of a variable? I tried functx:substring-before-last to try and remove the new line (denoting it as \r, \n and also as \r\n) but still when I output that variable's value, the newline is still there in it.

È stato utile?

Soluzione

You can either use fn:normalize-space($arg as xs:string?) to remove all additional whitespace:

Summary: Returns the value of $arg with whitespace normalized by stripping leading and trailing whitespace and replacing sequences of one or more than one whitespace character with a single space, #x20.

Or you can just use fn:replace(..) and a regular expression:

fn:replace($string, '(\r?\n|\r)$', '')

Altri suggerimenti

CR and NL can be specified as 
 and 
. The following regular expression will remove all trailing newlines from your input string:

replace($string, '(
?
)*$', '')

If you want to normalize all whitespaces of a string, normalize-string is another, more readable option:

normalize-space($string)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top