Newsgroup: it.comp.lang.visual-basic
Titolo: Newbie VB question

How to break a line like "Our   Beautful    World!" 
into words "Our", "Beautiful" and "World!"?.
Visual Basic.
Tim
 
-- 
Sent by 1  from  yahoo  piece from  com
This is a spam protected message. Please answer with reference header.
Posted via 
http://www.usenet-replayer.com/cgi/content/new

 Visual Basic.
Wayulllll... First, off, you want to know about the VB reference
Manual.  In the Visual Studio IDE, select "Contents" from the "Help"
menu.  Now, in the window that appears, expand the tree nodes for
"MSDN Visual Studio...", "Visual Basic Documentation"*, "Reference",
"Language Reference", and "Functions".
Now, if Microsoft had ever hired anyone over the age of 12 to write
their documentation, you would look under "String Functions". 
Unfortunately, rhere are some terminal morons in positions of
authority at Microsoft, so, in order to find the "Substring" function,
you have to already know its name (or, at least, its initial!)  Turns
out that in VB (perhaps in all flavors of BASIC, for all I know) the
substring function is named "Mid".  So, yer going to have to work yer
way through the alphabet, looking at ALL the function definitions, to
find the ones that will be useful to you.  And those are:
1) InStr(source, target) returns the position of *target* in *source*
2) Replace(source, old, new) returns a copy of *source*, where all
occurances of *old* have been replaced by *new*
3) Trim(source) returns a copy of *source* from which leading and
trailing spaces have been removed.
4) Left(source, numchars) returns a target string consisting of the
leftmost *numchars* characters of *source*
5) Right(source, numchars) returns a target string consisting of the
leftmost *numchars* characters of *source*
6) Len(source) returns the length, in characters, of *source*
Now, you write yer own reusable functions, the first of which gets rid
of any excess space characters.  You could also expand CleanString to
get rid of puntuation, quotation, and other marks.  The second
function returns all characters preceding the first space in the
source string, and then removes those characters and the space from
the string.  The final fragment of code shows how to use those two
functions.
Understand that I have just knocked these out in the Google
newsreader, not in VB, so I have not tested them and there may be some
typos or bugs; testing is left as an exercise...
Public Function CleanString(source as String) as String
    source = Trim(source)                   ' First, be sure there...
    While InStr(source, "  ") > 0           ' ...are no preceding,...
        source = replace(source, "  ", " ") ' ...trailing, or ...
      Wend                                  ' ...multiple spaces.
End Function
Public Function FirstWord(source as string) as string
      Dim sp_pos as Integer
    sp_pos = InStr(source, " ")             ' Get posit'n of 1st space
    If sp_pos <> 0 Then                     ' If there is a space,...
        FirstWord = Left(source, sp_pos-1 ) ' ...return 1st word, &...
        source = Right(source, Len(source - sp_pos)) ' ...remove it.
      Elseif Len(source) > 0                ' If there is no space,...
        FirstWord = source                  ' ...return source,...
        source = ""                         ' ...and clear source.
      Else                                  ' If source was empty,...
        FirstWord = ""                      ' ...return nothing.
      End If
End Function
      :
    Dim sentence as String, nextword as string
      :
    sentence = " Our   Beautful    World!"
    sentence = CleanString(sentence)
    While Len(sentence) > 0
        nextword = FirstWord(sentence)
        ' do something with nextword 
      Wend
      :
-Richard
* Ignore the fact that someone at Microsoft does not know that "BASIC"
is correctly typed in all caps because it is an acronym.  You don't
need to be literate to be a programmer, but at Microsoft you don't
even have to be literate to be a writer!  If Microsoft writers are not
required to know how to write, do you s'pose Microsoft programmers are
required to know how to program?  Jus' askin'.
======================================================================
                Richard Hopley, Winston-Salem, NC, USA 
rho...@earthlink.net
 
1-301-775-0471
      Nothing really matters except Boats, Sex, and Rock'n'Roll.
rho...@wfubmc.edu
 
1-336-713-5077
  OK, OK; computer programming for scientific research also matters.
======================================================================

       Wend
