Skip to content Skip to sidebar Skip to footer

How To Parse Text From Multiple Strings?

I am using a terminal emulator library to create a terminal and then I use it to send the data entered over serial to a serial device. When the data is sent back I want to parse it

Solution 1:

Basically, you need a way to reliably detect the end of a command result. Then it boils down to sending your command, reading data from the device until you encounter the end of result, and finally parsing that result.

I would scan for the prompt (switch#) as you do in your own answer. Maybe your are even able to force the device to use a more peculiar character sequence, which is unlikely to occur in the regular output of commands and makes it easier to detect the end of a result. For example, you could try to configure the prompt to include a control character like ^G or ^L. Or if your users don't mind, you could always send a second command that emits such a sequence, for example, "show vlan; echo ^G".

You should also be prepared for command errors, which result in a different output, for example, more or fewer lines as expected or a totally different output format. A result may even contain both, a regular output and a warning or an error.

Solution 2:

I solved this in a messy way with a boolean and a few strings. i made a method for appending strings.

if((parseCommand.contains("VLAN Name") && parseCommand.contains("Status")&& parseCommand.contains("Ports")) 
                            || ((ShowVlanAppend.contains("VLAN Name")&& ShowVlanAppend.contains("Status")&& ShowVlanAppend.contains("Ports"))))
                        {
                                commandParse();
                                if(finalCommandBool == true){
                                runOnUiThread(new Runnable() {
                                    publicvoidrun() {

                                        mReceiveBox.setText(finalCommand);
                                        mReceiveBox.setSelection(finalCommand.length());
                                        ShowVlanAppend = "";
                                        finalCommand = "";
                                        finalCommandBool = false;
                                    }
                                });

                                }


                        }
 publicvoidcommandParse()

        {
            if (!parseCommand.contains("switch#")){
            ShowVlanAppend = ShowVlanAppend + parseCommand;
            }
            else{
                finalCommand = ShowVlanAppend;
                finalCommandBool = true;
            }

        }

Post a Comment for "How To Parse Text From Multiple Strings?"