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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

split word and number

Status
Not open for further replies.

thegame8311

Technical User
Jan 6, 2012
133
US
somehow I have case like this

1John

what's the best way to get

1 John
 
If all you want to do is insert a space, as you've shown here, see STUFF() in the help file.

I suspect you want more, so you'll likely find Left(), Substr() and Isdigit() useful.
 
Can you give us a more general rule for what you want to do.

Do you want to separate out the first character from the rest of the string? Or do you want to separate out the numeric part from the alphabetic part? And, in that case, will the numeric part always be at the start of the string, or can it be at the end? Or what?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
+1 = 3 people not knowing how to help here.

The problem isn't specified good with a single example. That single example is easily solved, eg with stuff(), but I don't think that'll help in general.

If I add your other posts, 1John could be something you read from a ie document.innertext, you can have manifold problems of merges, which are originally displayed seperate, but appear as a single word in innertext, and that problem may not only involve numbers and names, you could also get JohnSmith instead of John Smith.

The problem may go away, if you get into html a bit more. If you won't want to dive into knowledge about html code itself, then perhaps look at the other part of the oIE.Document.body, the document object model.

Anyway, you will need to add a bit of a fault tolerance when scripting off data from a browser intended for human end users, it's not intended for programmatic usage, for that area we have apis, webservices or web databases. Html is no vehicle for data, it's a document description language and browsers are rendering it for humand end users.

Bye, Olaf.
 
I found that left() is helpful for what I want to do

Thanks
 
I'll join the other 3 above ????

Your example shows only a single numeric digit prior to the Name

Do you need your code to handle an un-limited number of numeric digits?
12345John
to
12345 John

If so, using the LEFT() function might not be flexible enough.

Good Luck,
JRB-Bldr
 
Confusion reigns here. There are so many different possibilities that no single solution will work without more information to define EXACTLY what is needed

LEFT() and STUFF() MAY work fine for THIS example, but UNLESS ALL other instances follow exactly the same format, both LEFT() and STUFF() could fail at a critical moment.

mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
Confusion reigns here. There are so many different possibilities that no single solution will work without more information to define EXACTLY what is needed

The OP said he got what he wanted/needed.

 
Actually I saw a past posting from Mike Lewis that would be pretty flexible and a variation of it should work for this.

Code:
cString = <The Input String>

* --- Get Part Without Numerics ---
cChkNumerics = "0123456789"
cAlphaPart = CHRTRAN(cString, cChkNumerics, "")

* --- Get Part Without Alphas ---
cChkUpperAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
cChkLowerAlpha = LOWER(cChkUpperAlpha)
cNumericPart = CHRTRAN(cString, cChkUpperAlpha + cChkLowerAlpha, "")

* --- Recombine for Result ---
cNewString = cNumericPart + " " + cAlphaPart

NOTE - this would not work if there were Numeric Characters in the Right part of the desired result.

Regardless, I am glad that 1 John feels that their simplistic approach works for their simplistic example.

Good Luck,
JRB-Bldr
 
While dan has a point, that the OP is already satisfied, I would suggest a fault tolerance approach not manually mending some cases, but only detecting unusual words or more general data. Names with non alphabetic chars for example. List them and manually mend them.

isalpha and isdigit are more helpful here, and chrtran can also always be used to find out the existance of unallowed chars via checking Len(ChrTran(lcStringexamined,ChrTran(lcStringexamined,lcUnallowedChars,""),""))=0

Bye, Olaf.
 
if you want to parse out someting like " any number of numerics followed by any number of alpha characters " into two and then seperate them by a space , this is screaming out to use reguler expression , cannot understand why any of the experts have not pointed to this , code would be something like


loRegExp=Createobject('VBScript.RegExp')
loRegExp.Pattern='^[A-Z0-9._%+-]+@[A-Z0-9.-]' && just a typical example
loRegExp.Global=.T. && find multiple occurences
lcData='1john' or '123joe' etc
lccTokens=loRegExp.execute(lcData) && outputs all matches i.e. tokens to collection

For Each token In lccTokens
lcText=token.Value
** do whatever
Endfor
 
cipper,

while regexpr are strong, they're still of limited use, it's easier to use chrtran() to check for wanted or unwanted characters in general. John1 surely also is something not wanted, if 1John isn't. Don't you think?

Bye, Olaf.
 
olaf , dunno , the original question was so trivial , I assumed the actual data was a bit more complex e.g multiples like 1john123joe456mary etc , but the original questioner never clarified. Suppose the main prob is figuring out the regex pattern syntax , but I believe this is an excellent tool for doing that
 
this is screaming out to use reguler expression , cannot understand why any of the experts have not pointed to this

Possibly because it's simpler to use VFP's native functions.

Using a regexp would surely be overkill for this particular need, plus it introduces a level of sophistication which I suspect the original questioner will not feel comfortable with.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
I agree to Mike.

You have a point though, if this extends to further needs, eg there are regular expressions you can find and simply use, to verify validity for mail adresses etc. So overall they are useful for webcrawling and are worth mentioning.

Bye, Olaf.
 
Sorry for the confusion

1Joe -> Joe

is what I wanted, so I used STUFF()

I did try out the regexp idea but that did not give what I wanted

I also used a combination of Left() and Getwordnum()

Thanks for all the ideas :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top