Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Substring question

Status
Not open for further replies.
Nov 17, 2004
9
US
Hi,

I have a string which has some "Enter keys" (\n). I am looping through the string for each occurance of the "\n".
I need to get the substring before the occurance of "\n".

For eg:
string = "This is a test string. (enter key) Each string has some values (enter key) and some defined attributes (enter key)."
I need to get the length of the substring before the enter key and between the enter keys.

I have code like this below. I need to find the length of the substring between the enter keys and check for some validation. Any ideas? Thanks

if (window.event.keyCode == 13)
{
while (i < strField.length)
{
if (strField.charAt(i) == "\n")
{
Lines = Lines + 1;

}
i = i + 1;
}
if (Lines > 5)
{
//alert("tesT");
}
}
 
use indexOf to find the position of the line feed:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

var a = "abcdef\nghijklm";
alert(a.substr(0, a.indexOf("\n")));

</script>

<style type="text/css">

</style>

</head>
<body>

</body>
</html>

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
Thanks.
But I have to look for the character string from backwards too.
for eg.
var a = "abcdef\nghijklm\nopq\rst\";

I would like to get the substring like
1. "rst"
2. "nopq"
3. ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top