Link to home
Start Free TrialLog in
Avatar of toni2002
toni2002

asked on

how can i get a part of an unfixed variable length

Hi,

Dim strA as string
Dim strB as string

StrA = "Name   Toni2002"

This variable consists of 3 parts: 1) a word    2) space   3) a word  ,  these words have  not fixed length but the space which inbetween is fixed  in any value of strA

How can I put the 1st part (Name) or the 3rd part (toni2002)  alone in another variable
I mean  how can I let

strB = "Name"
OR
strB = "toni2002"

Thanx
SOLUTION
Avatar of Rog D
Rog D
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

i think
dim lstart as long
dim lend as long

lstart=instr(1,strA," ",vbtextcompare)
lend=instr(lstart,strA,"&",vbtextcompare

msgbox mid$(1,strA,lstart) & mid$(lend,strA)
Sorry some problem with my Browser I saw &...

sName = mid(stra,4,len(stra) - 4)  'This will give you Toni2002

sNmae = trim(sName)

This will get rid of spaces.

Rog
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Last example would not work if the actual name was "Joe Schmo 2002"

You would then have to look at the uBound(Array) then.

Rog
and  "Joe Schmo 2002"
 is NOT in agreement with the original question, which stated:
1) a word    2) space   3) a word  

not 1) a phrase 2) space 3) a word

If the original statement was imprecise, then ALL of the examples suggested so far will fail, for the same reason.

AW
Author,

I wasn't trying to start anything at all.  An all the suggestions would not fail if you looked closely.  Mine does not take the space into account in the Name "Joe Schmo 2002".

Sorry...

Your code was very good, I was just pointing out that sometimes peopole put spaces in names.

Rog
Am i crazy or main Q was edited??
The problem here is that you cannot simply search for the first space in the string.

Rog pointed out that you can have a name with spaces in it like "Joe Schmo 2002".

We don't know if this may also be true of the first part, the Field Name, which in this case happens to be "Name".  If one does, then any scheme searching for spaces will be futile.

I believe that Rog has had the best approach so far.  Although, we would have to start with a known list of Field Names to search for.

toni2002... Do you know all the possible values that the first word may contain, or whether they will ever contain spaces?

Idle_Mind
To me, in any case several spaces should be a problem since you are searching for the first one only and stop on it, so Split should fails due to that ,but Instr function would works OK.
It's a moot point since toni2002 has accepted an answer.

I was simply pointing out that no combination of Split() and/or Instr() will work reliably if either your Field Name or your Field Value has spaces in it.

Without knowing whether spaces are even allowed in the Field Names/Values, discussion is useless.

Having a list of known field names would allow you to determine at what point in the string the field value begins without question.

Enough said,

Idle_Mind
"It's a moot point since toni2002 has accepted an answer."

I don't mind if answer was accepted or not (even if it would be my comment or not)
I like to give some clarification to what i think is useful to round an idea.
But, again, as long as stated,
1) a word    2) space   3) a word  

i don't care how many spaces would be in 3) a word, instr function will do the job.
Problem only will rise if 1) a word is other than Name as provided (like First Name, in example)
Exactly.  With the given values, your code executes flawlessly.  I don't in any way mean to imply that it wouldn't.

The way the original question is worded though, it leads me to believe that there are other possible values for the "word" in position 1 and toni2002 was just asking the question to get a generic answer to the problem.

I like to develop algorithms with the most generic case in mind, and was tyring to get more information on the problem from toni2002 to no avail.

Idle_Mind