Is there any library API or regex pattern to split a String on some delimiter and automatically trim leading and trailing spaces from every element without having to loop the el
Just trim it before you split
" A B # C#D# E # ".trim().split("\\s*#\\s*")
The spaces after the commas in [ A B, C, D, E] are just the way Arrays.toString prints
[ A B, C, D, E]
Arrays.toString