peter
peter

Reputation: 8652

How to replace a particular character from string in c#?

I have a string like AX_1234X_12345_X_CXY, I want to remove X from after the first underscore _ i.e. from 1234X to 1234. So final output will be like AX_1234_12345_X_CXY. How to do it?? If I use .Replace("X", "") it will replace all X which I don't want

Upvotes: 1

Views: 639

Answers (7)

Charles Owen
Charles Owen

Reputation: 2840

Something like this could work. I'm sure there's a more elegant solution.

 string input1 = "AX_1234X_12345_X_CXY";
 string pattern1 = "^[A-Z]{1,2}_[0-9]{1,4}(X)";
 string newInput = string.Empty;
 Match match = Regex.Match(input1, pattern1);
 if(match.Success){
    newInput =  input1.Remove(match.Groups[1].Index, 1);
 }            
 Console.WriteLine(newInput);

Upvotes: 0

Charles May
Charles May

Reputation: 1763

If and only if this is always the format then it should be a simple matter of combining substrings of the original text without including the x in that position. But the op hasn't stated that this is always the case. So if this is always the format and the same character position is always removed then you could simply just

    string s = "AX_1234X_12345_X_CXY";
    string newstring = s.Substring(0, 7) + s.Substring(8);

OK, based on only the second set of numbers being variable in length, you could then do something like:

int startpos = s.IndexOf('_', 4);
string newstring = s.Substring(0, startpos - 1) + s.Substring(startpos);

with this code, the following tests resulted in:

"AX_1234X_12345_X_CXY" became "AX_1234_12345_X_CXY"
"AX_123X_12345_X_CXY"  became "AX_123_12345_X_CXY"
"AX_234X_12345_X_CXY"  became "AX_234_12345_X_CXY"
"AX_1X_12345_X_CXY"    became "AX_1_12345_X_CXY"

Upvotes: 0

user117499
user117499

Reputation:

Try looking at string.IndexOf or string.IndexOfAny

string s = "AX_1234X_12345_X_CXY";
string ns = HappyChap(s);

public string HappyChap(string value)
{
        int start = value.IndexOf("X_");
        int next = start;

        next = value.IndexOf("X_", start + 1);
        if (next > 0)
        {
            value = value.Remove(next, 1);
        }

        return value;
}

Upvotes: 1

Jonathan Applebaum
Jonathan Applebaum

Reputation: 5986

You can iterate trough the string from the first occurrence of '_' .
you can find the first occurrence of '_' using IndexOf().
when loop will get to 'X' it will not append it to the "fixed string".

private static void Func()
{
    string Original = "AX_1234X_12345_X_CXY";
    string Fixed = Original.Substring(0, Original.IndexOf("_", 0));
    // in case you want to remove all 'X`s' after first occurrence of `'_'` 
    // just dont use that variable
    bool found = false; 

    for (int i = Original.IndexOf("_", 0); i < Original.Length; i++)
    {
        if (Original[i].ToString()=="X" && found == false)
        {
            found = true;
        }
        else
        {
            Fixed += Original[i];
        }
    }

    Console.WriteLine(Fixed);
    Console.ReadLine();
}

Upvotes: 2

TomServo
TomServo

Reputation: 7409

One way is String.Remove, because you can tell exactly where to remove from. If the offending "X" is always in the same place, you can use:

string newString = old.Remove(7,1);

This will remove 1 character starting as position 7 (counting from zero as the beginning of the string).

If not always in the same character position, you might try:

int xPos = old.IndexOf("X");
string newString = old.Remove(xPos,1);

EDIT:

Based on OP comment, the "X" we're targeting occurs just after the first underscore character, so let's index off of the first underscore:

int iPosUnderscore = old.IndexOf("_");
string newString = old.Remove(iPosUnderscore + 1 ,1); // start after the underscore

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

Why not good old IndexOf and Substring?

  string s = "AX_1234X_12345_X_CXY";

  int pUnder = s.IndexOf('_');

  if (pUnder >= 0) { // we have underscope...
    int pX = s.IndexOf('X', pUnder + 1); // we should search for X after the underscope

    if (pX >= 0) // ...as well as X after the underscope
      s = s.Substring(0, pX) + s.Substring(pX + 1);
  }

  Console.Write(s);

Outcome:

  AX_1234_12345_X_CXY

Upvotes: 2

Sam Axe
Sam Axe

Reputation: 33738

string original = @"AX_1234X_12345_X_CXY";

original = @"AX_1234_12345_X_CXY";

Upvotes: 1

Related Questions