简体   繁体   中英

Split string using Excel

I have a string with the following format:

StockCode Country Date Price equity

for example:

  • 1 hk 10/31/12 C70.5 equity
  • 101 hk 11/21/13 P63 equity
  • 388 hk 10/17/12 P100 equity

I can extract Date by this Excel Command:

LEFT(RIGHT(RIGHT(LEFT(A1,LEN(A1)-7),LEN(LEFT(A1,LEN(A1)-7))-FIND(" ",A1)),
LEN(RIGHT(LEFT(A1,LEN(A1)-7),LEN(LEFT(A1,LEN(A1)-7))-FIND(" ",A1)))-FIND(" ",
RIGHT(LEFT(A1,LEN(A1)-7),LEN(LEFT(A1,LEN(A1)-7))-FIND(" ",A1)))),FIND(" ",
RIGHT(RIGHT(LEFT(A1,LEN(A1)-7),LEN(LEFT(A1,LEN(A1)-7))-FIND(" ",A1)),
LEN(RIGHT(LEFT(A1,LEN(A1)-7),LEN(LEFT(A1,LEN(A1)-7))-FIND(" ",A1)))-FIND(" ",
RIGHT(LEFT(A1,LEN(A1)-7),LEN(LEFT(A1,LEN(A1)-7))-FIND(" ",A1))))))

(line breaks for readbility)

result: 10/31/12

Anyone has a better solution for this?

here are some of my codes:

  • how to get "C" and "P":

     TRIM(MID(A1,IFERROR(FIND("P",A1),FIND("C",A1)),1)) 
  • how to get the strike price:

     TRIM(SUBSTITUTE(RIGHT(A1,LEN(A1)-IFERROR(FIND("P",A1), FIND("C",A1))),"equity","")) 

试试这个,我认为它适用于所有2个字符年份的日期:

=TRIM(MID(A1,FIND("/",A1)-2,8))

I'd recommend running Text to Columns from the Data tab. Choose Delimited, and indicate space as the delimiter. This will put your string into five columns with the third one being date, and then you can freely use the data directly.

Otherwise, I'd look at Lajos Example:( =TRIM(MID(A1,FIND("!^!",SUBSTITUTE(A1," ","!^!",2)),8)) ) or John's answer.

If you can use a macro UDF this is simple.

Function STR_SPLIT(str as String, sep as String, n as Long)
    Dim Arr() as String
    Arr = Split(str, sep)
    STR_SPLIT = Arr(n - 1)
End Function

str is the string to split, sep is the character to split on - " " in your case - and n is the column number you're looking for. So =STR_SPLIT(A1," ",3) would return the date for example.

您可以通过转到“数据”>“文本到列”并将空格用作分隔符,将每个部分拆分为Excel中的列。

Your date is between the second and the third space in all your examples. You can replace your third space with "!^!" for example and then get the substring until the first occurrence of "!^!". After that you just have to extract the substring after the second space. Kindly read more here .

即使年份有4个字符,此公式也应该提供所需的结果

=TRIM(MID(SUBSTITUTE(A1," ",REPT(" ",99)),198,99))

Thanks John

here are some of my codes:

how to get "C" and "P":

TRIM(MID(A1,IFERROR(FIND("P",A1),FIND("C",A1)),1))

how to get the strike price:

TRIM(SUBSTITUTE(RIGHT(A1,LEN(A1)-IFERROR(FIND("P",A1),FIND("C",A1))),"equity",""))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM