简体   繁体   中英

Cannot concat string in C#

I have a code to read data block inside MIFARE card.

The method rfidM1.ReadDataFromCardM1 will read a block and return value in string .

string memQuery = string.Empty;
int i = 0, j = 0;
sector = 4;
block = 4;

for (i = 0; i < block; i++)
{
    for (j = 0; j < sector; j++)
    {                  
        memQuery += rfidM1.ReadDataFromCardM1(Convert.ToByte(j), Convert.ToByte(i), _Key1) + ",";
    }
}          

My intention is concating memQuery with comma. Example output here:

,0,,,,,True,,C0-12320,0,,,,,,

I concat memQuery with various ways, for example, using += , StringBuilder or ArrayList but they didn't work because it always has an output like this when I put it in MessageBox.

,0

It looks like string after that 0 cannot concat with other string after it. Why?

My intention is concating memQuery with comma

Wel, first get rid of the ArrayList and replace it with var memQuery = new List<string>(); .

Inside your for-loops, decide what to with null or empty results. Add a null or skip the Add or ...

And then when the memQuery is filled correctly, you can do

 string result = string.Join(",", memQuery);

string.Join() can handle null s in the input sequence.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM