Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created August 2, 2016 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/941a13f0283e8063cf8207715e19e4ac to your computer and use it in GitHub Desktop.
Save jianminchen/941a13f0283e8063cf8207715e19e4ac to your computer and use it in GitHub Desktop.
<!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> substring</p>
<p>this is a JavaScript String study practice </p>
<p id="test">test..</p>
<script type="text/javascript">
var anyString = 'Mozilla';
// Displays 'Moz'
console.log(anyString.substring(0, 3));
console.log(anyString.substring(3, 0));
// Displays 'lla'
console.log(anyString.substring(4, 7));
console.log(anyString.substring(7, 4));
// Displays 'Mozill'
console.log(anyString.substring(0, 6));
// Displays 'Mozilla'
console.log(anyString.substring(0, 7));
console.log(anyString.substring(0, 10));
///
// Displays 'illa' the last 4 characters
var anyString = 'Mozilla';
var anyString4 = anyString.substring(anyString.length - 4);
console.log(anyString4);
// Displays 'zilla' the last 5 characters
var anyString = 'Mozilla';
var anyString5 = anyString.substring(anyString.length - 5);
console.log(anyString5);
$('#test').html = anyString5; // include Jquery library
// Replaces oldS with newS in the string fullS
function replaceString(oldS, newS, fullS) {
for (var i = 0; i < fullS.length; ++i) {
if (fullS.substring(i, i + oldS.length) == oldS) {
fullS = fullS.substring(0, i) + newS + fullS.substring(i + oldS.length, fullS.length);
}
}
return fullS;
}
replaceString('World', 'Web', 'Brave New World');
$("p:last").html(anyString5 +" one more");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment