Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello. How do I separate/extract the value 22.2 from the following:

0065.0 0022.2 0050.0 0029.1 0100.0 0100.0 0000.0 0011.2 0000.0 -000.1 0000.0 -000.1 0000.0 -000.1 00000000000000000000000000000000


What I have tried:

It is the output of a device and I need to read the 2 parameter which is 22.2. any ideas would be appreciated
Posted
Updated 19-Nov-19 19:21pm
v2
Comments
F-ES Sitecore 15-Nov-19 8:00am    
The answer depends on what the possible formats are. If the format never changes from what you've posted then use

yourString.Substring(9, 4)

That will return 4 characters from position 9. If the format can change then we can't answer the question unless we know how it can change.
Member 14589606 15-Nov-19 17:35pm    
It says it exceeds the index.
I receive the response from the device and I save it in a string and then try to separate it or use your method of yourString.Substring(9,4).

If i try writeLine on the string it prints each value in a separate line like this
0
0
6
5

Hence It states that the index exceeds

string str = "0065.0 0022.2 0050.0 0029.1 0100.0 0100.0 0000.0 0011.2 0000.0 -000.1 0000.0 -000.1 0000.0 -000.1 00000000000000000000000000000000";
string[] arr = str.Split(' ');
double number;

// Strip non-numeric characters
arr[1] = string(arr[1].Where(c => char.IsDigit(c)).ToArray());

if (Double.TryParse(arr[1], out number)) 
            Debug.Print("The 2nd value = " + number);
For a way to strip non-numeric characters from your string, see: Stripping out non-numeric characters in string - Stack Overflow[^]
 
Share this answer
 
v4
Comments
Maciej Los 15-Nov-19 17:31pm    
This answer is almost perfect...
Note: Aboce code returns '0022.2' instead of 22.2
:)
RickZeeland 16-Nov-19 2:07am    
Corrected :)
But there seem to be more issues like non-ASCII characters in the string ...
Maciej Los 16-Nov-19 2:13am    
5ed!
Member 14589606 18-Nov-19 17:04pm    
It says it exceeds the index.
I receive the response from the device and I save it in a string and then try to separate it or use your method

If i try writeLine on the string it prints each value in a separate line like this
0
0
6
5

Hence It states that the index exceeds
RickZeeland 19-Nov-19 1:29am    
See the updated solution.
Sorry for Java like code before

here

string s = "0065.0 0022.2 0050.0 0029.1 0100.0 0100.0 0000.0 0011.2 0000.0 -000.1 0000.0 -000.1 0000.0 -000.1 00000000000000000000000000000000";
string [] arr= s.Split(' ');
//Char out = arr[1];
while(arr[1].IndexOf('0') == 0){
arr[1] = arr[1].Substring(1);
}
Console.WriteLine(arr[1]);
 
Share this answer
 
string s ="0065.0 0022.2 0050.0 0029.1 0100.0 0100.0 0000.0 0011.2 0000.0 -000.1 0000.0 -000.1 0000.0 -000.1 00000000000000000000000000000000";
string [] arr= s.split(" ");
string out=arr[1];
while(out.indexOf("0") == 0){
out = out.substring(1);
}
Console.WriteLine(out);
 
Share this answer
 
v2
Comments
Richard MacCutchan 19-Nov-19 9:56am    
That is not C#.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900