434,807 Members | 1,492 Online
Need help? Post your question and get tips & solutions from a community of 434,807 IT Pros & Developers. It's quick & easy.

# Function needed - String Part(Source,Section)

 P: n/a OK folks here is what I need help with. Lets assume I have a text field that will contain AlphaNumeric data. There is no set pattern to the field such that any given character can be either alpha or numeric. I need a function that will return the requested 'part' of the contents of the field. A 'part' would be defined as consecutive Alpha or Numeric characters in the text field. Examples: Field Name = cFoo Field Value = "102ab3xz4" StringPart(cFoo,2) should return "ab" (as a string). StringPart(cFoo,5) should return 4 (as an integer). Field Value = "a0914zva33" StringPart(cFoo,1) should return "a" (as a string). StringPart(cFoo,2) should return "914" (as an integer). Thanks for whatever assistance you can provide. DB Aug 7 '06 #1
12 Replies

 P: n/a Field Value = "102ab3xz4" StringPart(cFoo,2) should return "ab" (as a string). Answer: Mid(cfoo,4,2) StringPart(cFoo,5) should return 4 (as an integer). Answer: Right(cfoo,1) Field Value = "a0914zva33" StringPart(cFoo,1) should return "a" (as a string). Answer: Left(Cfoo,1) StringPart(cFoo,2) should return "914" (as an integer). Answer: Mid(cfoo,3,3) Hope that helps! Aug 7 '06 #2

 P: n/a Your function will need to look at each character in the string, keeping track of the number of transitions between Alpha and Numeric. When it gets to the requested 'part' use the mid function to return the part of the string between the start and end of the transition. "Ima Loozer"

 P: n/a In article <11**********************@m79g2000cwm.googlegroups .com>, jl*******@hotmail.com says... Field Value = "102ab3xz4" StringPart(cFoo,2) should return "ab" (as a string). Answer: Mid(cfoo,4,2) StringPart(cFoo,5) should return 4 (as an integer). Answer: Right(cfoo,1) Field Value = "a0914zva33" StringPart(cFoo,1) should return "a" (as a string). Answer: Left(Cfoo,1) StringPart(cFoo,2) should return "914" (as an integer). Answer: Mid(cfoo,3,3) Hope that helps! Uh you missed 100% what I said. I will not know where the numerics and alphas change. I cannot hard code the Mid functions. Thanks for the try but please read the input first! Aug 7 '06 #4

 P: n/a In article , pa**@packairinc.com says... Your function will need to look at each character in the string, keeping track of the number of transitions between Alpha and Numeric. When it gets to the requested 'part' use the mid function to return the part of the string between the start and end of the transition. "Ima Loozer"

 P: n/a Ima Loozer wrote: OK folks here is what I need help with. Lets assume I have a text field that will contain AlphaNumeric data. There is no set pattern to the field such that any given character can be either alpha or numeric. I need a function that will return the requested 'part' of the contents of the field. A 'part' would be defined as consecutive Alpha or Numeric characters in the text field. Examples: Field Name = cFoo Field Value = "102ab3xz4" StringPart(cFoo,2) should return "ab" (as a string). StringPart(cFoo,5) should return 4 (as an integer). Field Value = "a0914zva33" StringPart(cFoo,1) should return "a" (as a string). StringPart(cFoo,2) should return "914" (as an integer). Thanks for whatever assistance you can provide. DB See if this works. It was an interesting exercise. You call the function with code similar to the code below. BTW, if you pass a string with the returned string count greater than the number of alpha/numeric strings, it returns "Unknown". Thus if the string is "102ab3xz4" and you passed 6, you'd get unknown Dim strResult As String Dim strToCheck As String Dim intStr As Integer strToCheck = "102ab3xz4" intStr = 3 strResult = CharStringValue(strToCheck,intStr) 'returns 3 Function CharStringValue(strVal As String, intNum As Integer) As String Dim strAlpha As String Dim strAlphaComp As String Dim intCnt As Integer Dim intFor As Integer Dim strChar As String Dim strHold As String strAlpha = IIf(IsNumeric(Left(strVal, 1)), "N", "A") strHold = Left(strVal, 1) intFor = 1 Do While True intFor = intFor + 1 strHold = strHold & Mid(strVal, intFor, 1) strAlphaComp = IIf(IsNumeric(Mid(strVal, intFor, 1)), "N", "A") If intFor < Len(strVal) Then If strAlpha

 P: n/a Ima Loozer wrote: In article <11**********************@m79g2000cwm.googlegroups .com>, jl*******@hotmail.com says... >>Field Value = "102ab3xz4"StringPart(cFoo,2) should return "ab" (as a string).Answer: Mid(cfoo,4,2)StringPart(cFoo,5) should return 4 (as an integer).Answer: Right(cfoo,1)Field Value = "a0914zva33"StringPart(cFoo,1) should return "a" (as a string).Answer: Left(Cfoo,1)StringPart(cFoo,2) should return "914" (as an integer).Answer: Mid(cfoo,3,3)Hope that helps! Uh you missed 100% what I said. I will not know where the numerics and alphas change. I cannot hard code the Mid functions. Thanks for the try but please read the input first! When asking for help, be nice. I gave you a solution for free that should work. We aren't being paid but we do try to help out. If you act nasty, we may simply think, this wiseguy should be able to carry his own water. Aug 7 '06 #7

 P: n/a ok, I will bite.... This is quite easy to write.... Public Function mToken(s As Variant, gNum As Integer) As Variant Dim cTokens As New Collection Dim intPtr As Integer Dim str As String Dim c As String Dim bolNum As Boolean Dim bolLastNum As Boolean If IsNull(s) Then Exit Function ' above test will allow this to be using in sql queries ' since null is often passed c = Mid(s, 1, 1) bolLastNum = c Like "[0-9]" For intPtr = 1 To Len(s) c = Mid(s, intPtr, 1) bolNum = c Like "[0-9]" If bolNum = bolLastNum Then str = str & c Else cTokens.Add str bolLastNum = bolNum str = c End If Next intPtr If str <"" Then cTokens.Add str End If mToken = cTokens(gNum) End Function -- Albert D. Kallal (Access MVP) Edmonton, Alberta Canada pl*****************@msn.com Aug 7 '06 #8

 P: n/a Ima Loozer wrote: OK folks here is what I need help with. Lets assume I have a text field that will contain AlphaNumeric data. There is no set pattern to the field such that any given character can be either alpha or numeric. I need a function that will return the requested 'part' of the contents of the field. A 'part' would be defined as consecutive Alpha or Numeric characters in the text field. Examples: Field Name = cFoo Field Value = "102ab3xz4" StringPart(cFoo,2) should return "ab" (as a string). StringPart(cFoo,5) should return 4 (as an integer). Field Value = "a0914zva33" StringPart(cFoo,1) should return "a" (as a string). StringPart(cFoo,2) should return "914" (as an integer). Thanks for whatever assistance you can provide. Function ReadTheInput(s As String) As String() Dim i As Integer Dim t As String Dim c As String * 1 Dim n As Boolean Dim delimit As Boolean For i = 1 To Len(s) c = Mid\$(s, i, 1) delimit = IsNumeric(c) = n If delimit Then t = t & "," n = Not n End If t = t & c Next ReadTheInput = Split(t, ",") End Function ''''''''''''''''''''''''''''' Sub test() Dim s() As String Dim i As Integer s = ReadTheInput("a0914zva33") For i = 0 To UBound(s) Debug.Print s(i) Next End Sub ''''''''''''''''''''''''''''' Aug 8 '06 #9

 P: n/a Albert D. Kallal wrote: ok, I will bite.... This is quite easy to write.... Public Function mToken(s As Variant, gNum As Integer) As Variant Dim cTokens As New Collection Dim intPtr As Integer Dim str As String Dim c As String Dim bolNum As Boolean Dim bolLastNum As Boolean If IsNull(s) Then Exit Function ' above test will allow this to be using in sql queries ' since null is often passed c = Mid(s, 1, 1) bolLastNum = c Like "[0-9]" For intPtr = 1 To Len(s) c = Mid(s, intPtr, 1) bolNum = c Like "[0-9]" If bolNum = bolLastNum Then str = str & c Else cTokens.Add str bolLastNum = bolNum str = c End If Next intPtr If str <"" Then cTokens.Add str End If mToken = cTokens(gNum) End Function Although my function works, your's is more elegant. I like it. You simply parse the whole string out into its components and grab the collection element. If the OP uses it, he may want to add a check to ensure gnum doesn't exceed the collection's element count or that gnum isn't 0. Aug 8 '06 #10

 P: n/a > Although my function works, your's is more elegant. I like it. You simply parse the whole string out into its components and grab the collection element. Interesting, but rkc has even a shorter version then mine posted!! (but, fails to write and include the code to return the given token - so, if I was marking this as a homework assignment, then your solution would likely get higher marks then the short, but un-finished rkc solution). This problem reads like a nice computer homework problem (I like it). (I think I keep this one for questions when hiring developers) If the OP uses it, he may want to add a check to ensure gnum doesn't exceed the collection's element count or that gnum isn't 0. Good advice. And, since we have 3 quite different answers, it shows there is many ways to skin a cat.... I think *often* the use of collections in code is over looked. I tend to prefer collections over that of arrays for "handy dandy" lists of *small* data in code. They are easier to setup, and generally less code. They are also a structure that is dynamic. Arrays can be dynamic with extra work, but they are a throw back to days of old. The only big downfall of using collections is that they are read only (in this case that is just fine..but, keep that in mind if you start to get all excited about using collections). -- Albert D. Kallal (Access MVP) Edmonton, Alberta Canada pl*****************@msn.com Aug 8 '06 #11

 P: n/a Albert D. Kallal wrote: >>Although my function works, your's is more elegant. I like it. Yousimply parse the whole string out into its components and grab thecollection element. Interesting, but rkc has even a shorter version then mine posted!! (but, fails to write and include the code to return the given token - so, if I was marking this as a homework assignment, then your solution would likely get higher marks then the short, but un-finished rkc solution). I saw the request as unfinished myself. The function I posted provides a way to retrieve a section of the target string, a way to know if the "section" even exists and a way to retrieve multiple "sections" without calling the function multiple times. Aug 8 '06 #12

 P: n/a I saw the request as unfinished myself. Exactly...and, I think anyone would quickly see that adding one line of code more to your example would add the ability to return a given array (group) number as a feature to your posted solution.... -- Albert D. Kallal (Access MVP) Edmonton, Alberta Canada pl*****************@msn.com Aug 9 '06 #13

### This discussion thread is closed

Replies have been disabled for this discussion.