ATRIM

=ATRIM(a,d,c)

a
any array
d
string. array delimiter
c
clean argument: 0 or ignored, does not clean ; 1, cleans non printable chars, replaces non breaking space with space

array TRIMs by any delimiter, and/or CLEANs non pintables char, replaces CHAR(160) with space

Xlambda

Well-known Member
Joined
Mar 8, 2021
Messages
860
Office Version
  1. 365
Platform
  1. Windows
ATRIM array trim, does for any delimiter what TRIM does to space chars, plus can CLEAN any non printable chars, replaces non breaking spaces CHAR(160), with regular spaces.
Excel Formula:
=LAMBDA(a,d,c,
    LET(x,OR(c={0,1}),h,CHAR(1),
      ax,IF(c,SUBSTITUTE(CLEAN(a),CHAR(160)," "),a),
      ay,IF(OR(d=0,d=""),ax,SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(TRIM(ax)," ",CHAR(1)),d," "))," ",d),CHAR(1)," ")),
      IF(x,ay,"check values")
    )
)
LAMBDA 6.0.xlsx
ABCDEFGHIJKLMNOPQR
1
2CLEAN worksokCLEAN worksno CLEAN
3line feedc returnspacenon breaking space
4sample CODE values2910132829303132129141143144157160
5="ab"&CHAR(B4:P4)&"cd"abcdab cdab cdab cdabcdabcdabcdabcdab cdabcdabcdabcdabcdabcdab cd
6=LEN(B5#)555555555555555
7
8=ATRIM(B5#,,1)abcdabcdabcdabcdabcdabcdabcdabcdab cdabcdabcdabcdabcdabcdab cd
9=LEN(B8#)444444445444445
10
11sample=ATRIM(I12:J14,"-",)sample=ATRIM(N12:N14,"//",)
12="--sd"&CHAR(1)&"fr--"&CHAR(129)&"--4"&CHAR(160)&"5--fvb#45--"--r 67--t-----pb 18-hr 67-tpb 18-h//iut yt56//rtujh45////sdfg$%456iut yt56//rtujh45//sdfg$%456
13--sdfr----4 5--fvb#45--s 56---v-xy 78-ns 56-vxy 78-nrtyu//ghj //46ghh// yuyt //ghfd65rtyu//ghj //46ghh// yuyt //ghfd65
14-t 89-wgh 98--y--t 89-wgh 98-y//34%&/dfg/////jhgf////dfg34534%&/dfg///jhgf//dfg345
15=ATRIM(B13,"-",1)
16sdfr-4 5-fvb#45
17
ATRIM post
Cell Formulas
RangeFormula
A5:A6,A8:A9A5=FORMULATEXT(B5)
B5:P5B5="ab"&CHAR(B4:P4)&"cd"
B6:P6,B9:P9B6=LEN(B5#)
B8:P8B8=ATRIM(B5#,,1)
K11,B15,Q11,B12K11=FORMULATEXT(K12)
K12:L14K12=ATRIM(I12:J14,"-",)
Q12:Q14Q12=ATRIM(N12:N14,"//",)
B13B13="--sd"&CHAR(1)&"fr--"&CHAR(129)&"--4"&CHAR(160)&"5--fvb#45--"
B16B16=ATRIM(B13,"-",1)
Dynamic array formulas.
 
Upvote 0
Basically, same functionality, same arguments, "c", (clean argument) can be omitted now, added a some tweaks:
- added, apart of CHAR(160), CHAR(127), the DEL command that also can not be cleaned by CLEAN function
- like ATEXTJOIN,ATEXTSPLIT, new ATRIM is keeping the spaces structure, previous formula was trimming spaces.

Excel Formula:
=LAMBDA(a,d,[c],
    LET(h,CHAR(1),x,IF(c,SUBSTITUTE(SUBSTITUTE(CLEAN(a),CHAR(160)," "),CHAR(127),""),a),
       y,IF(d="",x,SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(x," ",h),d," "))," ",d),h," ")),
       IF(ISNUMBER(--y),--y,y)
    )
)
LAMBDA 1.1.2.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMAN
1codes of control/"special" chars between [1,255]
21234567891011121314151617181920212223242526272829303132127129141143144157160
3=CHAR(A2:AM2)
4 
5="/"&A4#&"\"
6/\/\/\/\/\/\/ \/\/ \/ \/ \/\/ \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ \/\/\/\/\/\/\/ \
7=LEN(A6#)
8333333333333333333333333333333333333333
9=ATRIM(A6#,,1)
10/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ \/\/\/\/\/\/\/ \
11=LEN(A10#)
12222222222222222222222222222222232222223
13As we see, checking with LEN functions, ATRIM "cleans" properly, space remains as space, like any other visible char, and non breaking space replaced with a space.
14
15common "special" chars
167bell or screen flashing
178backspace
189horizontal tab
1910line feed
2011vertical tab
2112form feed
2213carriage return
2326Ctrl-Z
2427escape
2532space (acts like any "visible" char)
26127DEL command, does not get CLEANed
27160non breaking space, does not get CLEANed
28
new ATRIM post 1
Cell Formulas
RangeFormula
A3,A11,A9,A7,A5A3=FORMULATEXT(A4)
A4:AM4A4=CHAR(A2:AM2)
A6:AM6A6="/"&A4#&"\"
A8:AM8,A12:AM12A8=LEN(A6#)
A10:AM10A10=ATRIM(A6#,,1)
Dynamic array formulas.
 
LAMBDA 1.1.2.xlsx
ABCDEFGHIJ
1sample=ATRIM(B2,",",1)=TRIM(ATRIM(B2,",",1))
2 ,, ,, ,b , c,,, ,,, , , , ,b , c, , , , , ,b , c, , ,
3
4=LEN(B2)=LEN(D2)=LEN(F2)
5262015
6spaces structure is keptextra spaces trimming only by choice
7
8Note: Cell B3 has leading space, trailing space, and some inner spaces also.
9Since we trim for "," delimiter, spaces structure is kept intact,only "," are trimmed
10Space trimming by choice, with a final TRIM
11
12array=ATRIM(B13:C14,",")
13,,a,,,b,c,,, , ,e,, f,,,A,,B,,C,,a,b,c, , ,e, fA,B,C
141,,2,,3,,4,,,,5,,6Ax2,, ,,By31,2,3,4,5,6Ax2, ,By3
15
16=ATRIM(C17:C18,",")=ISNUMBER(E17#)
17,,,-32.576,,-32.576TRUE
18,,,,1E+3,,,,1000TRUE
19
20Note: Also, like ATEXTSPLIT, function is consistent
21with numbers, whenever is the case
22
23dbl delimiter=ATRIM(C24,"//")
2406////09///a06//09///a
25only multiple of "//" are trimmed
26
new ATRIM post 2
Cell Formulas
RangeFormula
D1,F1,E23,E16,G16,E12,F4,D4,B4D1=FORMULATEXT(D2)
D2D2=ATRIM(B2,",",1)
F2F2=TRIM(ATRIM(B2,",",1))
B5,F5,D5B5=LEN(B2)
E13:F14E13=ATRIM(B13:C14,",")
E17:E18E17=ATRIM(C17:C18,",")
G17:G18G17=ISNUMBER(E17#)
E24E24=ATRIM(C24,"//")
Dynamic array formulas.
 

Forum statistics

Threads
1,224,829
Messages
6,181,219
Members
453,024
Latest member
Wingit77

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top