edof
edof

Reputation: 18

Split and delete some data to another textbox

I have incoming serial data like my screenshot below. I want to split data from richtextbox (that have any string there) to another textbox (the one below richtextbox). but I only take any part of them, actually string after C. I want to delete 12 first string and then put them to another textbox.

FYI, my program is a RFID Reader and this is how my program works:

when I click select tag send, serial will send data and place them to richTextBox. e.g data is 01018200000C000000000000000000000003 and I want 000000000000000000000003 go to another textbox (because this is the tag ID, and characters before it are message detail). After that I click send and next data will showed on richTextBox then newest tag ID will replace first tag ID in textBox, and so on.

If data from serial not error, it will always send me format like that and I need to grab tag ID only so that's why I need to delete first 12 characters. But if error serial send me only 4 characters.

Anyone know easy way to do that? I have tried some codes but there is still error this is my code:

private void SetText(string text)
    {
        if (this.rtbIncoming.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(SetText);
            this.Invoke(d, new object[] { text });
        }
        else
        {
            string[] newData = text.Split('C');

            tagBox.Text = newData[1];

        }

        this.rtbIncoming.Text += text;

    }

enter image description here

Edit:

after investigating any further, there is an error in the data that I have received, and after I assure data received appropriate, I think all the answers are works to me. But I choose answer from Pranav-BitWiser to skipping data, because using split at 'C' character will make more difficult if tag ID have 'C' character on it. Thanks for your answers, it really helpful :)

Upvotes: 0

Views: 394

Answers (4)

Kavindu Dodanduwa
Kavindu Dodanduwa

Reputation: 13059

If 12 characters are fixed you can use substring

String input = "01018200000c000000000000000000000003";
String portion = input.Substring(13); // Note the 0 based index 
Console.WriteLine(portion); // Verify test

Now you can use portionon your required textbox

textbox_one.Text = portion;

Edit : if your String is not fixed length try something like this [taking it has same format] + Will determine error state

Quoting :

If data from serial not error, it will always send me format like that and I need to grab tag ID only so that's why I need to delete first 12 characters. But if error serial send me only 4 characters.

Suggestion:

String input = "01018200000c000000000000000000000003"; // This is input you get
String portion;

if(input.Length > 4){ 
    if(input.IndexOf("c")!=0){
          portion = input.Substring(input.IndexOf("c")+1); // Note the 0 based index 
          Console.WriteLine(portion); // Verify test
          textbox_one.Text = portion;
   }
   else{
         // Not a valid tag
         textbox_one.Text = "Error Tag :" + input;
   }
}  

Upvotes: 1

Abhishek
Abhishek

Reputation: 2945

Try this:

string serialData;
serialData = "01018200000C000000000000000000000003";
textBox.Text = serialData.Split('C')[1];
serialData = //Your new serial data here
textBox.Text = serialData.Split('C')[1];

I hope split function helps. This will work only if your string has one "C". If it has more than one C, it splits wherever a C occurs in the string. Can you confirm if your string has only one C or more than one Cs?

Upvotes: 0

Pranav Bilurkar
Pranav Bilurkar

Reputation: 965

If you want to always delete first 12 characters from string then you can use:

 string data = "01018200000c000000000000000000000003";
 TextBoxID.Text= data.Remove(0, 12);

OR

TextBoxID.Text = new string(data.Skip(12).ToArray());

Here- TextBoxID is the ID of your TextBox

Upvotes: 1

itzmebibin
itzmebibin

Reputation: 9439

Try this,

string data = "01018200000c000000000000000000000003"; string[] splitdata = data.Split('c'); textBox2.Text = splitdata[1].ToString();

Upvotes: 2

Related Questions