user3541631
user3541631

Reputation: 4008

Get a substring starting from a substring, and with limited length

I know how I can get a substring from a string using index, but I want a substring starting from a substring, and with limited length.

For example. I have the string:

7F4P_gmLYIQottr9LMsJ5_arena_word2_fdsfds_fdsfds_fgdgdfs

I want to get a substring starting from the word arena, but no more than 40 characters. The result should be:

arena_word2_fdsfds_fdsfd

The underscores, "_", are in a way random generated, so I can't just split after the second underscore.

Upvotes: 0

Views: 81

Answers (1)

Ugenteraan Manogaran
Ugenteraan Manogaran

Reputation: 121

word = "7F4P_gmLYIQottr9LMsJ5_arena_word2_fdsfds_fdsfds_fgdgdfs"

result = "arena" + word.split("arena")[-1] #append the word "arena" and the characters after the word "arena"

limit_result = result[:40] #limit the result to 40 characters

Final result : "arena_word2_fdsfds_fdsfds_fgdgdfs"

Upvotes: 2

Related Questions