Remove white spaces from a KQL string: Kusto

4.1k Views Asked by At

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.

4

There are 4 best solutions below

0
On

Remove all spaces from a string:

let String = "   s idan   mor  ";
print replace_string(String, " ", ""); // output will be "sidanmor"

enter image description here

0
On

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)

2
On

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

1
On

Please try trim with Regex \s for whitespaces:

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

enter image description here