|
-
Sep 28th, 2019, 07:25 PM
#1
Thread Starter
Lively Member
String BEFORE & After extraction of specifc data
I have the one of the following from data input:
TRANSPORTATION OF TEMPERATURE CONTROLLED PRODUCT @ -10°F (could be any temperature between -10 and 70°F)
- OR -
TRANSPORTATION OF TEMPERATURE CONTROLLED PRODUCT @ DRY
I want to extract ONLY -10 or DRY (whichever is present) and place it in another textbox. Notice DRY does not have the "°F" after it.
I suspect a CASE Select will be required but unsure how to pull out only the actual temperature w/o the °F
Currently I'm using code to take only the last 5 character out of the string as follows:
Code:
Dim strSetTemp = MyList.Items.Item(196).ToString
Dim strTemp As String = strSetTemp.Substring(strSetTemp.Length - 5)
Select Case strTemp
Case "DRY"
tbTemp.Text = "DRY"
Case Else
tbTemp.Text = strTemp
End Select
Needless to say, this is not working properly and I end up with either "@ DRY" or -10°F - as expected. I really need to be able to end up with either "DRY" or "-10" - as my end result. I figured a "between" statement would work but unsure how to properly code this.
Last edited by K3JAE; Sep 28th, 2019 at 07:28 PM.
-
Sep 28th, 2019, 09:51 PM
#2
New Member
Re: String BEFORE & After extraction of specifc data
Here's one way you can do it.
vbnet Code:
Dim data As String = "TRANSPORTATION OF TEMPERATURE CONTROLLED PRODUCT @ -10°F" Dim newdata As String = data.Split("@")(1).ToString.Replace("°F", "")
-
Sep 29th, 2019, 05:05 PM
#3
Thread Starter
Lively Member
Re: String BEFORE & After extraction of specifc data
 Originally Posted by netcoder1337
Here's one way you can do it.
vbnet Code:
Dim data As String = "TRANSPORTATION OF TEMPERATURE CONTROLLED PRODUCT @ -10°F" Dim newdata As String = data.Split("@")(1).ToString.Replace("°F", "")
This is a no go - still bringing in the °F and it is still placing a space in front of the "DRY" which throws off the program.
I tried adapting to the following:
Code:
Dim strSetTemp As String = MyList.Items.Item(196).ToString
Dim strTemp As String = strSetTemp.Split("@ ")(1).ToString.Replace("°F", "")
AND tried:
Code:
Dim strSetTemp As String = MyList.Items.Item(196).ToString
Dim strTemp As String = strSetTemp.Split("@")(2).ToString.Replace("°F", "")
This results in the space being placed in front of -10 (using the example above). and still puts the °f after the -10 which I am trying to eliminate.
I also need to account for the Space after the "@"sign that I do not want to import.
The 2nd piece I tried errored with an INDEX error.
To further Clarify:
if MyList.Items.Item(196).ToString would equal: "TRANSPORTATION OF TEMPERATURE CONTROLLED PRODUCT @ -10°F" - I want to extract ONLY "-10" - no spacing prior and NO °F
if MyList.Items.Item(196).ToString would equal: "TRANSPORTATION OF TEMPERATURE CONTROLLED PRODUCT @ DRY" - I want to extract ONLY "DRY" - no spacing prior.
Last edited by K3JAE; Sep 29th, 2019 at 05:09 PM.
-
Sep 29th, 2019, 05:29 PM
#4
Re: String BEFORE & After extraction of specifc data
netcoder1337's suggestion should do what you want (apart from the space), as long as the characters "°F" in the code are exactly the same characters as in your data ("°f" is different, and so are other symbols that look similar to ° ).
Removing the space should be as simple as using .Trim, eg:
Code:
Dim strTemp As String = strSetTemp.Split("@")(1).ToString.Replace("°F", "").Trim()
 Originally Posted by K3JAE
The 2nd piece I tried errored with an INDEX error.
That is to be expected, because Split() returns an array of strings based on a delimiter, and as there is only one delimiter in the data then the array will have 2 items in it (which have indexes of 0 and 1).
-
Sep 29th, 2019, 05:40 PM
#5
Thread Starter
Lively Member
Re: String BEFORE & After extraction of specifc data
OK, all understood. One last question please... In the string there is a "°" (degree symbol) and note sure the program is interpreting this properly. I say this as when it imports into the program it shows as a "?" in a black diamond in the textbox. Obviously it is not reading that in properly. Is there a way to force that to read it as ALT+0176 (°)? I think this may be the issue with the °F importing. I still want to drop it but if it is not seeing the degree symbol properly it will not drop it.
Last edited by K3JAE; Sep 29th, 2019 at 06:35 PM.
-
Sep 30th, 2019, 05:52 AM
#6
Re: String BEFORE & After extraction of specifc data
 Originally Posted by K3JAE
I say this as when it imports into the program it shows as a "?" in a black diamond in the textbox. Obviously it is not reading that in properly.
Where's the data being imported from, and what code are you using?
What you describe often happens when you have character data that has been encoded using one of the "Windows" single byte character encodings (often erroneously referred to as ANSI encoding), but then try to decode it back to text by using UTF-8 encoding. When encodings encounter characters they can't decode, they use a fallback strategy (best fit, replacement or exception fallbacks). UTF-8 replacement fallback substiutes character U+FFFD ; a question mark in a black triangle.
-
Sep 30th, 2019, 05:13 PM
#7
Thread Starter
Lively Member
Re: String BEFORE & After extraction of specifc data
 Originally Posted by Inferrd
Where's the data being imported from, and what code are you using?
What you describe often happens when you have character data that has been encoded using one of the "Windows" single byte character encodings (often erroneously referred to as ANSI encoding), but then try to decode it back to text by using UTF-8 encoding. When encodings encounter characters they can't decode, they use a fallback strategy (best fit, replacement or exception fallbacks). UTF-8 replacement fallback substiutes character U+FFFD ; a question mark in a black triangle.
The information is being written to a CSV file from a PDF. When I look at the CSV file the "°" (degree symbol) is properly displaying. That same CSV file is what is used to import the date into the program.
Last edited by K3JAE; Sep 30th, 2019 at 05:20 PM.
-
Sep 30th, 2019, 05:23 PM
#8
Re: String BEFORE & After extraction of specifc data
That answers the first part of the question, but not the second part: What code are you using to import it?
-
Sep 30th, 2019, 06:02 PM
#9
Thread Starter
Lively Member
Re: String BEFORE & After extraction of specifc data
 Originally Posted by si_the_geek
That answers the first part of the question, but not the second part: What code are you using to import it?
My project is written in VB.
-
Sep 30th, 2019, 06:38 PM
#10
Re: String BEFORE & After extraction of specifc data
VB is the language. If you show the lines of code you are using to import the CSV file, it would help in tracking down the cause of your problems with the wrong characters.
Most .NET methods that read text files have overloaded definitions that allow you to specify a Text Encoding that is used to decode the bytes in the file back into text. You need to specify the same encoding that was used to create the CSV file or you'll likely end up with the wrong characters.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|