Results 1 to 19 of 19

Thread: String Slicing

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    11

    String Slicing

    ....
    Last edited by MGCL21; Mar 31st, 2011 at 09:25 AM.

  2. #2
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: String Slicing

    The explode and substr methods should get you what you want.

    edit:
    This is the PHP section.
    I see you're using VB.NET which we have another section for.
    Please don't create a new thread there though. I've already asked a moderator to move this thread.

    But in that case you should look at the String.Split() and String.Substring() methods
    Delete it. They just clutter threads anyway.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: String Slicing

    Moved To VB.NET

    Thanks for the report BigB!

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: String Slicing

    That's going to be a bit awkward to work with. I would split on : as a first step. This will result in an array of strings, but it will also split it further than you really want, so there would be another step. For one thing, the first element in the array would start with <, which you don't want, so use Substring to get rid of that (or ignore the first element, since it is just a label). Similarly, the last element in the array ends with >, which you will want to get rid of.

    The next thing to note is that the time is array elements 1,2, and 3, while the date is array element 5. Lat will be element 7 and Long will be element 9 and so forth.
    My usual boring signature: Nothing

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: String Slicing

    Please define how you would want to split the text.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    11

    Re: String Slicing

    Quote Originally Posted by stanav View Post
    Please define how you would want to split the text.
    I would like the text split up into seperate cells into an excel spreadsheet.

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: String Slicing

    Based on the sample text you provided, it looks like the lines have fixed length and so do the fields. The easiest way to parse it is to use String.Substring function.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    11

    Re: String Slicing

    Quote Originally Posted by stanav View Post
    Based on the sample text you provided, it looks like the lines have fixed length and so do the fields. The easiest way to parse it is to use String.Substring function.
    Is it still possible if the text length is variable?

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: String Slicing

    See http://msdn.microsoft.com/en-us/library/zezabash.aspx. It has an example for files with text fields.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    11

    Re: String Slicing

    ....
    Last edited by MGCL21; Mar 31st, 2011 at 08:27 AM.

  11. #11
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: String Slicing

    Or regex using this string:

    Code:
    <time:(.+):date:(.+):lat:(.+):long:(.+):wind:(.+):temp:(.+):recipitation:(.+):visibility:(.+)>

  12. #12

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    11

    Re: String Slicing

    Quote Originally Posted by Grimfort View Post
    Or regex using this string:

    Code:
    <time:(.+):date:(.+):lat:(.+):long:(.+):wind:(.+):temp:(.+):recipitation:(.+):visibility:(.+)>
    what do you mean by regex? if i copy this code into my current code will it work?

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: String Slicing

    Not directly. Regex is a means of matching strings by applying a pattern. That pattern is a derivative of a language in itself, and results in bizarre strings like the one Grimfort gave you (though that one is relatively mild as Regex patterns go). Look up Regex in MSDN for an example of how to work with it. You might also find this site to be a useful reference as to how to put together Regex strings:

    http://www.regular-expressions.info/tutorial.html
    My usual boring signature: Nothing

  14. #14
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: String Slicing

    vb.net Code:
    1. '' put in imports section
    2. Imports Microsoft.VisualBasic.FileIO
    3.  
    4.  
    5. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.     Dim myData As New List(Of String())
    7.     Dim parser As New TextFieldParser("c:\temp\test.txt")
    8.     parser.Delimiters = New String() {"<time:", ":date:", ":lat:", ":long:", ":wind:", ":temp:", ":precipitation:", ":visibility:", ">"}
    9.     While Not parser.EndOfData
    10.         myData.Add(parser.ReadFields)
    11.     End While
    12.     parser.Close()
    13.     parser = Nothing
    14.  
    15.     '' lets see what we have in our list now.
    16.     For Each row As String() In myData
    17.         Debug.WriteLine(Join(row, vbTab))
    18.     Next
    19. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  15. #15

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    11

    Re: String Slicing

    thanks i'll try out this code

  16. #16

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    11

    Re: String Slicing

    .....
    Last edited by MGCL21; Mar 31st, 2011 at 08:27 AM.

  17. #17
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: String Slicing

    Is your textbox multiline?

    Try this:
    vb.net Code:
    1. Private Sub Slice_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Slice_btn.Click
    2.     Dim parser As New FileIO.TextFieldParser("D:\CT Year 2\Applications and VB\Assignments\sample2044ass2.txt")
    3.     parser.Delimiters = New String() {"<time:", ":date:", ":lat:", ":long:", ":wind:", ":temp:", "recipitation:", ":visibility:", ">"}
    4.     TextBox1.Text = Join(parser.Delimiters, vbTab) & vbCrLf
    5.     While Not parser.EndOfData
    6.         TextBox1.AppendText(Join(parser.ReadFields, vbTab) & vbCrLf)
    7.     End While
    8.     parser.Close()
    9.     parser = Nothing
    10. End Sub
    modify according to your needs.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  18. #18

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    11

    Re: String Slicing

    Yea both of my textboxes are multi lines

  19. #19

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    11

    Re: String Slicing

    ....
    Last edited by MGCL21; Mar 31st, 2011 at 08:27 AM.

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