-
-
Save gonewayword/4d052366ea766040555683701624d291 to your computer and use it in GitHub Desktop.
String TheoryStrings vibrate and create matter// source http://jsbin.com/kodexoq
This file contains hidden or 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
//String Manipulation | |
//Strings are essentially characters (or a lack of characters) put inside of quotes. They can | |
//be viewed essentially as "display-only", if you will, in that they don't contain a value | |
//that is represented by what they look like. In other words, a string could be | |
//"This string is the number six", but it would not be. The string would be, simply, that | |
//series of characters in that order. They can be manipulated in many ways, outlined below. | |
//SOMEthing to keep in mind while looking at all of the below is that strings are immutable, which means | |
//they cannot be changed. Any of the manipulations you may perform to it--slice, turn into array, etc, these | |
//all simply produce a new string and do not change the original string. | |
//String length | |
//You can obtain the length of a string using the .length call: | |
var text = "Antelope Wedding" | |
console.log(text.length); | |
//Finding specific strings of characters inside a string | |
//If you want to find where a specific string of characters exists inside of a string | |
//you can use indexOf and it will return the numerical position at which that string of | |
//characters occurs. In this sense, string manipulatio methods like this are treating a | |
//string like an array, accessing each character numerically starting with 0, | |
//which is, as you can see, useful in some ways, | |
//but it can be dangerous. Check the next entry. | |
var str = "Tell me where 'badminton' even exists"; | |
console.log(str.indexOf("badminton")); | |
//search() for of it can take much more powerful search values (What does this mean? look up later) | |
//Extracting part of a string | |
//If you ever need to extract just part of a string there are three ways to do this. | |
//They're presented below along with their formatting. | |
//slice(start, end) | |
//substring(start, end) | |
//substr(start, length) | |
//slice | |
var str = "Euripedes, Eumendades, Eubuyades"; | |
var res = str.slice(9,18); | |
console.log(res); | |
//substring | |
//The only difference is that substring cannot accept negatives. | |
//substr | |
//The only difference here is that it extracts from a starting point and ends at a designated length. | |
//This can be useful if, for whatever reason, you know how many characters you want removed but you don't | |
//know the exact position you want to end at ahead of time. | |
var str = "Poland, Agribiz, Terra"; | |
var res = str.substr(7,6); | |
console.log(res); | |
//Replace | |
//Replacing within a string is an extremely useful ability. Any text you see in a video game, application, | |
//phone app, or any such thing often needs to be mutable or insterable--in other words, programs often need | |
//text or numbers that are subject to change based on input from user, moving to a different part of the program, | |
//or any number of similar reasons. This is easy with replace | |
roses = "Welcome to the jungle, it gets worse here every day!"; | |
var n = roses.replace("worse","better"); | |
console.log(n); | |
//The standard replacement will just replace the first match where the interpreter finds that string for | |
//the first time in the string. You can make it change all of the instances in a string by replacing the | |
//quotes with forward slashes and adding a g after the second. The g means global. | |
roses = "Welcome to the jungle, it gets worse here every day! I say again, it's gotten worse."; | |
var n = roses.replace(/worse/g,"better"); | |
console.log(n); | |
//uppercase and lowercase | |
//Changing a string definition to uppercase or lower case is extraordinarily useful | |
//for received prompted/asked for information from a user. If all you need to confirm | |
//or accurately portray or correctly program something is the correct strings in the | |
//correct order, in order for the program to keep moving smoothly, then these manipulations | |
//are your friend. You can change a received string to upper or lower case and then match | |
//that case to the input where the string is received and not worry about capitalization at all. | |
var meal = "lobster thermidor"; | |
console.log(meal.toUpperCase()); | |
//concatenation | |
//Concatenation is a fancy word for combining, and can be used the same way as the plus ( + ) operator. | |
var leia = "I love you."; | |
var han = " I know"; | |
var lukeSad = leia.concat(han); | |
console.log(lukeSad); | |
//charAt | |
//The charAt manipulation is a way of finding what character is at what position in a string. This could be | |
//useful in a great many ways, not least of all the aforementioned string slicing. If you don't know where | |
//a character is in a string, you can find it this way. | |
var wheresWaldo = "All over the world in his stripey shirt. But what does he do for fun? Does he love?"; | |
console.log(wheresWaldo.charAt(0)); | |
console.log(wheresWaldo.charAt(1)); | |
console.log(wheresWaldo.charAt(2)); | |
console.log(wheresWaldo.charAt(3)); | |
console.log(wheresWaldo.charAt(4)); | |
//Strings as Arrays | |
//It is potentially hazardous to treat a string like an array. AS shown above you can access a string like | |
//an array, but it's best to remember that it isn't actually one, and to instead convert it into an array | |
//if you are keen to treat it like one. | |
//To convert a string to an array quite simply use the following method: | |
var murica = "United we stand, divided we flail."; | |
murica.split(","); | |
console.log(murica); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment