I have a list of strings, some of which are all upper case, and some mixed upper and lower, and some are digits with commas:
lis = {"ABC","Abc","Def","1","DEF","Ghi","Jkl","MNO","1,"}
I would like to StringJoin adjacent elements that consist of mixed upper and lower cases to give:
res = {"ABC", "AbcDef","1","DEF","GhiJkl","MNO","1,"}
I can identify the elements of lis that contain lower case letters easily enough:
StringContainsQ[tes,CharacterRange["a","z"]
but I don't know how to make a rule to StringJoin adjacent elements that return True. Thanks for suggestions.
Answer
You can useSequenceReplace
:
SequenceReplace[lis, {a__} /;
And @@ (StringContainsQ[{a}, Alternatives @@ CharacterRange["a", "z"]]) :>
StringJoin[a]]
% == res
True
Faster alternatives:
SequenceReplace[lis, {a__}/; Nor @@ StringFreeQ[_?LowerCaseQ] @ {a}:> StringJoin[a]]
and
StringJoin /@ Split[lis, Nor @@ StringFreeQ[_?LowerCaseQ] @ {##}&]
Comments
Post a Comment