Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." NOT for ASP.NET 1.0, 1.1, or 2.0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 1st, 2004, 10:24 AM
Authorized User
 
Join Date: Jan 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default string splitting

ive a text string like this ..

John Markus K Philip

i need to split it into 3 parts and take it to 3 variables like:
var1: John
var2: Markus
var3: K Philip

if only i name is there it shud only be using var1 and the other 3 vars r left blank .. howto do this ?

 
Old February 1st, 2004, 09:41 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Dim arrFullName()
'split the full name into the array
arrFullName = split(strName," ")

'check how many elements are in the array
select case ubound(arrFullName)
 case 1
  'Code to handle one name entered
  var1 = arrFullName(0)
 case 2
  'code to handle two names entered
  var1 = arrFullName(0)
  var2 = arrFullName(1)
 case 3
  'code to handle three names entered
  var1 = arrFullName(0)
  var2 = arrFullName(1)
  var3 = arrFullName(2)
 case 4
  'code to handle Four names entered
  var1 = arrFullName(0)
  var2 = arrFullName(1)
  var3 = arrFullName(2) & " " & arrFullName(3)
end select

BUT:
This is a very about face way of dealing with a name, you should try to get entered in the fields for first name, second, etc and then join them when you need a full name. With the huge variety of name formats out there I'd hate to have to rely on this method.

EG
"John Markus K Philip" would be
John
Markus
K Philip

BUT
"John K Philip" would be
John
K
Philip

Different result just because the didn't wish to provide (or didn't have) a middle name.


======================================
They say, best men are moulded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old February 2nd, 2004, 02:06 AM
Authorized User
 
Join Date: Jan 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i get this error ..

Error Type:
Microsoft VBScript runtime (0x800A000D)
Type mismatch
/clinicasp/DBConvert.asp, line 49

the input thru strName is string only ...

 
Old February 2nd, 2004, 02:20 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Sorry,
My mistake.

Dim arrFullName
NOT
Dim arrFullName()

======================================
They say, best men are moulded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old February 2nd, 2004, 02:25 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Sorry, there are further mistakes, I really didn't think when doing this one.

The case statement should be from a base of 0 (zero)

so
 case 1
 case 2
  ...etc

should be case 0, case 1, ...etc.

Below is working code to illustrate clearly
Code:
<% @language = "vbscript" %>
<html>
<head>
    <title>Untitled</title>
</head>
<body>
<%
Dim arrFullName
dim strName
'split the full name into the array
strName = "John Markus K Philip"
arrFullName = split(strName," ")

'check how many elements are in the array
select case ubound(arrFullName)
 case 0
  'Code to handle one name entered
  var1 = arrFullName(0)
 case 1
  'code to handle two names entered
  var1 = arrFullName(0)
  var2 = arrFullName(1)
 case 2
  'code to handle three names entered
  var1 = arrFullName(0)
  var2 = arrFullName(1)
  var3 = arrFullName(2)
 case 3
  'code to handle Four names entered
  var1 = arrFullName(0)
  var2 = arrFullName(1)
  var3 = arrFullName(2) & " " & arrFullName(3)
end select

response.write("<BR>Var1 = " & var1)
response.write("<BR>Var1 = " & var2)
response.write("<BR>Var1 = " & var3)
%>
</body>
</html>
======================================
They say, best men are moulded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old February 2nd, 2004, 05:59 AM
Authorized User
 
Join Date: Jan 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanx it works perfectly now ..






Similar Threads
Thread Thread Starter Forum Replies Last Post
Splitting a text string ghall202 Access 4 February 14th, 2008 05:45 PM
evc++: splitting string into array using strtok bluebeta Visual C++ 2 May 3rd, 2006 01:49 AM
Splitting of string lily611 SQL Server 2000 3 March 19th, 2005 08:17 AM
Splitting Strings ryanpatrick Classic ASP Basics 2 March 9th, 2004 02:42 PM
splitting this string menesesg Classic ASP Basics 1 September 10th, 2003 12:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.