View Full Version : Normal string functions in JavaScript?
Seymour Clufley
09-09-2008, 08:21 AM
In PureBasic there's a function StringField for extracting items from a string split by a given delimiter. So:
list="apples|oranges|pears|melons|"
result=StringField(list,3,"|")
would return "pears".
How would you do that in JavaScript?
The end goal is to turn "http://images/im67547.jpg" into "im67547".
rangana
09-09-2008, 08:55 AM
<script type="text/javascript">
var url='http://images/im67547.jpg'; // Test string here
alert(url.split('/')[3]); // resultant string
</script>
Clueful
09-09-2008, 12:38 PM
In PureBasic there's a function StringField for extracting items from a string split by a given delimiter. So:
list="apples|oranges|pears|melons|"
result=StringField(list,3,"|")
would return "pears".
How would you do that in JavaScript?
The end goal is to turn "http://images/im67547.jpg" into "im67547".
Just add it to the language; using 0-based indexing of course:<script type='text/javascript'>
String.prototype.getField=function(index, delim)
{
return this.split( delim )[ index ] || "";
}
list="apples|oranges|pears|melons|"
alert( list.getField(2, '|') );
</script>
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.