How to cut characters according to a rule

I want to trim a cell’s input to 25 characters, and if there’s a space at the 25th position, cut it off. If there isn’t a space, find the nearest space, move frontward, and cut it off. The remaining characters should then move to the next row, and the process repeats. If the last row has characters that are less than or equal to 25, the robot should end.
I’ve tried using length, trim, and replace, but it didn’t work. Is there a way to achieve this?

The question is insufficient, so I explain it in an image.
Thank you in advance for your response!

@Nguyen_Van_Luong1

1 Like

HI @22222222asas ,
I will try it, I think switch case can use
regard

1 Like

@22222222asas
have a look here as it can be adapted

1 Like

@ppr
I appreciate your response. It seems to be a solution to a similar problem. However, since I am at the stage of learning UiPath, I don’t quite understand it well. Could you provide more details about the flow you have in mind regarding my question?

Please find the solution attached. I have taken the example on the screenshot and wrote it from A3 incrementing the cell count. You can do string manipulation according to your requirement and modify the solution.

@22222222asas

TEST.zip (9.3 KB)

1 Like

@Gayathri_Ramanathan

Write Cell: The length of the string cannot be zero.
Parameter Name: oldValue

image

I appreciate your response. HOWEVER I encountered an error while applying the code. The text I’m working with is ’ SKSKSKS Business Solution Corp. 2500 OKAKSA MijimashiraRRRRRR city, hajagawAAa USA 2500 Tel: Fax:', and I’m wondering if you could provide a solution for this issue.

Hi @22222222asas

Please find the solution attached for the string provided. However, the words are abruptly cut since you mentioned 25 characters in the cell.

Since it’s string manipulation, you need to tweak the code to work for all the scenarios and texts. The solution provided is just a starting template.

TEST.zip (16.6 KB)

Hi @22222222asas

initially go to the imports panel and import the below

 System.Text.RegularExpressions

Then
Assign
inputString=“Bussiness Corp.3000 UinateaMinamia city, KanaUSA 3000Phone:Fax:”

Message box

String.Join(Environment.NewLine, System.Text.RegularExpressions.Regex.Matches(inputString, ".{1,25}(\s|$)").Cast(Of Match)().Select(Function(m) m.Value.Trim()))

The expected output shown below

Hope it helps!!

2 Likes

We refer to:

and use the space as a split marker

we just adapted the linked code by
Variables:

Flow:

strText = your Text

arrSplits = System.Text.RegularExpressions.Regex.Split(strText, strPattern)
Condition: (tmpSplitAggregator & item.ToString).Length <= maxLength
Then: tmpSplitAggregator = tmpSplitAggregator & " " & item
Else: 
SplitLines = SplitLines.append(tmpSplitAggregator).toList
tmpSplitAggregator = item

Last Appending:
SplitLines = SplitLines.append(tmpSplitAggregator).toList

So we can check within the immediate panel
Understanding the 6 Debugging Panels of UiPath in the easiest way possible! - News / Tutorials - UiPath Community Forum

grafik

And can also do:
grafik

1 Like

@ppr
Thank you for the response. Could you please share your code with me as a file? I’m experiencing an error in the regex, so I would appreciate your assistance.

@22222222asas
May I know what is the error you are getting

1 Like

@pravallikapaluri
Thank you for your response! I think the error was my mistake.I was amazed by your answer. Could you please guide me on how to save the last output to each cell in an Excel file?

EX)
image

@22222222asas

Use a for loop on System.Text.RegularExpressions.Regex.Matches(inputString, ".{1,25}(\s|$)")

Inside loop use write cell and use auto increment checked on it…and gice the cell value as Excel.Sheet("SheetName").Cell("A1") and then value as currentitem.Value

1 Like

@Anil_G
Thank you for your response. I tried to create it as you suggested, but an error occurred. Could you please let me know what went wrong?

image

@22222222asas

  1. this is old activity soc hange the type argument to regex.match
  2. in write cell use item.value
  3. in the in argument you need to send only this System.Text.RegularExpressions.Regex.Matches(inputString, ".{1,25}(\s|$)") without string.join part

cheers

@22222222asas

str_in="Bussiness Corp.3000 UinateaMinamia city, KanaUSA 3000Phone:Fax:"
inputString=System.Text.RegularExpressions.Regex.Matches(str_in, ".{1,25}(\s|$)")
Counter=1

Hope it helps!!

1 Like

Thank you so much! I could easily solve it with the regular expression! I’m in awe of the perfect solution! Have a great day! Thank you!

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