sabe.io sabe.io

How to Remove the First and Last Character from a String in Python

String manipulation is one of the most common yet basic operations to do in Python. Thankfully, Python makes it easy to work with strings and do things like remove characters from the beginning and end of a string. In this post, we'll look at examples of how to remove the first and last characters from a string in Python. Removing the First Character First, let's start off with our example string: str = "Hello...

sabe.io sabe.io

How to Check if a String Contains a Character in Java

Strings in Java are just a sequence of individual characters. This means that we can search strings for individual characters and detect if they are present. In this post, we'll look at examples of the two best ways to search for a character in a string in Java. Using the String contains() method The easiest way to check if a string contains another character is to use the contains() method on the String class. This method is called on...

sabe.io sabe.io

How to Convert a String to a Number in JavaScript

Numbers can be used in JavaScript in both strings and number form. In some cases, we'll need to able to convert a string to a number so that we can use it in mathematical operations. Thankfully, there are several built-in ways to do this in JavaScript. In this post, we'll go over the two best ways to convert a string to a number in JavaScript. Using the parseInt() method The most straight-forward way to convert a string to a number is...

sabe.io sabe.io

How to Convert a List to Comma-Separated String in Java

Lists in Java are a collection of items that can be held in a single variable. If this list is holding strings inside, you have the option to convert it to a comma-separated string. In this post, we'll learn the two best ways to turn a string list into a comma-separated string in Java. Using the join() method The most straight-forward way to convert a string list to a comma-separated string is to use the join() method that exists on...

sabe.io sabe.io

How to get the Width and Height of Browser's Viewport in JavaScript

The browser is the program that takes your HTML, CSS, and JavaScript, and renders it on the screen. With that being said, the actual size of the canvas is going to be different depending on the screen size, browser, device type, and other factors. For example, mobile phones will have a smaller viewport than desktop computers. Because of this, as front-end developers, we need a way to get the size of the viewport of the browser so we...

sabe.io sabe.io

How to Print an Array in PHP

When you're working with arrays in PHP, one of the most common things you'll need to do is print them out to the screen. This is useful for when you are trying to debug your code or simply to check the value of the elements in the array. In this post, we'll learn the best ways to print out an array in PHP. Using a foreach loop The most straightforward way to print out an array is to use a foreach loop. This involves simply iterating...

sabe.io sabe.io

How to display JavaScript values in HTML

When you are doing front-end development, you often need to display values that are in JavaScript variables on the page using HTML. This is useful when you are trying to debug your code or when you want to update the UI of your application. In this post, we'll learn all the different ways you can display values in JavaScript using HTML. Using the alert() function The easiest way to display a value in JavaScript is to use the alert()...

sabe.io sabe.io

Get Index of Object in Array by Property in JavaScript

When you have an array of objects, finding if an object exists in it is trickier than if the array was of primitives, like strings or numbers. This is because objects can take many different forms and have different data values in them. In this post, we'll learn how you find the index of an object in an array by property in JavaScript. Finding the Index of an Object in an Array by Property First let's start off with an example...

sabe.io sabe.io

How to calculate Exponents in Python

Exponentiation is the operation in which a number is multiplied by itself a certain number of times. Just like in the real-world, using exponents is useful in programming languages whenever you want to repeat multiplication. In this post, we'll look at how to calculate the exponent of a number in Python. Using the ** operator The easiest way to calculate the exponent of a number is to use the ** operator. This operator uses the...

sabe.io sabe.io

How to fix ReferenceError: Document is not defined in JavaScript

Depending on your environment and setup, you can run into an error when you try to reference the document object. Specifically, the error will look like this: ReferenceError: document is not defined In this post, we'll look at the most common reasons for this error and how to best avoid getting them. On Node The most common reason for this error is because you're using Node. That is to say, you are trying to access the document...

sabe.io sabe.io

How to get the Max Int in Python

Every programming language has a limit to how large an integer can be. This is because there is a set number of bits that can be used to represent a number, and so those bits only have so many different permutations. Typically, when this number is exceeded, the number is called an overflow, and will go negative if the number is signed. In Python 2, the maximum value for an integer is limited to 2 ** 63 - 1. That is to say the number 2,...