Split first occurrence of hyphen

But -in order to make your website yours, you- will need to replace them

I need in result But in (0) index and in order to make your website yours, you- will need to replace them in (1) index.

Please help me out.

1 Like

Hi @Aleem_Khan

Can you be more brief?

Regards

This will help you to get the array you want, assign below value to an array variable

{strYourString.Substring(0, strYourString.IndexOf("-")), strYourString.Substring(strYourString.IndexOf("-") + 1)}

Hi,

Another solution:

img20211016-6

m= System.Text.RegularExpressions.Regex.Match(yourString,"(^[^-]*)-(.*)")

then

m.Groups(1).Value
m.Groups(2).Value

note: m is System.Text.RegularExpressions.Match class.

Sequence2.xaml (5.8 KB)

Regards,

1 Like

I need to split “but” in one index and rest of the string in one index “-” is repeat t
Multiple time here

input=“But -in order to make your website yours, you- will need to replace them”
expected result is =“in order to make your website yours, you- will need to replace them”

Did you try the above code? that gives you the array you requested. First element as But and rest of the string in second element

regarding this;

you can get this by
strTemp.Substring(strTemp.IndexOf(“-”) + 1)

Always appreciated if you give a close look to the solution provided. :slightly_smiling_face:

Even @Yoichi has given the absolutely correct solution to your original question with a different approach.

2 Likes

Hi

Let’s go step by step

If the below string input is saved in a string variable named Strinput

We can split the sentence with first occurrence of - like this

  1. Get the index of first - in the sentence
    Using a ASSIGN activity

int_firstindex = Strinput.IndexOf(“-“)

Where int_firstindex is a variable of type int32

  1. Now let’s split the sentence with that first index

Using ASSIGN activity

str_firstpart = Strinput.SubString(0, int_firstindex)

This Substring expression will try to read from 0 index that is from first character and till first - and split that

For second part

str_secondpart = Strinput.SubString(int_firstindex + 1)

On the second part, the +1 is to avoid including the hyphen.

For more details and ideas on this method
Refer this link

Cheers @Aleem_Khan

Came here looking for the same and came across a super simple activity created for this by UiPath so I figured i’d share in case anyone else is looking for a solution. Activity is called ‘Text to Left/Right’
image

Hi @rahulsharma thanks for the logic. It worked.
My string is A_12345_XYZ.
I wanted to get 12345_XYZ and it worked

1 Like