Remove Information from a String

0

hi I have a string of characters in which I would like to extract certain information Signal level = -9 dBm this information I want to get out of that string but I do not know how to do it in c # thanks

Thank you very much for helping me it works fine scans me all the networks and points out the most powerful now I would like your opinions on that code and how I could optimize it

    
asked by carlos1016 04.05.2018 в 04:35
source

2 answers

1

Given any string, you can locate a substring of it using the string.indexof function. With that, you get the position of a piece of the string. Then we could try to locate "Signal level=", inside your string. Once located that, we can also search "dBm", to know how many characters are in the middle. And with that, get the value you're looking for.

string cadena = @"hola tengo una cadena de caracteres en la cual quisiera sacar cierta información Signal level=-9 dBm esta información deseo sacar de esa cadena pero no se como hacer en c# gracias";
int pos = cadena.IndexOf("Signal level") + ("Signal level=").Length;
int lastpos = cadena.IndexOf("dBm")+3;
string valor = cadena.Substring(pos, lastpos - pos);
Console.WriteLine(valor);
Console.ReadKey();

You can try the above code in a console application, and you will see that it prints the value you are looking for. If necessary, you can adjust the start and end values (in case there are spaces or things like that).

In the example, use the string of your question, ("Signal level=").Length; This instruction, for example, returns the length of that string, since indexof returns the beginning of the string you are looking for.

    
answered by 04.05.2018 / 06:02
source
2

You can use the Split function to separate all that line that is a variable of type String in an array of sub-strings. Of course, it would be easier if you added to your total array (which in this case is Quality=87/94 Signal level=9dBm Noise level=103dBm ) a separation. For example, with commas; in other words, that it was in the following way:

Quality=87/94, Signal level=9dBm, Noise level=103dBm

This separates that string into substrings, and each substring stores it in an array. At the end you will put in the text box the position of the array that has the sub-string that has Signal level=9dBm .

string charCommaString = "Quality=87/94, Signal level=9dBm, Noise level=103dBm";
char[] commaSeparator = new char[] { ',' };
string[] result;
result = charCommaString.Split(commaSeparator, StringSplitOptions.None);
Console.WriteLine(result[1]);

Look that my string charCommaString contains the whole line that you want to extract only one part. I only added 3 commas.

In the fourth line of code what I do is use the Split() method, which is responsible for separating the text of the variable String charCommaString in sub-strings every time it finds a separator. In this case, the separator is a comma ",".

I hope my answer will help you.

    
answered by 04.05.2018 в 04:57