String split by count from left or right, fail

In a for each row loop, I want to take the value of a cell, split the string into two parts, based on the length from the left, in my case 2.
When I try to input a number into the split function i get the error message that integer value cannot be converted to Char.

I’ve read all I can find on the topic on this forum but all I get is that it is surprisingly difficult to split a string.

Any ideas on how to solve the issue?
Best wishes
Rupert

1 Like

@robert.rudberg

Welcome to Forums

Can you share sample string and output required?

Thanks

Hey! Sure, lets say it is as follows:

string = 123456789
output1 = 12
output2 = 3456789

Thank you! :slight_smile:

@robert.rudberg You can use Substring method to get the Output you need :

string.Substring(0,2)
string.Substring(2)

Since 2 is the length that you want to Split with

@robert.rudberg Try this

   Assign  string1 = “123456789”
   Assign output1 = string1.Substring(0,2)
   Assign  output2 = string1.Substring(2,8)

Hello!

Thank you! :smiley:
Seems like I was trying to make it more complicated than it was! Have a lovely weekend!
Rupert

1 Like

Hi! Tried it, yet for “output2 = string1.Substring(2,8)” I get the following message:

Assign: Index and length must refer to a location within the string.
Parameter name: length

I’d like to create a variable called readLength so the script will look like this:

output2 = string1.Substring(2,variable)

Since the string should always be cut at 2, yet its length differs.
I’ll try and see if I can crack it!! :slight_smile:

@robert.rudberg I don’t think you need to provide the other Index Limit to the SubString method if you have to take the Splitted String From Length 2.

You can Just use this :
output2 = string1.Substring(2)

Also you might need to Check if the Input String is having Length more than 2, Otherwise It will throw an error

Hey!

Beautiful! Thank you! :slight_smile:

1 Like

Hey! It worked like magic a few times, now it doesnt work anymore, with this error for the assign activity(output1=string1.substring(0,2)):
“Assign: Object reference not set to an instance of an object.”
The assign task is marked with “No condition defined.”

Read one could assign default value: “” but that didnt make any difference. Also i found someone saying solution could be to change to “generic” variable yet i cant find it so no luck there.

Any ideas on what the matter might be?
Thanks!

@robert.rudberg The input String Might be Empty

cảm ơn bài viết của bạn