Removing the first numbers in a string

Hi Im trying to remove the 1st 2 numbers in my list but i cant seem to make it work i always get a null value whenever i use a substring(0, str-2) function i even tried remove but what it removes is the last 2 digits of my numbers my sample number is 0420 and i want to remove the 04 and get the 20.

Help is greatly appreciated I have been stuck in this problem since 2days ago.

Hey @Arvin_Gabriel_Mari_A

You can do this with String.Remove().

String str_data= "0420";
String new_data = str_data.Remove(0,2) 

Regards…!!
Aksh

6 Likes

HI @Arvin_Gabriel_Mari_A,
You can go by @aksh1yadav’s solution if you want to remove.
Else you can try with the sub string as follows:

It will give you the result excluding the first two characters/numbers.
Hope it helps.
Regards,
Jiban

3 Likes

Hello, I have a similar requirement. I am reading (1,00) such value into a variable and i want to use only 1 out of the string and ignore the zeroes. how can i use Sub-string in this case?

Thanks !!

@Maverick

Split based on colum and get the first element
stra=1,00

str B= stra.Split({“,”},StringSplitOptions.RemoveEmptyEntries).ToList()(0).ToString

Regards,
Mahesh

Hi @Maverick,

Convert the number into string and follow this method.

Substring helpes here…

It will be successful even if you remove comma from the entire number.
image

Thanks,
Jiban

Thanks, this is working like i wanted !!

1 Like

Hi Mahesh,
your solution is more flexible,as it is working with all values. thanks !!
I need to do a summation of this column, containing comma values like… 1,00 2,00 10,00 etc. Comma represents decimal notation so i need to ignore the zeroes and consider only preceeding value. I tried using “Compute(SUM(columnname)” but it won’t work… how can i alter the values in datatable itself and do the summation?

1 Like

@Maverick

Try this

Declare a variable with type double Sum

Sum=dt.AsEnumerable.Sum(Function(row) Convert.ToDouble(row(“Column_Name”).ToString.Split({“,”},stringSplitOptions.RemoveEmptyEntries)(0)))

Regards,
Mahesh

1 Like

Hi mahesh…

I have a data like this 1,234.00 i need to remove the decimal nd 2 zeroes. Can u help m

@Darshhan

Let us take

strA=1,234.00

strB=strA.Split({“.”,StringSplitOptions.RemoveEmptyEntries).ToArray(0).ToString

Regards,
Mahesh

Hi @Darshhan,

This activity will help you. It has with sample

Regards
Balamurugan.S

Hi mahesh…

I scraped a structured data from the web. The problem is that some string in the data contains comma, and when I put the structured data in a csv that’s a problem because I can’t divide it properly with the unwanted comma.

How can I scan over the entire structured data to remove the comma and afterwards write the csv?

Thanks a lot
Stefano

Hi @Stefano_Skjupyter

You can use a simple .Replace method on a string, like this:

yourString.Replace(",","")

This replaces all commas with nothing, effectively removing all commas.

1 Like

Thanks @loginerror

the problem is my data type is a data table, not a single variable type string. I think I should iterate over it with a for cycle for example. is that correct?

Stefano

Indeed, that would be the approach. I am pretty sure someone knows a 1-liner for that in an Assign activity, but they would have to speak up (maybe @ClaytonM, if he feels like it; I don’t like using direct tags like that :slight_smile: )

Hi @Stefano_Skjupyter

From my understanding, you can’t change row items in a datatable without using a For Each to loop through each row.

Also, you don’t necessarily need to remove the comma because if you surround that item with quotes, then the CSV will delimit the value correctly.

So basically if would look like this:

For each activity: For each row in dt1
            If row.Item("columnname").ToString.Contains(",")
                  Assign: row.Item("columnname") = """"+ row.Item("columnname").ToString+""""
                  // or row.Item("columnname") = row.Item("columnname").ToString.Replace(",","")

So that would loop through each row and change the value using an Assign activity.

If the data has thousands of rows, this may not be an efficient solution. The other method would be to manipulate the entire structured data as a string (ie using Output Data Table). However, if you look at the entire row as one string, I don’t know how you would identify which commas are delimiters and which commas need to be removed.

EDIT: added if condition in pseudocode to check if value contains comma

Regards.

Oh, you can also filter your table prior to replacing the commas, rather than looping through each row.

That would be like this:

For Each (Framework) activity: For each row in dt1.AsEnumerable.Where(Function(r) r("columnname").ToString.Contains(",") ).ToArray
         Assign: row("columnname") = """"+ row.Item("columnname").ToString+""""
                  // or row.Item("columnname") = row.Item("columnname").ToString.Replace(",","")

I am used to programming in python where I can put I piece of code essentially where I want to, but in this platform where should I copy paste your piece of code to be executed??

sorry if the question might sound silly

Regards

Sorry let me clarify. My examples were just pseudocode as a representation of the logic.

Here is a more visual representation done in UiPath:
image

Here are the example For Each activities in a workflow:
foreach.xaml (12.9 KB)

1 Like