-1

I have an string array of size 10:

[0] = "1,0000000" [1] = "479,00000" .... [9] = "145,0".

I want to remove the trailing ",00000" of the first 9 elements with regex and linq. Please help.

1
  • loop through the array and replace each element and do replacement like, Regex.Replace(string, ",0+$", "") add * instead of + if necessary. Commented Feb 26, 2015 at 10:33

3 Answers 3

6

Basically I would do this like that :

var yourArray = new string[10];
var yourResult = yourArray.Take(9).Select(s => s.Split(',')[0]).ToArray();

But you can replace the Select method content with a Regex call if you wish.

Sign up to request clarification or add additional context in comments.

3 Comments

Cool solution, using the split
My answer is very basic, but so is his question.
There will be no such cases. I did it in a lil bit different way with .Select(x => Regex.Replace(x, ",0+$", "")).ToArray(). Anyway...good to know. Nice!
3

Use a for-loop and string methods like IndexOf and Substring:

for(int i = 0; i < Math.Min(array.Length, 9); i++)
{
    int commaIndex = array[i].IndexOf(",");
    if(commaIndex >= 0) array[i] = array[i].Substring(0, commaIndex);
}

Comments

0

You can simply try

data.forEach(x => x.replace("ABC", "'"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.