String to array


#1

hello

i have in my string my name
StringName: Name

output: Sara Vanesa Santos Cruz

but I want only the “Santos” and “Cruz”

I tried this

StringName = “Sara Vanesa Santos Cruz”

arrMyStrings (Cadena) = StringName.Split(Char.Parse(" "))

strApellidos= Cadena(0).Split(Char.Parse(" "))(0)

strSecondLastName= Cadena(0).Split(Char.Parse(" "))(1)

but I have this error

Index was out of the range

I need the output like this

strApellidos= “Santos”

strSecondLastName= “Cruz”

the name string to array


#2

This shows that you are splitting StringName by the space. Cadena is = to {“Sara”,“Venesa”,“Santos”,“Cruz”}

So, to extract just one of the words like “Santos”, all you need to do is use the index (or run it through a for each loop if you want)

strApellidos = Cadena(2)
strSecondLastName = Cadena(3)

The reason you are getting the index out of range is because you are trying to split Cadena(0) which has no spaces, because Cadena(0) = “Sara”

Like I said, StringName.Split(Char.Parse(" ")) is already splitting your string into the array.

Hope that answers it.

Regards.


#3

of course, thank you!!


#4

Hi ClaytonM/Rammohan91/prankurjoshi,

I want to get output as “Santos Cruz” but I am getting “arr[2]arr[3]”.
Please have a look. Thanks in advance. xaml file attached.Main.xaml (6.7 KB)

Regards


#5

Hi @Deepak11

You placed the arr(2) and arr(3) with quotes around it, which converts them to a string (which is why you get “arr[2]arr[3]”, because “arr[2]”+“arr[3]” = “arr[2]arr[3]”). What you want is arr(2)+arr(3) so it uses the variables; whenever you use a variable, it will not have quotes around it.

Additionally, UiPath uses vb dot net syntax, so array indexes go between parenthesis, not brackets. Like, arr(2) and arr(3)

See below image that shows your errors where you put the variables surrounded by quotes:
image

See below image that shows the needed correction:
image

Also if each variable is “Santos” and “Cruz”, you will need a space between them, like this:
arr(2)+" "+arr(3) or str1+" "+str2

Hope that helps.

Regards.