ASP - Inserting line break after 15 characters

Associate
Joined
12 Aug 2004
Posts
1,009
Location
Glasgow, Scotland
Hi there,

How can I insert a line break (<br />) after every 15 characters with ASP (classic)?

What i'm trying to do is output peoples email addresses within table cells but because an email address is all one word i can't figure out how make the string of text automatically go down to the next line.

Cheers for any help,

Steven.
 
Associate
Joined
25 Dec 2002
Posts
1,540
Location
South GLOUCESTER
You can use string functions to navigate your string and dice it up etc. Read here: http://www.w3schools.com/vbscript/vbscript_ref_functions.asp#string

Out of interest, where are these e-mail addresses being read from? Also why every 15 characters? You wouldn't want to use <br /> to separate them, just use the response.write to write new table rows.

like this: Response.Write("<tr><td>" & emladdy & "</td></tr>")

Of course you could make it all neat when output as html by putting in the tabs using: vbTab or breaks with: vbCrLf

You could use the above when looping through database records to output your e-mail addresses.

If you tell me more about what you trying to acheive, i should be able to knock something up for you if your having difficulty.
 
Associate
OP
Joined
12 Aug 2004
Posts
1,009
Location
Glasgow, Scotland
Hi mate, thanks for replying.

--------------------------------------------------
Name_| Email______________ | Level
--------------------------------------------------
Fred | [email protected] | 1
James | [email protected]___ | 2
Adam_ | [email protected]___ | 3

As you'll be able to see above i've got a table that shows a users name, email and level in a table with 3 rows. I'd like to stop the email addresses taking up a huge amount of horizontal space by forcing a line break after around 15 characters to keep every nice and neat :)

Cheers,

Steven.
 
Permabanned
Joined
18 Oct 2002
Posts
2,275
I'm slightly confused why you do this? Wouldn't it look worse if half the email address was on the next line?

Pretty much what Joilet said with an If statement: -

If Len(theemailaddress) > 15 Then.
 
Associate
Joined
25 Dec 2002
Posts
1,540
Location
South GLOUCESTER
Hi

I just made this bit of code to demonstrate what you are trying to achieve:

Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Character Test</title>
</head>

<body>
<%
str = "[email protected]"
strmax = 15

if len(str) > strmax then

	response.write("<p>" & left(str,strmax) & "<br />" & mid(str,(strmax + 1),len(str)) & "</p>")

else

	response.write("<p>" & str & "</p>")

end if
%>
</body>
</html>

If you copy this into an asp file and upload it to your web server, you will be able to see it in action.

i don't know where your data source is, but for the sake of the example i just specified a random string. You may have this fed from a database etc.

As you can see i have specified a max string length just under the random string. I chose to specify the max length so changes can be made to that variable and the changes will roll out in the if statement. First of all it checks whether the length of the string exceeds the limit set, if it does, it breaks it up. It takes the first block of characters up till the strmax, then concatenates <br /> to create the line break. Then it concatenates the remainder of the string by using the mid function to read the text starting after the max limit (hence the adding of 1) characters till the string length which is the end. If the string does not exceed the max set, then it just outputs it without the <br /> through the else statement.

I hope this gives you an idea of what to do. You could go all advanced and build it into a function to call up. You could do a lot of things...

Idealy if this is a normal html table and i needed to achieve what you're doing then id simply just specify the column width in px and let the browser break it automatically!

Hope that helps mate!
 
Associate
Joined
25 Dec 2002
Posts
1,540
Location
South GLOUCESTER
Just had a thought, what if the e-mail address was over 30 characters long, my code wouldn't work right!

For that to work, you would need to do some basic math and divide the string length by the max length and ROUND UP to the nearest whole number. Use that number and loop through the string breaking it down a line every 15 characters.

You would be best off creating a function to do that and just pass the e-mail variable through it as you take it from your database or whatever.

I would show you, but i'm knackered and need sleep! Hopefully you can use my example to work on doing what i've just said. Thats assuming you really want to use this method. Unless your doing this to practice messing with strings, the only thing i can think of is if your trying to output this into a plain txt file which im starting to think after seeying your diagram. reminds me of ASCII art and old newsletters! If this is what you're doing then i don't recommend html at all. Read about what i mentioned above about the different line break: vbCrLf
 
Associate
OP
Joined
12 Aug 2004
Posts
1,009
Location
Glasgow, Scotland
Hi again,

I've been mucking about with your example, and looking at W3 Schools and just can't get the 'mid' function to run :confused: Am i doing something wrong?

My code:

Code:
	'declare & initalise variables to execute sql statement
	Dim objSearchSections
	Set objSearchSections = objConn.execute("SELECT SectionID, SectionTitle, SectionText FROM tblSections WHERE SectionTitle LIKE '%"& searchQuery &"%' OR SectionText LIKE '%"& searchQuery &"%' ORDER BY SectionTitle ASC;")%>
	
	<h3 class="section">Content</h3>
	
	<%'let the user know if there is no page results
	If objSearchSections.EOF Then%>No content results<br /><br /><%End If
		
		Dim txt
		txt = "abcdefghijklmnopqrstuvxyz"
		
		'list all section title results
		Do Until objSearchSections.EOF
			Response.Write(objSearchSections("SectionTitle") & "<br />")
			
			If objSearchSections("SectionText") <> "" Then
				Response.Write(Mid(txt),1,5))
			End If
			objSearchSections.MoveNext
		Loop

	'close the recordset connections
	objSearchSections.Close
	Set objSearchSections = Nothing

Error message:

Error Type:
Microsoft VBScript compilation (0x800A0414)
Cannot use parentheses when calling a Sub
/test/SSN/Website/search.asp, line 53, column 28
Response.Write(Mid(txt),1,5))
---------------------------^

Cheers :)
 
Associate
OP
Joined
12 Aug 2004
Posts
1,009
Location
Glasgow, Scotland
joilet said:
Just had a thought, what if the e-mail address was over 30 characters long, my code wouldn't work right!

For that to work, you would need to do some basic math and divide the string length by the max length and ROUND UP to the nearest whole number. Use that number and loop through the string breaking it down a line every 15 characters.

You would be best off creating a function to do that and just pass the e-mail variable through it as you take it from your database or whatever.

I would show you, but i'm knackered and need sleep! Hopefully you can use my example to work on doing what i've just said. Thats assuming you really want to use this method. Unless your doing this to practice messing with strings, the only thing i can think of is if your trying to output this into a plain txt file which im starting to think after seeying your diagram. reminds me of ASCII art and old newsletters! If this is what you're doing then i don't recommend html at all. Read about what i mentioned above about the different line break: vbCrLf

Hi mate,

I've got your original function working, but is their any chance of a very quick example of what your talking about above? ^

I get the idea, but i'm just not entirely sure on how to implement it.
 
Back
Top Bottom