Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Replace Everything before the first Space

Tags:

c#

regex

replace

I need to remove everything in a string before the first occurrence of a space.

  1. Every string starts with a number and followed by a space
  2. Replace the number and the space, thus leaving the rest of the string in tact

For Example:

22 The cats of India

4 Royal Highness

562 Eating Potatoes

42 Biscuits in the 2nd fridge

2564 Niagara Falls at 2 PM

I just need:

The cats of India

Royal Highness

Eating Potatoes

Biscuits in the 2nd fridge

Niagara Falls at 2 PM

Basically remove every number before the first space, including the first space.

I tried this:

foreach (string line in lines)
{
       string newline = line.Trim().Remove(0, line.IndexOf(' ') + 1);
}

This works for numbers below 10. After it hits 2 digits, it doesn't work properly.

How should I change my code?

like image 928
Dinuka Jay Avatar asked Oct 16 '25 17:10

Dinuka Jay


2 Answers

If you want to make sure you only match digits at the beginning of the string, you can use the following regex:

^\d+\p{Zs}

See demo

Declare it like:

public static readonly Regex rx = new Regex(@"^\d+\p{Zs}", RegexOptions.Compiled);

The ^\d+\p{Zs} regex means: one or more digits at the start of the string followed with 1 whitespace.

And then use it like

string newline = rx.Replace(line, string.Empty);

EDIT: To make sure the line has no leading whitespace, we can add .Trim() to strip it like:

Regex rx = new Regex(@"^\d+\p{Zs}", RegexOptions.Compiled);
string newline = rx.Replace(line.Trim(), string.Empty);
like image 198
Wiktor Stribiżew Avatar answered Oct 19 '25 09:10

Wiktor Stribiżew


I know you already found a resolution to your issue. But I am going to explain why your code didn't work in the first place.

Your Code

Your data has extra spaces which is why you are trimming it: line.Trim(). But the real problem lies in the the following statement:

string newline = line.Trim().Remove(0, line.IndexOf(' ') + 1);

You are making the assumption about the order of the operation and the fact that string data type is not immutable. When the operation of Trim() function is complete it returns a whole new string which is used in the Remove() operation. But the IndexOf() function is done on the original line of data.

So the correct line of code would be the following:

foreach (string line in lines)
{
    // trim the line first
    var temp = line.Trim();

    // now perform all operation on the new temporary string
    string newline = temp.Remove(0, temp.IndexOf(' ') + 1);

    // debugging purpose
    Console.WriteLine(newline);
}
like image 41
Black Frog Avatar answered Oct 19 '25 07:10

Black Frog