Unable to trim space, while other symbols are trimmed


#1

varSplit(2).ToString.Replace(" “,”").Replace(",","").Replace("-","").Trim

This changes the value of the variable from e.g. “1 622,-” to “1 622”

Have tried searching both this forum and google without finding any reason why space is the only char I’m not able to remobe from the variable.


#2

This seems odd, so I’m not sure.
But, there are also alternative ways to remove characters.

System.Text.RegularExpressions.Regex.Replace(varSplit(2).ToString,"[^0-9]","")
the above example will remove all characters except numbers.

Regards.


#3

Worked perfectly! If it is an int32 variable, will it work just as good if I drop the “.ToString”?


#4

Great!
It needs to be a string for the Replace to work, but you can convert it back with Convert.ToInt32() or CInt()


#5

Thank you!


#6

I’ve tried this and not getting what I expect.
So i’m reading a CSV and the variable name is CSVFundSelection1 and in that cell it has Emerging Markets (100). And what i’m wanting to do is remove everything but the 100.
This is what i put in the Assign:
image
System.Text.RegularExpressions.Regex.Replace(CSVFundSelection1.ToString,"[^0-9]","")

Can you give me assistance?
Thanks!


#7

Is CSVFunSelection1 the string that equals “Emerging Markets (100)”? And, what is the output you are getting after the assign activity?

Regards.


#8

I’m good now. Spoke too soon.


#9

So now i have a new issue so the CSVFundSelection1 now has Emerging 2020 Markets (1188). So when i use the above expression i get 20201188. and all i need is 1188.
I have tried this and it doesn’t work:
CSVFundSelection1.Trim.ToString.Replace("(","" ).Replace(")", “”)

I’ve been able to remove the ( ) but all the text stays.
Thanks!


#10

value[]=CSVFundSelection1.Split.ToString("(“c),You will get your value in value(1)
string = string.Replace(”)", “”)
@tmartin


#11

image


#12

browsers.xaml (5.5 KB)


#13

I’m looking into it now. and trying to get this to work.


#14

I would still suggest you use a Regex pattern. You can find out how to do the pattern with many examples and tutorials online.

Using the Split option works though, but I would normally do it like this:

CSVFundSelection1.Split("("c)(1).Split(")"c)(0)

So it takes the string after “(” and before “)”

The Regex solution would be like this:

System.Text.RegularExpression.Regex.Match(CSVFundSelection1, "(?<=\()(.*)(?=\))").Value
or
System.Text.RegularExpression.Regex.Match(CSVFundSelection1, "(?<=\()([0-9]{1,10})(?=\))").Value

So it simply pulls the string that is between ( ) or second one is pull it if only it’s digits 0-9
Also, get in the habit of storing this pattern in a variable or part of a config dictionary.

Regards.


#15

thanks. i will try this now. i appreciate your assistance.


#16

I’ll also add that if you use the Split method, make sure you verify it contains a Parenthesis or it will throw an error. You can do this with an If activity or using If() inside the code, like my below example…

If(CSVFundSelection1.Contains("(") and CSVFundSelection1.Contains(")"), CSVFundSelection1.Split("("c)(1).Split(")"c)(0), "")

The Regex method does not need this though.

Regards.


#17

This worked great. Thanks so much for your assistance.


#18

OK. I have another csv question. So my cell has two values in it and the first part can vary but i know the end of the cell the last 11 digits i need to pull in one variable and the first part i can do a trim to end to get the empty spaces.
Here is what the cell looks like:
image