Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for Removing All the repetation of a string and assign to an array

I have the following text in a file:

"SHOP_ORDER001","SHOP_ORDER002","SHOP_ORDER003","SHOP_ORDER004","SHOP_ORDER005"

Now I am getting the values by reading the file and assigning to array by spilt:

 String orderValue = "";
 string[] orderArray;
 orderValue = File.ReadAllText(@"C:\File.txt");
 orderArray = orderValue.Split(',');

But I am getting the values as : enter image description here

I need the Values in Array as "ORDER001","ORDER002","ORDER003"

like image 763
Simsons Avatar asked Jan 29 '26 19:01

Simsons


1 Answers

The \" you see is just added by debugger visualizer for strings (because quote is a special characted and need to be escaped to don't get confused), don't worry they're not in your orderArray.

In case you want to remove quotes too so that your array will be:

SHOP_ORDER001
SHOP_ORDER002
...

Just use this (with LINQ):

var orderArray = orderValue.Split(',').Select(x => x.Trim('"'));

By the way String.Split isn't very robust unless you're sure each field will never contain a comma.

EDIT
To answer the point you added in the comments if you need to remove SHOP_ just write this:

var orderArray = orderValue.Split(',')
    .Select(x => x.Trim('"').Substring("SHOP_".Length));
like image 86
Adriano Repetti Avatar answered Feb 01 '26 10:02

Adriano Repetti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!