Andare a capo con gli array non troncando le parole

  • Creatore Discussione Creatore Discussione luca1317
  • Data di inizio Data di inizio

luca1317

Nuovo Utente
9 Nov 2008
12
0
0
Lo script seguente prende la stringa abcdefg e la suddivide in 3 righe: abc def g

Ora, se resto è uguale a 0? succede una cosa strana:

al posto di abcdefg (7caratteri) inseriamo abcdef (6 caratteri) in modo che il resto sia uguale a zero e le righe siano solo2 (array(0) e array (1).

Lo script in questo caso mi crea un terzo array(2) vuoto:
ARRAY(0):abc
ARRAY(1):def
ARRAY(2)

Come mai?
Codice:
dim testo,maxLen 
testo_abstract="abcdefg"


maxLen=3
lentotale=Len(testo_abstract)
righe=Int(lentotale/maxlen)
resto=lentotale-(maxLen*righe)


For x=0 To righe
If testo="" Then
testo=Mid(testo_abstract,x+1,maxLen) & "<br>"
Else
testo=testo & Mid(testo_abstract,(maxLen*x)+1,maxLen) & "<br>"
End If
Next


If resto > 0 Then
For y=0 To resto
testo=testo & Mid(testo_abstract,(maxLen*(x+y))+1,maxLen) & "<br>"
Next
End If


Dim MioArray 
MioArray = Split (testo, "<br>", -1, 1) 
testo=""


conta=righe




if conta>=0 then response.write("arr1: "&MioArray(0))
RESPONSE.WRITE("<BR>")

if conta>=1 then response.write("arr1: "&MioArray(1))
RESPONSE.WRITE("<BR>")
if conta>=2 then response.write("arr2: "&MioArray(2))
RESPONSE.WRITE("<BR>")
if conta>=3 then response.write("arr3: "&MioArray(3))
 
Ciao Luca,
prova modificando la parte finale con questo codice:
Codice:
conta=righe


response.Write conta&"<br />" ' riga aggiunta


	If (conta >= 0) Then
		If (MioArray(0) <> "") Then  
			response.write("arr1: "&MioArray(0))
		End If
	End If
	
		RESPONSE.WRITE("<BR>")

	If (conta >= 1) Then
		If (MioArray(1) <> "") Then 
			response.write("arr2: "&MioArray(1))
		End If
	End If
		RESPONSE.WRITE("<BR>")

	If (conta >= 2) Then
		If (MioArray(2) <> "") Then 
			response.write("arr3: "&MioArray(2))
		End If
	End If
		RESPONSE.WRITE("<BR>")

	If (conta >= 3) Then
		If (MioArray(3) <> "") Then 
			response.write("arr4: "&MioArray(3))
		End If
	End If

se vuoi evitare di scrivere le righe in alto, prova con un ciclo:

Codice:
conta=righe


response.Write conta&"<br />"

ciclo = 0
If conta > 0 Then
For xyx = 0 To conta

	If (conta >= ciclo) Then
		If (MioArray(ciclo) <> "") Then  
			response.write("arr"&ciclo&": "&MioArray(ciclo))
			response.Write("<br />")
		End If
	End If
ciclo = ciclo + 1

Next
End If

script completo con ciclo:
Codice:
<%

dim testo,maxLen 
testo_abstract="abcdefg"


maxLen=3
lentotale=Len(testo_abstract)
righe=Int(lentotale/maxlen)
resto=lentotale-(maxLen*righe)


For x=0 To righe
If testo="" Then
testo=Mid(testo_abstract,x+1,maxLen) & "<br>"
Else
testo=testo & Mid(testo_abstract,(maxLen*x)+1,maxLen) & "<br>"
End If
Next


If resto > 0 Then
For y=0 To resto
testo=testo & Mid(testo_abstract,(maxLen*(x+y))+1,maxLen) & "<br>"
Next
End If


Dim MioArray 
MioArray = Split (testo, "<br>", -1, 1) 
testo=""


conta=righe


response.Write conta&"<br />"

ciclo = 0
If conta > 0 Then
For xyx = 0 To conta

	If (conta >= ciclo) Then
		If (MioArray(ciclo) <> "") Then  
			response.write("arr"&ciclo&": "&MioArray(ciclo))
			response.Write("<br />")
		End If
	End If
ciclo = ciclo + 1

Next
End If

%>

Link esempio con ciclo: http://www.caprioli.info/FITNESS/script/array.asp

Paolo
 
Ultima modifica:

Discussioni simili