Using split or regex

i have a value for ex “abc - xyz - jkl”
and my result should be “abc - xyz” in a string

@mark_01

Welcome to forums

Check as below

image

Hope this may help you

Thanks

i want a single line code.
Thanks

@mark_01

inputString is a type of String variable
parts is a type of String variable

Thanks,
Srini

@mark_01

Try this way then

String.Join(" - “, inputString.Split(”-"c).Take(2))

image

Thanks,
Srini

Thank you so much

1 Like

Try this-
Use the Split method to split the inputString based on the delimiter “-”. This will return an array of substrings.

Assign splitArray = inputString.Split("-"c)

Use the Substring method to extract the first two substrings from the split array, which correspond to “abc” and “xyz”. Concatenate them back together using the delimiter “-”.

Assign result = String.Join(“-”, splitArray.Take(2))

The resulting string will be “abc - xyz”.

Thanks!!

Hi @mark_01 ,

Here’s using regex:

System.Text.RegularExpressions.Regex.Match(StringVariable,"(\w+[\s-]+\w+)").Value.ToString

image

Regards,

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.