Click to See Complete Forum and Search --> : Remove string after white space


Illufox
05-10-2005, 07:26 PM
I'm trying to remove the last name from a full name record to create a greeting that says "Welcome Tim" instead of "Welcome Tim Sutter". I was not able to find code for this on the internet. Is this possible?

David Harrison
05-10-2005, 08:07 PM
Say you have a string with their name in, you could simply split the string into an array using space for the delimeter:<%@ language="VBscript" %>
<% option explicit %>
<%

dim username
username="John Q. Public"

username = Split(username, " ", -1, 1)

response.write "Hi " & username(0)

%>

Illufox
05-11-2005, 07:46 PM
Excellent! This does the job very well! I had to adjust it a little like that:

Welcome
<%
Dim Greeting
Greeting = Session("FullName")
Greeting = Split(Greeting, " ", -1, 1)
Response.Write Greeting(0)
%>
, what would you like to do today?

Thanks a bunch!!!! :D

David Harrison
05-11-2005, 07:52 PM
Might be better to write it a bit more like this:<%
Dim Greeting
Greeting = Session("FullName")
Greeting = Split(Greeting, " ", -1, 1)
Response.Write Greeting(0)
%>
Welcome <%= Greeting(0) %>, what would you like to do today?That way you can keep the bulk of the ASP separate from the greeting.

Illufox
05-11-2005, 07:52 PM
Good idea but the one line needs to be removed in this case:

Response.Write Greeting(0) :)

David Harrison
05-11-2005, 07:54 PM
Happy to help. :)

wmif
05-11-2005, 08:52 PM
another way to do this

left(Session("FullName"),instr(Session("FullName")," "))

didnt test it but that should be pretty close. instead of using an array.