Print Page | Close Window

Get one word from a string

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=14645
Printed Date: 30 March 2026 at 9:36am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Get one word from a string
Posted By: bootcom
Subject: Get one word from a string
Date Posted: 11 April 2005 at 7:06pm

Hi,

I'm in the middle of creating my chat mod for the forums and have come across an issue. One of the commands I will be implementing on there looks as follows: /ban username banlength message_to_user , the user then clicks their send button and the script reads in "/ban" using the following code:
 

 
msg = request.form("strMessageInput")
 
' If the user enters the ban emote then do the ban stuff
If left(lcase(msg), 4) = "/ban" then
 
' If the user is not an administrator then show them an error
If NOT blnAdmin then
 
''''  Error stuff goes in here  '''''
 
Else
 
' This is the main bit where I want to disect the message
 
End If
 
' Put the rest of the checks in below
ElseIf left(lcase(msg), 5) = "/kick" then
End If
 
What I want to do from here is create 3 variables 1 to hold the user name of the user to ban and the next to hold the ban length and the final one to hold the message to the user.
 
What I would like is to ask the question to see if anyone can come up with an easier way to do it than I currently have it planned, as below.
 

' Take the emote out of the string
msg = replace(lcase(msg), "/ban", "")
 
' Trim the string to get rid of all spaces at either end if any exist
msg = trim(msg)
 
' Convert the remaining bit of the message into an array
msgArray = split(msg, " ")
 
' The first part we need to take out is the username to check
banUsername = msgArray(0)
 
' Get the length of the ban we are wishing to give the user
banLength = cInt(msgArray(1))
 
' Get the message to send the user when they are being kicked from the chat
banMsg = msgArray(2)
 
The only problems I have is that from within the 4 part string, 2 of these will be optional - the ban length and the message to the user. So when I run this code and I just enter /ban bootcom the script will spit it out because it is quite clearly no longer an array as there are no spaces to trim. How would I be able to get around this and would anyone have any better suggestions on how to make this work?



Replies:
Posted By: dj air
Date Posted: 11 April 2005 at 7:59pm
using the array is probbabby the best way to do so.. what you could do to over come a sub script error

is when submitted do a javascript that checks the lengh and message fields if they = ""

make then have a miscalanious value like //11\\

then when split its ok.. and after banlengh and blnMSG are set .. from the array chheck if they = //11\\ if so its a no message or legh value

//11\\ = a null value , that is what you are using it for.


Posted By: bootcom
Date Posted: 11 April 2005 at 8:30pm
Thanks for the advise dj air, Smile
 
I could do it like that but the there's so many commands that can be entered that I don't want to have to check each one with javascript when the form is submitted. I would much rather do it in ASP while I'm doing the other checks. I suppose I could put an extra line in there so when I do the:
 

msg = replace(lcase(msg), "/ban", "")
msg = trim(msg)
 
Then I could also do the following check:
 

if inStr(msg, " ") <> 0 then
' do the create array stuff here
else
' If there is no spaces this means that we just have the user name to deal with
end if
 
Just thought of that one, whats your opinion there?  Or would you think that the JS way is the correct way to proceed?


Posted By: dj air
Date Posted: 12 April 2005 at 5:34am
i had another think at 2 thismoring...


what you can do is after trim do

if Right(msg,4) = "/ban"

then its not got anything else

but if its not that.. there is something else within the string


Posted By: Lofty
Date Posted: 12 April 2005 at 8:22am
I think you'd be best off putting a set devider charater into the command.

split on the devider charater.
replace any /ban


run the array through a loop to count the variables and run each variable through the trim function.

Array = Split(form, "devider character")

counter = 0
For each value in Array
  counter = counter + 1
  If counter = 1 then Username = trim(value)
  If counter = 2 then Bantime = trim(value)
  If counter = 3 then banmessage = trim(value)
Next




Posted By: bootcom
Date Posted: 12 April 2005 at 10:45am
Thanks dj air and lofty for your suggestions. I have a better idea on what i'm going to do now. I think I was mainly looking to bounce ideas off you guys and see what you thought, and I was on the right track with what I initially thought, but your ideas will help me improve it.
 
Once again, thanks guys Thumbs Up
 
Much appreciatedClap



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net