-5

Good day, I asked this question before but i wasn't specific for what I apologize. I'm making a simple windForms Chess Game using picture Boxes as each cell. According to the rules, the King can't move if the cell is targeted by an enemy piece. To implement this rule I'm using pictureBox.Tag property and assign a string to it. If a piece targets it I use pictureBox1.Tag += "D" D as in Danger. So if two pieces are targeting it the Tag will become "DD". My question is this - How do I remove just one 'D' from my string ? Can I use -= operator or something similar?

user3552161
  • 15
  • 1
  • 6
  • Take a look at the SubString method. – Transcendent Jun 09 '14 at 20:31
  • 1
    Are you keeping a large amount of meta info in these strings or is it just the D? If it is just the D then do an = not a +=. – stonemetal Jun 09 '14 at 20:32
  • I'm just learning programming, I searched the web and didn't find what I need. If I'm asking an obvious question, please give me some kind of answer or at least a link to an answer. – user3552161 Jun 09 '14 at 20:33
  • http://stackoverflow.com/questions/7494654/strings-immutability – kenny Jun 09 '14 at 20:33
  • @kenny, that's for java, but the same principle applies. – gunr2171 Jun 09 '14 at 20:36
  • @stonemetal the maximum ammount of D's is about 16 since so many pieces can target a single cell all at once. I'm also using lower case 'd' to distinguish friendly danger from enemy e.g. a cell's tag during a game can look like "dDDddD". I already wrote the algorythm that adds and removes 'D' according to piece movement. The only problem now is how to remove just 'D' – user3552161 Jun 09 '14 at 20:37
  • Regarding `dDDddD`, please check my answer and consider using an object oriented approach. Your code will become much easier to read and maintain this way. – Victor Zakharov Jun 09 '14 at 20:43

5 Answers5

1

Assuming:

string a = "ABCDEFG";

To remove the first 'D':

a = a.Remove(a.IndexOf('D'), 1);

To remove all 'D's

a = new string(a.Where(c => c != 'D').ToArray());

Although I would recommend looking at object oriented approach. Then you could easily store references to the actual chessmen who target a spot, hence easy to modify (no need to recalculate).

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
1

While there ware ways to do what you're trying to do, what you really want to do is ditch string manipulation, and use something else. For instance, create a whole class for this square meta-information. Something like

public class SquareInfo
{
    public int Danger; //the number of pieces that can move to this square.
    //... Any other information about the square you want.
}

Then you could grab the tag as:

var myInfo = (SquareInfo)myBox.Tag;

if (myInfo.Danger > 2)
{
//do something
}

And so on.

Pharylon
  • 9,796
  • 3
  • 35
  • 59
0

Simply use the Replace method, replacing your character (as a string) with an empty string:

string s = "Daddy comes back";
s = s.Replace("D", string.Empty);   // Replace ALL D'
Console.WriteLine(s);

Alternatively, if you already know the index of the characters to be removed, or if you need to remove a single character, consider using Substring methods.

s = s.Substring(i, i+1); // Remove the character at position i

Note that a C# string is immutable: in both cases, a new string instance is returned.

quantdev
  • 23,517
  • 5
  • 55
  • 88
0

I think this would solve your problem

public void Remove (PictureBox pb){
     if (pb.Tag.ToString().Length > 1) {
         // Greater than 1 because we need to keep one D in case of DD, 
         String temp = pb.Tag.ToString();
         pb.Tag = temp.Substring(0, temp.Length - 2);
     }
     else 
        pb.Tag = "D";
        // Tag equals D because if there is only one D, it won't be deleted  
}
Transcendent
  • 5,598
  • 4
  • 24
  • 47
0

You can use a regex to specify to remove just one instance of a character:

Regex regex = new Regex(Regex.Escape("D"));
string output = regex.Replace("ABCDDEFG", string.Empty, 1);
PulseLab
  • 1,577
  • 11
  • 15
  • 1
    With all due respect, I think that using regular expressions is an overkill for this particular task. – Victor Zakharov Jun 09 '14 at 20:44
  • I respect your comments guys, although in the case of defence: @Neolisk, I guess it's a matter of opinion. I think this is about as simple as any regex operation can be, and as the OP says he is learning, it's a simple way to be introduced to a useful tool. quantdev, I don't think there's an appreciable performance issue here, especially considering the nature of the program described by the OP. In any event, fair comments both. – PulseLab Jun 10 '14 at 08:15