Please help with string manipulation

Are you assigning the value its pulling from excel to a Variable?

You can for example contain the value in VariableIamReplacingWith

Is the value of type string?
If not convert it, try .toString:

myVariable.replace(myVariable.Split("."c)(1).tostring, ". "+VariableIamReplacingWith.toString)

Or convert it before.

That’s what I initially did with my Replace Text activity
Replace Text
"Modification No. " - "Modification No. " + VariableIamReplacingWIth and it keeps the last part i.e. P00004 or 016 or whatever the rest of the line is so it looks like
Modification No. 5P00004

Please show me your .xaml or at least a screenshot. I think we are misunderstanding each other. There is no ‘replace text’ activity being used in my answer. You shouldn’t have an input text at all, and there should be no ‘replace text’ activity, it’s all done in a single assign activity

I am modifying a word document that has "Modification No. and an unknown value like P00004 after it. That’s why I was trying to use a Replace Text activity.

I completely understand what you’re trying to do. I just don’t understand why the “replace text” is necessary. You need to use “replace text” only when you are replacing substrings sporadically throughout a string. If you are just hardcoding the front and appending a new variable to the end, then you should not use “replace text” and should instead use "hardcode text " + YourStringVariable instead

EDIT: again, if you provide an .xaml or at least a screenshot of your workflow it may make it easier

I use Word Application Scope since I am modifying an existing word document.
I don’t understand the flow you’re suggesting. If I assign myVariable= “Modification No.” and then use assign for IntendedOutput= myVariable + VariableIamReplacingWith, nothing happens

Can you give a Screenshot of your Excel file(Replace info if it is confidential)

Then give the wanted output you want.

The values coming out of the excel file works. I can easily add the valueIamReplacing in my xaml. My only issue is getting the unknown value i.e. P00004, 016, or whatever else coming after Modificiation No. to be removed.

Why not use the stuff i provided from before:

newVariable = yourVariable.Substring(0,16)

yourVariable could contain Modificiation No. <whatever>

Then create a new variable resultVariable:

resultVariable = newVariable + stringYouWantInstead

resultVariable will ouput Modification No. <whatever>

This only works if “yourVariable” always starts with “Modification No.”

Right.

My variable always starts with “Modification No.”
The write line output for what you describe works.
My problem is that it does not replace the P00004. I cannot put “Modification No. P00004” as the value of my variable because the P00004 will not always follow Modification Number.

myVariable= “Modification No.”
Assign newVariable= myVariable.substring(0,16) output “Modification No.”
Assign resultVariable= newVariable + myVariable output “Modification No. 4”
The write line shows the correct output but nothing happens to my word doc, which is why I think I need a Replace Text activity but it won’t remove the P00004, or any value following the Modification No. The result when I use a replace text activity is Modification No. 4 P00004

@Jessica_Moseley - the reason we are all struggling to provide an answer is because of the way the question is worded. It is still not clear what you’re trying to do. Please provide 3 things:

  1. An example/sample of a FULL TEXT representation of what the input may look like
  2. An example of what you want the final output to look like
  3. A quick explanation of what you’re trying to achieve, what you’ve tried so far, what you’re struggling with, etc.

So far, you’ve only provided a portion of each of these.

@Jessica_Moseley…“30 replies”.People are trying to help here…did you consider their request of providing the xaml file?

Not to be mean: this simple stuff is dragging this post …

1 Like

My apologies. I am trying to be as clear as possible. I cannot share the xaml because it’s for a client.

The bot reads an excel cell and the output is variable: modNumber (attached image shows ‘4’ is the modNumber)
The bot opens the word document that has a different value (attached image shows ‘Modification No. P00003’)
I want the final output to look like ‘Modification No. 4’

DatatoChange Output

So the ENTIRE TEXT of the input text is: Modification No. P00003 is that correct? Because if not, that is exactly why you are getting bad answers.

If it is the entire text, then you should do as I’ve stated before. assign Final Output = "Modification No. " + modNumber and write that to your word doc (or whatever your output format is going to be).

If it’s NOT the entire text, then we will need to get a better sample of what the input text will look like. At the very least, we need to know if this is in the middle of the doc, or only at the front, if you need to keep whatever is before/after this text, etc. The more info the better…

The ENTIRE TEXT of the input is a variation of Modification No. P00003 or could be Modification No. 016, etc. It will always start with Modification No. and it is in the header. Attached is what the header looks like.

I’m confused on how to write that to my word doc, which is why I was using the Replace Text activity.

Ok, based on that picture Modification… is not the entire text. It is a substring within a larger amount of text in your entire word document.

Based on this new info, I think your best bet is to use regex. First step is to read the entire word document (i’ll call it InputText).

Next, use regex to find the correct “junk” you want to replace. You haven’t provided enough data or examples to give a 100% accurate regex, but this should be a good start for you: (?<=Modification No\.\s*)\S+\b - this regex will find the first word (any number/letter/character between 2 spaces) that occurs after “Modification No.”. Save the value here to a string variable I’ll call junkString
Assign junkString = System.Text.RegularExpressions.Regex.Match(InputText,"(?<=Modification No\.\s*)\S+\b").Value

Now that you know the “junk” to replace with your new word, you can utilize the ‘replace text in document’ activity.
Search for: "Modification No. " + junkString
Replace with: "Modification No. " + modNumber

And that’s it! Now your word document should have the proper modNumber coming from your excel document. Be sure the ‘autosave’ is checked in your word application scope so it saves everything properly

2 Likes

@Dave @atomic
Thank you for your help!

Hello Jessica,
In this video I do a lot of stuff with String:

0:35 Examples for Substring functions
4:10 IndexOf and LastIndexOf
5:00 SubString working together IndexOf and LastIndexOf
6:45 Split string by character
8:50 Split string by string
12:00 Lower Case and Upper Case
12:45 Trim
15:05 Compare strings in multiple ways
19:05 Resume of all the String function part

Thanks,
Cristian Negulescu