Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    78
    Thanks
    2
    Thanked 0 Times in 0 Posts

    split function at dot --- how?

    Hi all,

    I understand that the dot or period has special meaning in some code.

    So how exactly do you split at a dot?

    If I have the file name AnimatedMiniRabbit-2.gif and wish to use the split function to happen at the .gif and need the split at the dot, if you simply use the split(".") everything is removed.


    Martin.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts
    Try...
    split("\.")

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,762
    Thanks
    23
    Thanked 548 Times in 547 Posts
    No split will make an array.
    if string = "AnimatedMiniRabbit-2.gif";
    var parts = string.split(".");
    then parts[1] = gif
    and parts[0] = AnimatedMiniRabbit-2
    Evolution - The non-random survival of random variants.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,960
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Are you trying to check if the filename is an image file or not?

    PHP Code:
    var filename "AnimatedMiniRabbit-2.gif";
    if ((/.(
    gif|jpg|jpeg|tiff|png)$/i).test(filename)) {
        
    //image
    }
    else {
        
    //not an image


  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts
    Code:
    <!DOC HTML>
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    //<![CDATA[
    var f = 'AnimatedMiniRabbit-2.gif';
    var tarr = f.split("\.");   // array
    alert(tarr.join('\n'));
    //]]>
    </script>
    
    </head>
    <body>
    
    </body>
    </html>
    Last edited by jmrker; 09-25-2013 at 03:21 AM.

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    pick your poison:
    Code:
     /*each line true:*/ [
     "AnimatedMiniRabbit-2.gif".split(".")[1] =="gif" ,
     "AnimatedMiniRabbit-2.gif".split(".").pop() =="gif" ,
     "AnimatedMiniRabbit-2.gif".split(/(\.\w+)/)[1] == ".gif" ,
     "AnimatedMiniRabbit-2.gif".split(".")[0] == "AnimatedMiniRabbit-2" 
    ].every(Boolean) // true
    Last edited by rnd me; 09-25-2013 at 04:19 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #7
    New Coder
    Join Date
    Mar 2012
    Posts
    78
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi All,

    Thanks for the various suggestions, I never thought there was so much you could do with a dot.

    My intension was to split at the dot so as to end up with the file name that was before the dot. The idea was not just for images but any file name with any extension.


    Martin.

  • #8
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,274
    Thanks
    28
    Thanked 271 Times in 265 Posts
    Quote Originally Posted by SpidersWebHelp View Post
    I understand that the dot or period has special meaning in some code.
    It has special meaning in regular expressions where it represents all characters except line break characters. This meaning can be escaped with a backslash (\.).

    Quote Originally Posted by SpidersWebHelp View Post
    My intension was to split at the dot so as to end up with the file name that was before the dot. The idea was not just for images but any file name with any extension.
    This can be done with regular expressions or the split method. I prefer the former, but the latter may be more comprehensible if you're unfamiliar with regular expressions:

    Code:
    <!doctype html>
    <html lang="en">
    	<head>
    		<meta charset="utf-8">
    		<title>Demo</title>
    	</head>
    	<body>
    		<script>
    			var filename = null;
    			var filenameWithoutExtension = null;
    		
    			document.body.createParagraph = function (text) {
    				var paragraph = document.createElement("p");
    				paragraph.textContent = text;
    				document.body.appendChild(paragraph);
    			}
    			
    			// Regular Expression Method
    			filename = "abc123.extension";
    			filenameWithoutExtension = filename.replace(/\..+$/, "");
    			document.body.createParagraph("The regular expression method result for “abc123.extension” is " + filenameWithoutExtension + ".");
    			
    			filename = "abc123noextension";
    			filenameWithoutExtension = filename.replace(/\..+$/, "");
    			document.body.createParagraph("The regular expression method result for “abc123noextension” is " + filenameWithoutExtension + ".");
    			
    			// Split Method
    			filename = "abc123.extension";
    			filenameWithoutExtension = filename.split(".")[0];
    			document.body.createParagraph("The split method result for “abc123.extension” is " + filenameWithoutExtension + ".");
    			
    			filename = "abc123noextension";
    			filenameWithoutExtension = filename.split(".")[0];
    			document.body.createParagraph("The split method result for “abc123noextension” is " + filenameWithoutExtension + ".");
    		</script>
    	</body>
    </html>
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #9
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,466
    Thanks
    3
    Thanked 495 Times in 482 Posts
    Code:
    var src='http://www.vicsjavascripts.org.uk/StdImages/Egypt5.jpg';
    alert(src.slice(0,src.lastIndexOf('.')))
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •