How to strip some parts out of a string

Hi

I have an array of data (dimension = 19)

I knew that the last bytes contains float number (in this example it is 139.25)

After using Encoding.ASCII.GetString(data) i get a string contains special characters with 139.25

how can i strip these special characters and get only 139.25


===========================================
Please see this picture for more details

http://www.swa7.com/upload/uploading/ArrayOfData.jpg

===========================================

Is there another way to get the float number only out of the array data



Answer this question

How to strip some parts out of a string

  • sgroenen

    Based on your picture I'm concerned the data might not be in the ASCII format. Secondly, could you paste a Base64 encoding of your array "data". It might better help us understand what's happening. What do you expect to be in this string s

  • susan_wolber

     boban.s wrote:

    You can use LastIndexOf string method.

    In your case will be someting like str.Substring(str.LastIndexOf('\') + 2)

     

    Thanks man this tip solve me problem

     

    Here is the solution

     lblData.Text = args.Text.Substring(args.Text.LastIndexOf(@"\") + 14).ToString() ;


  • Voldemort

    Use the System.Convert.ToBase64String string. It'll convert any byte array to a Base64 encoded string, for future reference. Base64 strings are easier to send on web pages and forums and people can decode them to get the original data back.

  • Bakerboy60

    Anthony D. Green wrote:
    could you paste a Base64 encoding of your array "data". It might better help us understand what's happening. What do you expect to be in this string s

    Would you please instructe me on how to do it. I have tried but i did not figure it out how to use Base64 on array of data

    Thanks


  • Vikas H

    Sorry may be i am not explain the question nicely

    This data comes from a sever upon my request into two format:

    1. As an array with dynamic length (It depends on the data i request)

    2. As a string text containing the the value like these ( 55.62, 14252252, 152.36, ....etc) but with special characters

    Please see this http://www.swa7.com/upload/uploading/StringFormat.jpg

    to get clear picture about what i want to do.

    Thanks


  • Shobha69358

    You can use LastIndexOf string method.

    In your case will be someting like str.Substring(str.LastIndexOf('\') + 2)



  • icomeinpieces

    While I think that the format of the binary data would be useful in developing a more elegant solution here is the short term solution: You can use this regex to parse numbers from the end of the string.

       Sub Main()
          Dim numericRegex As New Regex("\d+(\.\d+) $", RegexOptions.ExplicitCapture Or RegexOptions.RightToLeft)

          For Each text As String In New String() {"3.1", "x\0x\0x\0x\0x\0x\0x\0x40.5", "x\0x\0x\0x\0x\0\b\0\a4463747"}
             Dim m As Match = numericRegex.Match(text)

             If m.Success Then Console.WriteLine(m.Value)
          Next

          Console.ReadLine()
       End Sub

    I put on explicit capture just because it's a bit more performant and right to left since we're dealing with the end of the string why not start there, no Hope that helps!



  • AndyC73

    If you know the exact size of the string you can do something like this;

    mystring.Substring(10, 5)

    My guess is your string is not always the same size so you can use

    using System.Text.RegularExpressions;
    ....
    Regex re = new Regex("[-9]", RegexOptions.Compiled);
    if (re.IsMatch(str)) {
    ...
    }



  • Oren Solomon

    Sorry if I am missing something but why are you using the Encoding.ASCII.GetString(data)

    what/where is "data" coming from



  • Roachy

    Anthony D. Green wrote:
    Based on your picture I'm concerned the data might not be in the ASCII format. Secondly, could you paste a Base64 encoding of your array "data". It might better help us understand what's happening. What do you expect to be in this string s

    I will try to use Base64 and give you a feedback

    Sorry may be i am not explain the question nicely

    This data comes from a sever upon my request into two format:

    1. As an array with dynamic length (It depends on the data i request)

    2. As a string text containing the the value like these ( 55.62, 14252252, 152.36, ....etc) but with special characters

    Please see this http://www.swa7.com/upload/uploading/StringFormat.jpg

    to get clear picture about what i want to do.

    Thanks


  • How to strip some parts out of a string