SPLIT : Traitement d'une chaîne de caractère
Application à la réalisation d'un moteur de recherche
Le mot clé split : Il
vous permet de récupérer des mots dans une chaîne
de caractères.
Split(Expression, Delimiter, Count, Compare)
The Split function separates a string into substrings and creates
a one-dimensional array where each substring is an element.
There is one mandatory argument.
Expression
The Expression argument is a string expression.
Code:
<% mystring = "How now brown cow?" %>
<% myarray = Split(mystring) %>
<% =myarray(0) %>
<% =myarray(1) %>
<% =myarray(2) %>
<% =myarray(3) %>
Output:
How
now
brown
cow?
There are three optional arguments.
Delimiter
The optional Delimiter argument specifies the characters (including
blanks) you wish to use to separate the expression. The default
is a single empty space, " ".
Code:
<% mystring = "How now brown cow?" %>
<% myarray = Split(mystring, "ow") %>
<% =myarray(0) %>
<% =myarray(1) %>
<% =myarray(2) %>
<% =myarray(3) %>
<% =myarray(4) %>
Output:
H
n
br
n c
?
Count
The optional Count argument specifies the number of elements to
return.
Code:
<% mystring = "How now brown cow?" %>
<% myarray = Split(mystring, " ", 2) %>
<% =myarray(0) %>
<% =myarray(1) %>
Output:
How
now brown cow?
Compare
The optional Compare argument must only use the constant or value
of the COMPARISON CONSTANTS.
CONSTANT VALUE DESCRIPTION
VBBinaryCompare 0 Binary comparison
VBTextCompare 1 Text Comparison
VBDataBaseCompare 2 Compare information inside database
In the example, by using VBTextCompare for the Compare argument,
the split occurs at the b and ignores the upper case difference
between the Delimiter argument "B".
Code:
<% mystring = "How now brown cow?" %>
<% myarray = Split(mystring, "B", 2, 1) %>
<% =myarray(0) %>
<% =myarray(1) %>
Output:
How now
rown cow?
Code:
<% mystring = "How now brown cow?" %>
<% myarray = Split(mystring, "B", 2, VBTextCompare)
%>
<% =myarray(0) %>
<% =myarray(1) %>
Output:
How now
rown cow?
|
Exemple
: Mettre une date au format américain
'la
date est remise dans le format anglais
mystring = date_Fi
myarray = Split(mystring, "/")
dateF
= myarray(1)&"/"&myarray(0)&"/"&myarray(2)
|
|
Exemple
:
Récuperer
les mots clés du champ description
if description <> "" then
myarray = Split(description)
NbTabIndice_description=UBound(myarray) Compter
le nombre de mot c'est dire la dimension du tableau
end
if
Construire
la requette
If
description <> "" Then
Mettre AND ( uniquement pour le premier
tour)
Dim m_description, p_description
p_description = 0
m_description=0
For p_description=0 to NbTabIndice_description
if myarray(p_description)<>"" and m_description=0
then
whereStr = whereStr & "AND("
m_description=m_description+1
end if
NEXT
Construire
la requette pour chaque mot clé
For
p_description=0 to NbTabIndice_description
whereStr = whereStr & "p.description LIKE '%"&
myarray(p_description)&"%' AND "
NEXT
if Mid (whereStr , Len(whereStr)-3,4) = "AND " then
whereStr = Mid (whereStr , 1 , Len(whereStr) -4)
end if
Construire
le dernière parenthese
Dim q_description
q_description=0
for p_description=0 to NbTabIndice_description
if myarray(p_description) <> "" and q_description=
0 then
whereStr = whereStr & ")"
q_description=q_description+1
end if
next
End If
Résultat
de la requette si les mots clé indiqué dans
le champ de formulaire sont informatique ASP java
AND(p.description
LIKE '%informatique%' AND p.description LIKE '%ASP%' AND p.description
LIKE '%java%' )
Une
variante :
Si
vous désirez que l'internaute ait le choix entre des
mots clés qui utilise AND ou OR ( et ; ou)
vous mettez dans votre formulaire un bouton radio avec AND
value=1 et OR value = 2
Voici
les modifs à faire au code précédent
For
p_titre=0 to NbTabIndice_titre
if operateur_logique = 1 then
whereStr = whereStr & "p.title LIKE '%"&
myarrayTitre(p_titre)&"%' AND "
else
whereStr = whereStr & "p.title LIKE '%"&
myarrayTitre(p_titre)&"%' OR "
end if
NEXT
if operateur_logique = 1 then
if Mid (whereStr , Len(whereStr)-3,4) =
"AND " then
whereStr = Mid (whereStr , 1 , Len(whereStr)
-4)
end if
else
if
Mid (whereStr , Len(whereStr)-2,3) = "OR " then
whereStr
= Mid (whereStr , 1 , Len(whereStr) -3)
end
if
end if
|
|