CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2003
    Posts
    21

    Identification of characters in a text file

    Hi, I was just wondering, say i wanted read a file but i only wanted to read 1 word or line from it. for example

    whfduweihrfweihrfweihfsdhfsdhfsdjkfhsdkjhfkjwrew

    the word in red is the word that i wanted to be printed to the text box on screen, is this possible?... if so dont suppose anyone could yell me how to do it?...

    Thanks Nick

  2. #2
    Join Date
    Feb 2001
    Location
    PA
    Posts
    163
    This should get you going...

    Private Sub Command1_Click()

    Dim fs, f, ts
    Dim pos
    Dim result As String
    Dim MyString As String
    Dim s As String

    MyString = "weih"

    'Ensure MS scripting runtime is selected in References
    Set fs = CreateObject("scripting.filesystemobject")
    Set f = fs.GetFile("c:\test1.txt")
    Set ts = f.OpenAsTextStream(ForReading, 0)
    s = ts.ReadAll
    pos = InStr(1, s, MyString, 1)
    result = Mid$(s, pos, Len(MyString))

    MsgBox result

    Set ts = Nothing
    Set f = Nothing
    Set fs = Nothing

    End Sub

  3. #3
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    263
    if you want to check for words you can do this , once you ave the string at hand :
    Code:
    Private Sub Command1_Click()
    Dim s As String
    s = "whfduweihrfweihrfweihfsdhfsdhfsdjkfhsdkjhfkjwrew"
    
    If InStr(s, "weih") Then
        MsgBox "weih" ' /// weih is in the text
    End If
    
    MsgBox UBound(Split(s, "weih")) ' /// the amount of times weih is in there ( eg: 3 )
    
    End Sub
    string Signature = Censored;

  4. #4
    Join Date
    May 2003
    Posts
    21
    Okay thanks guys that helped me alot.

    now i need to spice it up

    say i wanted to select character 9 then 16 then 19 from a sentance and then put them together in a text box is this possible. for example

    whfduweiwprnsektinorkfjrnwqlkrjewkhfuj

    the letters in red are the letters i want to be selected.

    w = 9
    t = 16
    o = 19

    If anyone could help me that would be great. Thank you very much

    Nick

  5. #5
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487
    You can use the Mid$() function to copy from the string..
    Code:
    Private Function CollectChar(sString As String, ByVal sChars as String) As String
    Dim i&
    Dim sResult$
    
    sResult = ""
    
    For i = 1 to Len(sString)
       
      If Instr(sChars, Mid$(sString, i, 1)) <> 0 Then
        sResult = sResult & Mid$(sString, i, 1)
      End If
    
    Next i
    
    CollectChar = sResult
    
    End Function
    Busy

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

Featured