I need to find string in the text and truncate text untill this string

Operating system used: windows
Operating system used: windows
Best Answers
-
Here's a Python function that truncates a string until a certain key and returns only the part to the right of the key:
def truncate_string(input_string, key):
# Find the position of the key in the input string
key_position = input_string.find(key)
# If the key is found, return the substring starting from the right of the key
if key_position != -1:
return input_string[key_position + len(key):]
else:
return input_string
# Example usage
input_string = "someone@gmail.com"
key = "@"
result = truncate_string(input_string, key)
print(result) # Output: gmail.comThis function searches for the key in the input string and returns the substring that follows the key. If the key is not found, it returns the original string. In the example provided, it correctly returns gmail.com
-
Ashley Dataiker, Alpha Tester, Dataiku DSS Core Designer, Registered, Product Ideas Manager Posts: 165 Dataiker
Hi @Elzbieta,
I'll build on what was suggested with the DSS formula language, my favorite!
Let's assume you're trying to truncate everything that comes after the @ in an email address like someone@gmail.com stored in an email column(pro-tip: if you're working IRL with emails, use the "Split e-mail address processor" to split your email into local part and domain). You can use a combo of the get(), indexOf(), and length() functions to get what you need:
get(email, indexOf(email, "@"), length("email").
With this, you'll end up with just "@gmail.com".Best,
Ashley
-
Thank you. This is what I was looking for.
Answers
-
Turribeach Dataiku DSS Core Designer, Neuron, Dataiku DSS Adv Designer, Registered, Neuron 2023 Posts: 2,248 Neuron