Split a string into its individual characters

Split a string into its individual characters

There is often a need to split a string into the individual characters that make up the string. One such instance is in computing checksums - including the (relatively) straightforward check-digit used to validate routing numbers in the U.S. banking system.

MS Excel

So, how do we split a string in Excel into its individual components? My instinctive reaction was to use the TEXTSPLIT function with the empty string as the delimiter. It failed. Apparently, we need to specify a non-empty delimiter.

The next choice was the good ole MID function. Combine it with the SEQUENCE function to get the result in a single row or column.

If the string is in cell B3, we get the individual characters in individual cells in a single row with

=MID(B3,SEQUENCE(,LEN(B3)),1)        

By contrast, to get the individual characters in a single column, use

=MID(B3,SEQUENCE(LEN(B3)),1)        

The next logical step is to create a lambda function that accepts a parameter for how it returns the characters - by row or by column.

=LAMBDA(_str,_byRow,
? ? LET(_byRowVal,IF(ISOMITTED(_byRow),FALSE,_byRow),
? ? ? ? MID(_str,
? ? ? ? ? ? SEQUENCE(IF(_byRowVal,LEN(_str),1),IF(_byRowVal,1,LEN(_str))),1)
? ? ))        
No alt text provided for this image

Python

Three ways to split a string into its individual characters. As a building block for additional analysis, the last one is arguably the most useful

str='111000025' 
print([*str])
print(list(str))
print([x for x in str])        

要查看或添加评论,请登录

Tushar Mehta的更多文章

  • MS Excel: How to develop a recursive function

    MS Excel: How to develop a recursive function

    Introduction A recursive function returns a value that depends on the value that the function returns for another set…

    2 条评论
  • Phone Number Region Lookup

    Phone Number Region Lookup

    Introduction We call people using phone numbers without giving much thought to what the number represents. Each number…

    1 条评论
  • MS Excel - Graph a Spill Range

    MS Excel - Graph a Spill Range

    Introduction to Dynamic Array Formulas and Spill Ranges Microsoft implemented a major improvement in Excel with the…

  • List and count unique values (MS Excel, Python, and SQL)

    List and count unique values (MS Excel, Python, and SQL)

    This is a fairly common requirement that until recently had a convoluted solution in Excel. Given a list of values, how…

    1 条评论
  • Excel Large Number Arithmetic with Python

    Excel Large Number Arithmetic with Python

    Introduction When it comes to numbers, MS Excel follows the IEEE 754 standard (Floating-point arithmetic may give…

  • MS Excel: Recursive functions

    MS Excel: Recursive functions

    In MS Excel: Functions are first-class objects, we saw how a custom Excel function, using LAMBDA, can call another…

    3 条评论
  • MS Excel: Functions are first-class objects

    MS Excel: Functions are first-class objects

    What makes a function a first-class object? This is almost certainly an advanced discussion. And, we will start with a…

  • Generate unique random integers

    Generate unique random integers

    Generating unique random integers is an important task in various domains. Examples include data sampling, simulations,…

    2 条评论
  • MS Excel: Goal Seek or Algebra

    MS Excel: Goal Seek or Algebra

    Let's start with a question. I want to pay a business through a payment processor such as PayPal.

    1 条评论
  • Calendar for the New Year

    Calendar for the New Year

    For the New Year..

    1 条评论

社区洞察

其他会员也浏览了