0
$\begingroup$

I have a string list constructed as follows:

lis = {"Abcdef","Ghij Kl"," GHI 01", " ABC 02"}

and would like to rearrange this to give:

res = {"Abcdef","02","Ghij Kl","01"}

This involves replacing the all-caps portion of the last two elements with the matching string that begins with the same characters, then splitting the revised third and fourth element to give res. [The white spaces in lis are intentional.]

Thanks for any thoughts!

$\endgroup$
1
  • 1
    $\begingroup$ It seems like there have been several of these kinds of questions from you lately. Maybe you should give it a go yourself this time. You might find useful functions here: reference.wolfram.com/language/tutorial/… $\endgroup$
    – lericr
    Commented Sep 2, 2022 at 17:36

1 Answer 1

1
$\begingroup$
lis = {"Abcdef", "Ghij Kl", " GHI 01", " ABC 02"}

StringReplace[
 SortBy[lis, StringTake[StringTrim[#], 3] &]
 , " " ~~ (WordCharacter ..) ~~ " " ~~ k : (DigitCharacter ..) :> k]

{"Abcdef", "02", "Ghij Kl", "01"}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.