We help IT Professionals succeed at work.

split? left? right?

on
i got rs("x") with lots of numbers divided with comma(,)
*ends with comma too
3434,4343,5456,676556565,65,

how i can print them with space between them?
like this:(and to kill the last comma)
3434 ,4343 ,5456 ,676556565 ,65 ,
Comment
Watch Question

View Solutions Only

iSeries Engineer
Commented:
dim mArray
mArray =  Split(rs("x"), ",")

for i=0 to uBound(mArray)
response.write mArray(i) & " "
next

Commented:
Give this a shot.
``````<%
Dim strValues, arrValues, iNumberCount, iIndex, sValue

'// If the entire string is NULL, convert it to an empty string.
strValues = Trim(rs("x") & "")

'// Convert the string into an array of numbers.
arrValues = Split(strValues, ",")

iNumberCount = 0
For iIndex = 0 to UBound(arrValues)
'// Get an individual item in the array, and shave off extra spaces on either side.
sValue = Trim(arrValues(iIndex))

'// If the value is a number, go ahead and write it out.
If IsNumeric(sValue) Then
iNumberCount = iNumberCount + 1
If (iNumberCount > 1) Then Call Response.Write(", ")
Call Response.Write(sValue)
End If
Next
%>
``````

Commented:
deathtospam ,i understand samic400 solution...
i am kind of lost with yours:

strValues = Trim(rs("x") & "") ?
sValue = Trim(arrValues(iIndex)) ?
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008
Commented:
Comma to the left: 3434, 4343, 5456, 676556565, 65
Response.Write Replace( Left( rs("x"),Len(rs("x"))-1 ), ",", ", ")

Comma to the right: 3434 ,4343 ,5456 ,676556565 ,65
Response.Write Replace( Left( rs("x"),Len(rs("x"))-1 ), ",", " ,")

Commented:
well that's what i'm talking about...replace function is great!:)
hielo, how are you?:)
i understand deathtospam's code now but,
what do you think about deathtospam's code?
i mean what is better-to replace from the begining or processing the rs("x") as deathtospam did...
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
>>...replace function is great!:)
It sure is

>>hielo, how are you?:)
Super

>>what is better-to replace from the begining or processing the rs("x") as deathtospam did
I find the "Replace" option easier to understand/maintain. If the rs("x") values is "consistent" , processing the other way is kind of an overkill, espcially since it saves the value of Split every time it is called. The way I see it, don't consume extra memory resources if it is not needed.

Commented:
Replace( Left( rs("x"),Len(rs("x"))-1 ), ",", " ,")
runs on the all rs("x")
or i still need to split and replace every part?
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008

Commented:
>>or i still need to split and replace every part?
No Splitting necessary. I am assuming rs("x") is some db field. As an example, you would use it as follows:

'assuming you queried the db and the result is in rs
while Not rs.eof
Response.Write Replace( Left( rs("x"),Len(rs("x"))-1 ), ",", " ,")
rs.moveNext
wend

Commented:
i just tried that...i got plain page...
my rs("x") was 232,343445,565656,35324,2432,
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008
Commented:
>>i got plain page
You must be doing something else wrong. I tested that input and it worked fine for me.
``````<html>
<body>
<h1>Test throught IE</h1>
<script type="text/vbscript">
Dim rs,result
rs="3434,4343,5456,676556565,65,"
result = Replace( Left( rs,Len(rs)-1 ), ",", ", ")
document.write result
</script>
</body>
</html>
``````

Commented:
aa ok...my mistake...

thank you!