473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Function needed - String Part(Source,Section)

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 2597
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
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" <no****@EyeDontWantSpam.foowrote in message
news:MP************************@newsgroups.comcast .net...
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 #3
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
In article <Wc******************************@athenet.net>,
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" <no****@EyeDontWantSpam.foowrote in message
news:MP************************@newsgroups.comcast .net...
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



yes I know what it needs to do. I was hoping someone would know how to
do it! :) It indeed needs to keep track of the number of changes from
Aplha to Numeric and grab the chunk of the string in question until the
A/N changes again.
Aug 7 '06 #5
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 <strAlphaComp Then
strAlpha = strAlphaComp
intCnt = intCnt + 1
If intCnt = intNum Then
strHold = Left(strHold, Len(strHold) - 1)
Exit Do
Else
strHold = Right(strHold, 1)
End If
End If
Else
intCnt = intCnt + 1
If strAlpha <strAlphaComp Then
If intCnt = intNum Then
strHold = Left(strHold, Len(strHold) - 1)
Else
intCnt = intCnt + 1
If intCnt = intNum Then
strHold = Right(strHold, 1)
Else
strHold = "Unknown"
End If
End If
Else
If intCnt <intNum Then strHold = "Unknown"
End If
Exit Do
End If
Loop
CharStringValue = strHold
End Function
Aug 7 '06 #6
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
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
rkc
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
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
>
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
rkc
Albert D. Kallal wrote:
>>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).
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Moosebumps | last post by:
I have a whole bunch of script files in a custom scripting "language" that were basically copied and pasted all over the place -- a huge mess, basically. I want to clean this up using Python --...
1
by: Richard A. DeVenezia | last post by:
Anyone aware of a function(s) that will take function and return a string containing tagged HTML that when document.written shows the function source stylized for syntax highlighting (i.e. comments...
13
by: sakitah | last post by:
Hello Everyone, Here's the problem (I'm using Visual c++ 6.0): I have a string: string1 = "This&is&life";
6
by: wfs | last post by:
Hi All, I've defined a few functions to DB2. I can see the entries in SYSIBM.SYSROUTINES - but where is the source text stored. I want to reverse engineer the definitions.
0
by: Marcus Jacobs | last post by:
Dear Group I am currently working on a c program and I am in need of source code that can determine the size in pixels (i.e. 600 x 600 pixels) for various electronic picture formats (i.e....
0
by: hudhuhandhu | last post by:
have got an error which says Input string was not in a correct format. as follows.. Description: An unhandled exception occurred during the execution of the current web request. Please review the...
11
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" //...
12
by: tfelb | last post by:
Hi all! I bought the book "Programming in C" by Stephen G Kochan. I miss 2 answers at his website. (removestr and the substr function) How can I implement these functions? It would be wonderful...
2
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi Is there any way to Read an INIFile from a string or Stream instead of a physical file ??? I want to read the INIFile into a string then store in a db but when I read the string from the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.