Using code to interrogate textual data | I'm a STRING. Ask me anything! | Coding tutorial
text
Introducing String Manipulation with JavaScript and PowerShell
Hey, everyone! We're diving into an exciting topic in this lessonβhow to build and interrogate strings using JavaScript and PowerShell. Let's jump straight into it, shall we?
To follow along, we recommend having both a PowerShell and a JavaScript environment ready for experimentation.
Introduction to Strings
Strings are sequences of characters that represent textual data within computer programs. We've previously looked at how strings serve various purposes like defining log messages or IDs for web elements. Today, our focus is on interacting with strings programmatically.
- Building Strings
- Interrogating Strings
Logging and String References
We can easily display messages in both environmentsβusing Write-Host
in PowerShell and console.log
in JavaScript. Let's also see how we can create string references in both languages.
// In JavaScript
let name = "DEEPLIZARD";
// In PowerShell
$name = "DEEPLIZARD";
We just created a reference called name that can be used to refer to the string DEEPLIZARD in both languages.
In PowerShell, references to strings are required to start with a $
symbol. In JavaScript, this is not required. However, it is a common practice to use the let
keyword to declare a reference to a string. This shows us how the same concept can be implemented in different ways in different languages.
Building Strings
What if we start with two separate strings, deep and lizard, and want to combine them? We can easily accomplish this through string concatenation.
// JavaScript
let fullString = "deep" + "lizard";
This gives us a new reference, fullString, that combines both strings into a single one: deeplizard.
Interrogating Strings
Once we have a string or a reference to a string, we can interrogate it to obtain various kinds of information. Here are some questions we might ask:
- What type of data are you?
- What character is present at index 6?
- What is the index of the letter 'Z'?
- Do you end with the word 'lizard'?
- Do you start with the word 'lizard'?
Each programming language has its own methods for these operations. For example, PowerShell uses contains
while JavaScript uses includes
to check if a string contains a particular substring.
// In JavaScript
let name = "deeplizard";
name.includes("lizard"); // True
// In PowerShell
$name = "deeplizard";
$name -contains "lizard"; # True
Both commands accomplish the same task but in their respective idiomatic ways.
If you're interested in diving deeper, you can always Google string methods along with the programming language of your choice. Got any questions? Feel free to drop them in the comments.
quiz
resources
updates
Committed by on