Results 1 to 12 of 12

Thread: Seperating Strings

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2015
    Posts
    53

    Exclamation Seperating Strings

    I have a string like this:
    "Temp state: NormalTemperature: 22.00 CBOARD: 22.00 C (1159 mV ADC: 36409)PA_THERM1: 21.00 C (1169 mV ADC: 36510)PA_THERM2: 21.00 C (1171 mV ADC: 36529)PMIC DIE: 28.14 C (603 mV ADC: 30867)XO: 28.03 C (836 mV ADC: 33142)XO_GPS: 28.03 C (836 mV ADC: 33142)"

    I want to print it into something like this:
    "Temp state: Normal"
    "Temperature: 22.00"
    "CBOARD: 22.00 C (1159 mV ADC: 36409)"
    "PA_THERM1: 21.00 C (1169 mV ADC 36510)"
    "PA_THERM2:21.00 C (1171 mV ADC: 36529)"
    "PMIC DIE: 28.14 C (603 mV ADC: 30867)"
    "XO: 28.03 C (836 mV ADC: 33142)"
    "XO_GPS: 28.03 C (836 mV ADC: 33142)"

    The thing is, the code needs to be flexible and universal to any types of string. For example if I also have a string like this:

    "Manufacturer: Sierra Wireless, IncorporatedModel: EM7455Revision: SWI9X30C_01.02.02.00 r3131 CARMD-EV-FRMWR2 2015/06/28 22:12:19IMEI: 001027009999999IMEI SV: 0FSN: LF524501170105+GCAP: +CGSM"

    I want to print it like this:
    "Manufacturer: Sierra Wireless, Incorporated"
    "Model: EM7455"
    "Revision: SWI9X30C_01.02.02.00 r3131 CARMD-EV-FRMWR2 2015/06/28 22:12:19"
    "IMEI: 001027009999999"
    "IMEI SV: 0"
    "FSN: LF524501170105"
    "+GCAP: +CGSM"

    Is there a possible way to approach writing this code? Any help is much appreciated. Thanks!

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Seperating Strings

    The best way to handle this data would be to know the "categories" up-front, are you able to do this? If so take a look at the following code:
    Code:
    Imports System
    Imports System.Text.RegularExpressions
    
    Public Module Module1
    
        Public Sub Main()
            Dim format1 As String = "Temp state: NormalTemperature: 22.00 CBOARD: 22.00 C (1159 mV ADC: 36409)PA_THERM1: 21.00 C (1169 mV ADC: 36510)PA_THERM2: 21.00 C (1171 mV ADC: 36529)PMIC DIE: 28.14 C (603 mV ADC: 30867)XO: 28.03 C (836 mV ADC: 33142)XO_GPS: 28.03 C (836 mV ADC: 33142)"
            Dim format2 As String = "Manufacturer: Sierra Wireless, IncorporatedModel: EM7455Revision: SWI9X30C_01.02.02.00 r3131 CARMD-EV-FRMWR2 2015/06/28 22:12:19IMEI: 001027009999999IMEI SV: 0FSN: LF524501170105+GCAP: +CGSM"
            Console.WriteLine(FormatData(format1, {"Temp state", "Temperature", "CBOARD", "PA_THERM1", "PA_THERM2", "PMIC DIE", "XO", "XO_GPS"}))
            Console.WriteLine()
            Console.WriteLine(FormatData(format2, {"Manufacturer", "Model", "Revision", "IMEI", "IMEI SV", "FSN", "GCAP"}))
        End Sub
    
        Private Function FormatData(ByVal input As String, ByVal categories() As String) As String
            Dim categoryEvaluator As MatchEvaluator = New MatchEvaluator(AddressOf ReplaceCategory)
            Return Regex.Replace(input, String.Join("|", categories), categoryEvaluator)
        End Function
    
        Private Function ReplaceCategory(ByVal m As Match) As String
            Return Environment.NewLine & m.Value
        End Function
    End Module
    Obviously you'll need to escape special characters like the plus sign in +GCAP, but again this is only if you know the categories up-front.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Seperating Strings

    The main problem I see there is that the last value and the next field or category are joined together. Unless you have a fixed table of fields, well defined, it would be very difficult to separate "NormalTemperature" into "Normal" and "Temperature" because according to your example, the Caps style is not consistent. One possibility is that there is a special character there that we are not able to see. Can you check it?
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2015
    Posts
    53

    Re: Seperating Strings

    At debug print, I get "Temp state: Normal Temperature: 22.00" I know that the space between Normal and Temperature isn't actually a space. How can I code it in such a way that it detects that it isn't actually a space and return the index between Normal and Temperature ?

  5. #5
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Seperating Strings

    It must be a special character, if that is the case the solution would be simple. Try obtaining the character code for that specific character. Show us the code where you have these strings.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2015
    Posts
    53

    Re: Seperating Strings

    How do I do that? I am not quite sure what the character is. I only know that it looks like a space.

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Seperating Strings

    At debug print, I get "Temp state: Normal Temperature: 22.00"
    That's debugging my code that I provided and it's not a NewLine?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2015
    Posts
    53

    Re: Seperating Strings

    Quote Originally Posted by dday9 View Post
    That's debugging my code that I provided and it's not a NewLine?
    Oh no, not for the code you wrote. I actually will not know the categories up front. I have several AT commands that I send to the USB to get a response, so I'm not sure how that code would work for me. Thanks a lot though

  9. #9
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Seperating Strings

    not the most elegant, but if you have that string in a variable, and it has exactly what you are posting. Put a line with:

    Code:
    MsgBox(Microsoft.VisualBasic.Asc(yourStringHere.Substring(18)))
    Note, is character 18, I had it wrong
    Last edited by kaliman79912; Jul 29th, 2015 at 03:58 PM.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  10. #10

    Thread Starter
    Member
    Join Date
    Jun 2015
    Posts
    53

    Re: Seperating Strings

    Solved! Thanks!

  11. #11
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    Re: Seperating Strings

    The issue that you have is that there are no consistencies:
    • There is a separator between categories and values, but none between the prior value and the next category.
    • The capital letters are not consistent, you could separate the prior value from the next category in cases such as NormalTemperature but not (1159 mV ADC: 36409)PA_THERM1
    • The values are not consistent in that one may be a digit and the next may be wrapped in parenthesis

    As far as I can tell, you simply cannot parse the text without hand-coding all of it for the specific format that's being parsed.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  12. #12
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Seperating Strings

    Could you post your solution? to help others with similar issues.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

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
  •  



Click Here to Expand Forum to Full Width