Created
August 2, 2016 23:23
-
-
Save jianminchen/45806800342101f3b4af7ac7c8ca8efa to your computer and use it in GitHub Desktop.
JavaScript - function split - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> | |
<head> | |
<title></title> | |
<style> | |
p { | |
color: blue; | |
margin: 8px; | |
} | |
b { | |
color: red; | |
} | |
</style> | |
</head> | |
<body> | |
<p><b>String </b> split</p> | |
<p>this is a JavaScript String drill - practice function one by one </p> | |
<p id="test">test..</p> | |
<script type="text/javascript"> | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split | |
"use strict"; | |
function splitString(stringToSplit, separator) { | |
var arrayOfStrings = stringToSplit.split(separator); | |
console.log('The original string is: "' + stringToSplit + '"'); | |
console.log('The separator is: "' + separator + '"'); | |
console.log('The array has ' + arrayOfStrings.length + ' elements: ' + arrayOfStrings.join(' / ')); | |
return arrayOfStrings.join(' / '); | |
} | |
var tempestString = 'Oh brave new world that has such people in it.'; | |
var monthString = 'Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec'; | |
var space = ' '; | |
var comma = ','; | |
var s1 = splitString(tempestString, space); | |
var s2 = splitString(tempestString); | |
var s3 = splitString(monthString, comma); | |
$('#test').html = s3; // include Jquery library | |
$("p:last").html(s3 + " - end of result. "); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment