Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Code 1

Status
Not open for further replies.

kitnba

MIS
Jul 31, 2000
132
HK
How do I find the desire space position in the string ?<br><br>example :<br>&nbsp;string = '69% wool 30% NYLON 1% Other'<br><br>can I find out the first space start with the position 11 ? Then it will return position 13 to me.<br><br>I can use Rat(), but it only can return the Last Space position of whole string . I want to search start from position 11 or any position.<br><br><br>any suggestion?<br>Thanks
 
Here is a function that takes a string and breaks it apart and places the elements into an array buy size using a space as the key.&nbsp;&nbsp;It is not exactly want you want it to do but with about 5 minutes of work you can have it doing what you want.<br><FONT FACE=monospace><br>*/***************************************************************************<br>*/Program&nbsp;&nbsp;&nbsp;: function FILLARAY<br>*/System&nbsp;&nbsp;&nbsp;&nbsp;:<br>*/Purpose&nbsp;&nbsp;&nbsp;: breaks up a string into an array<br>*/Syntax&nbsp;&nbsp;&nbsp;&nbsp;: = fillaray(string,array,size)<br>*/Returns&nbsp;&nbsp;&nbsp;: Integer - lines - number of lines required to fill the array<br>*/Parameter : String&nbsp;&nbsp;- string - the char string or database filed to break up<br>*/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: String&nbsp;&nbsp;- array - the name of the array to use<br>*/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: Integer - size - the length of the array strings to be returned<br>*/Changes&nbsp;&nbsp;&nbsp;: The array containing the string pieces<br>*/Requires&nbsp;&nbsp;: THE ARRAY MUST BE DECLARED IN THE CALLING PROGRAM and passed by reference<br>*/Calls&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:<br>*/Version&nbsp;&nbsp;&nbsp;: 1.0<br>*/Dated&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: 22 Nov 1993<br>*/Written By: David W. Grewe<br>*/***************************************************************************<br>*& Array Function<br>*/***************************************************************************<br>*/ Record Of Change<br>*/<br>*/***************************************************************************<br>parameters pcStr , paArray , pnSize<br>*external array (paArray)&nbsp;&nbsp;&& before the days of reference passing<br>private pcStr, pnSize, paArray<br>pcStr = alltrim(pcStr) + &quot; &quot;<br>*<br>private lnCount, lnLen, lcStr, lnPlace<br>lnCount = 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&& number of array elements used<br>lnLen = len(pcStr)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&& length of string to be worked on<br>lcStr = &quot; &quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&& swap var to hold a piece of pcStr<br>lnPlace = 0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&& where a blank space is<br>*<br>if len(pcStr) &gt; pnSize<br>&nbsp;&nbsp;do while len(alltrim(pcStr)) &gt; 1<br>&nbsp;&nbsp;&nbsp;&nbsp;lnCount = lnCount + 1<br>&nbsp;&nbsp;&nbsp;&nbsp;dimension paArray(lnCount)<br>&nbsp;&nbsp;&nbsp;&nbsp;lcStr = substr(pcStr , 1 , pnSize)<br>&nbsp;&nbsp;&nbsp;&nbsp;lnPlace = rat(&quot; &quot; , lcStr)<br>&nbsp;&nbsp;&nbsp;&nbsp;&paArray(lnCount) = alltrim(substr(pcStr , 1 , lnPlace))<br>&nbsp;&nbsp;&nbsp;&nbsp;pcStr = substr(pcStr , lnPlace)<br>&nbsp;&nbsp;enddo<br>else<br>&nbsp;&nbsp;lnCount = 1<br>&nbsp;&nbsp;&paArray(1) = pcStr<br>endif<br>return lnCount<br></font><br><br> <p>David W. Grewe<br><a href=mailto:Dave@internationalbid.net>Dave@internationalbid.net</a><br><a href= > </a><br>
 
The following code will display the position of all &quot;spaces&quot; in your string in a wait window:<br><br>mPOS = &quot;&quot;<br>mString = &quot;Return the location of spaces&quot;<br>FOR I = 1 TO LEN(mString)<br>IF SUBSTR(mString, I,1) = CHR(32)<br>mPOS = mPOS + ALLTRIM(STR(I)) + &quot;, &quot;<br>ENDIF<br>NEXT I<br>WAIT &quot;Spaces found at &quot; + mPOS WINDOW<br><br>Modify the the IF/ENDIF to perform what ever task you want to do, or use the code as part of a procedure and pass it parameters.<br><br>CDavis
 
My $.02<br><br>(Based on the parameters you provided above)<br><br>lnStartPos=11<br>lcString='69% wool 30% NYLON 1% Other'<br><br>lcSubString=SUBSTR(lcString,lnStartPos)<br>lnFoundPos=AT(' ',lcSubString) && Returns 3 in this example<br><br>IF lnFoundPos &gt; 0<br>&nbsp;&nbsp;lnFoundPos=lnStartPos+lnFoundPos-1 && Returns 13<br>ELSE<br>&nbsp;&nbsp;Messagebox('Houston...We have a problem!',0,'')<br>ENDIF<br><br>You could easily build this into a re-usable UDF by accepting the string to search, the string to search for, and the beginning position of the search as parameters. <p>Jon Hawkins<br><a href=mailto: > </a><br><a href= > </a><br>Carpe Diem! - Seize the Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top