Here's the function I would provide
Function GetWords(ByVal Source As String) As String()
  Source = Trim$(Source)
  Do While Source Like "*  *"
    Source = Replace$(Source, "  ", " ")
  Loop
  GetWords = Split(Source, " ")
End Function
which would be used something like this (kicked off here via a
CommandButton Click event). Each word is loaded as an element of the
array returned by the function (placed in the IndividualWords array in
our calling routine)...
Private Sub Command1_Click()
  Dim X As Long
  Dim MyString As String
  Dim IndividualWords() As String
  '  Here is the actual code
  MyString = " Our   Beautiful    World!"
  IndividualWords = GetWords(MyString)
  '  Let's see if it worked
  For X = 0 To UBound(IndividualWords)
    Debug.Print IndividualWords(X)
  Next
End Sub
Rick - MVP

 Tim
Try this...
----------------
Dim I As Integer
Dim C As String
Dim MyString As String
Dim LastStart As Integer
LastStart = 1
MyString = "Our Beautiful World is at war"
For I = 1 To Len(MyString)
   C = Mid(MyString, (I), 1)
   If I = Len(MyString) Then
       MsgBox Mid(MyString, (LastStart), (I))
       Exit Sub
   End If
   If C = " " Then 'Space
       MsgBox Mid(MyString, (LastStart), (I - LastStart))
       LastStart = I + 1
   End If
Next I

Tim
OK Tim,  After seeing the responses to this post, I decided to let you
in on a real fine function.  It's called "Split".  You'll see the
Split function used in the code sample below, the rest of the code
(besides the part where the array is compressed) is fluff to show the
results.  I've taken out all indentation to minimize the wrap effect
in posting, but be aware some lines may be wrapped.
=========================code snip
Dim saResult() As String
Dim sSource As String
Dim lItem As Long
Dim aCount As Long
Dim ndx As Long
'the following line contains a number of double spaces
sSource = "This  Beautiful  World is unendingly  at  War"
'and here's the split function
'note: the 3rd param defaults to full return, but can be set
'   to minimize the number of returned substrings
saResult = Split(sSource, Chr(32), , vbTextCompare)
'show results
aCount = UBound(saResult)
Debug.Print "Number of substrings = " & CStr(aCount + 1)
If aCount > 0 Then
For ndx = 0 To aCount
Debug.Print "saResult(" & CStr(ndx) & ") Value = " & _
IIf(saResult(ndx) <> vbNullString, saResult(ndx), "<Null String>")
Next ndx
'compress array
ndx = -1
lItem = 0
Do While ndx < aCount
ndx = ndx + 1
If saResult(ndx) <> vbNullString Then
If ndx > lItem Then saResult(lItem) = saResult(ndx)
lItem = lItem + 1
End If
Loop
If ndx > lItem Then ReDim Preserve saResult(lItem - 1)
'show results again
aCount = UBound(saResult)
Debug.Print "Number of substrings = " & CStr(aCount + 1)
If aCount > 0 Then
For ndx = 0 To aCount
Debug.Print "saResult(" & CStr(ndx) & ") Value = " & _
IIf(saResult(ndx) <> vbNullString, saResult(ndx), "<Null String>")
Next ndx
End If
End If
============================end code snip
The following is the debug print copied from the Immediate window.
Notice that the results after the Split function contain a number of
null strings (due to multi spacing).  You can either loop through the
array looking for your own non-null values, or take the "compress"
path and have no null strings in the resulting array.
Number of substrings = 11
saResult(0) Value = This
saResult(1) Value = <Null String>
saResult(2) Value = Beautiful
saResult(3) Value = <Null String>
saResult(4) Value = World
saResult(5) Value = is
saResult(6) Value = unendingly
saResult(7) Value = <Null String>
saResult(8) Value = at
saResult(9) Value = <Null String>
saResult(10) Value = War
Number of substrings = 7
saResult(0) Value = This
saResult(1) Value = Beautiful
saResult(2) Value = World
saResult(3) Value = is
saResult(4) Value = unendingly
saResult(5) Value = at
saResult(6) Value = War
HTH Everyone!
Shell
-
http://drshell.home.mindspring.com/
Into computers since 1972.
-

...
