1

I have a string

let String = "     Test  "; 

I need an output as below:

Output = Test

I have tried using trim(), trimstart(), trimend()
Only one space has been removed using these scalar functions.

Markus Meyer
  • 3,327
  • 10
  • 22
  • 35

3 Answers3

1

Please try trim with Regex \s for whitespaces:

let String = " Test ";
print a = String, b = trim(@"\s", String)

enter image description here

Markus Meyer
  • 3,327
  • 10
  • 22
  • 35
  • The solution is fine, however **(1)** You are using a very convenient use-case, for which the OP naive solution (`trim(" ", String)') would also have worked. **(2)** Please avoid the use of images unless absolutely necessary. In this case you can use [unicode_codepoints_to_string()](https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/unicode-codepoints-to-string-function) to show the content of the original text vs. the altered text. – David דודו Markovitz Feb 28 '23 at 13:48
0

Found the work-around using replace_regex(source,lookup_regex, rewrite_pattern)

Pass the string value as source, lookup_regex as " " (nothing but blank spaces), rewrite_pattern as "" (replace with no space)

Example:

Input: let String = " Test "; //String length is 11

print(replace_regex(String," ", "")) Output is: Test (String length is 4)

--Happy Coding:)

  • Nope. That code removes *all* spaces in the text, including mid text. – David דודו Markovitz Feb 28 '23 at 13:42
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 06 '23 at 05:13
0

To replace more than one trailing and leading space character , We can use regular expression [trim(@"[ \t]+",<string_to_replace>)] in trim

let String = "   Te.  se   st.    ";

print original_string = String replacing_all_space_via_regex= trim(@"[ \t]+",String)

//Other possible methods to replace just the single occurrence of space

//print original_string = String, replacing_single_space_via_space_character = trim(@" ", String)

//print original_string = String replacing_single_space_via_regex = trim(@"[ \t]",String)