Counting the Number of Leading and Trailing Spaces

1. Pter Guest

## Counting the Number of Leading and Trailing Spaces

How can I do this? I tried using this code, but for some reason my page just freezes:<BR><BR>Function CountLeadingSpaces(center)<BR> dim count &#039;number of leading spaces<BR> count = 0<BR> Do while InStr(1, center, " ") &#060;&#062; 0 <BR> count = count + 1 <BR> Loop<BR> CountLeadingSpaces = count<BR>End Function<BR><BR>I used the InStrRev function to count the trailing spaces.<BR><BR>Somethign wrong with my logic and/or syntax? Can you suggest a better way of doing this?<BR><BR>Thanks.

2. sam
Senior Member
Join Date
Dec 1969
Posts
866

## RE: Counting the Number of Leading and Trailing Sp

Your are not omiting the already counted ones. So I guess your code will be in continuous loop. try this<BR><BR>count = 0<BR>intpos = 1<BR>Do while InStr(intpos, center, " ") &#060;&#062; 0 <BR>count = count + 1 <BR>intpos = intpos + 1<BR>Loop<BR>CountLeadingSpaces = count

3. Senior Member
Join Date
Dec 1969
Posts
1,505

## The question is why u want to do that ? <eop&

.

4. sam
Senior Member
Join Date
Dec 1969
Posts
866

## RE: The question is why u want to do that ? <

He may need that and he is doing so. Is that a problem for you?

5. Pter Guest

## RE: The question is why u want to do that ? <

My task is to print a statement containing accounting codes which will be separated by a dash. If the codes are say, <BR>(space)(space)123-345(space)-(space)88(space) then I should be able to print them like it is. It seems response.write ignores the leading and trailing spaces and doesn&#039;t print them.

6. Senior Member
Join Date
Dec 1969
Posts
1,505

## Cool it man r u jobless these days to be so hatefu

I just wanted to know and there is no harm learning and just because u gave the first answer ever in this post and may be all the posts and sites combined doesnt mean u have to be rude, no body gives u a sh**

7. Pter Guest

## RE: Counting the Number of Leading and Trailing Sp

Thanks for looking at my code and spotting my error. I did as you suggested but it seems that the code doesn&#039;t just count the leading spaces but the whole string. For example, (space)(space)MMM gives me a leading space count of 5. However, MMM gives me 0 which is correct.

8. sam
Senior Member
Join Date
Dec 1969
Posts
866

## RE: I am cooooooool now

take it is buddy!. Its your day and answer for that guy who desperately needs help(yours). <BR>BTW:-) I am not job less and I just having a bad day. I guess I have to go home now.

9. Senior Member
Join Date
Dec 1969
Posts
1,505

## Peace then!! :) <eop>

.

10. sam
Senior Member
Join Date
Dec 1969
Posts
866

## RE: Counting the Number of Leading Spaces

try this for leading space count. It gave me correct result.<BR>Function CountLeadingSpaces(center) <BR>dim count &#039;number of leading spaces <BR>count = 0 <BR>intPos = 1<BR>Do while instr(intpos,center," ") &#060;&#062; 0<BR>if mid(center,intpos,1) = " " then<BR>count = count + 1<BR>intPos = intPos + 1<BR>else<BR>exit Do <BR>end if <BR>Loop <BR>CountLeadingSpaces = count <BR><BR>End Function

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•