Categories
JavaScript Answers

How to get value of a string after last slash in JavaScript?

Sometimes, we want to get value of a string after last slash in JavaScript.

In this article, we’ll look at how to get value of a string after last slash in JavaScript.

How to get value of a string after last slash in JavaScript?

To get value of a string after last slash in JavaScript, we can use the string lasrtIndexOf and substring methods.

For instance, we write

const str = "foo/bar/test.html";
const n = str.lastIndexOf("/");
const result = str.substring(n + 1);

to get the index of the last '/' in str with lastIndexOf. Then we call str.substring with n + 1 to return a substring from str from index n + 1 to the end of the str string.

Conclusion

To get value of a string after last slash in JavaScript, we can use the string lasrtIndexOf and substring methods.

Categories
JavaScript Answers

How to convert base64 string to ArrayBuffer with JavaScript?

Sometimes, we want to convert base64 string to ArrayBuffer with JavaScript.

In this article, we’ll look at how to convert base64 string to ArrayBuffer with JavaScript.

How to convert base64 string to ArrayBuffer with JavaScript?

To convert base64 string to ArrayBuffer with JavaScript, we can use the Uint8Array.from method.

For instance, we write

const b = Uint8Array.from(atob(base64String), (c) => c.charCodeAt(0));

to call Uint8Array.from with atob(base64String) and (c) => c.charCodeAt(0) to convert the base64String to an ArrayBuffer.

atob decodes the base64 string into an ASCII string.

charCodeAt returns the integer Unicode character code for the first character in each ASCII character string.

Conclusion

To convert base64 string to ArrayBuffer with JavaScript, we can use the Uint8Array.from method.

Categories
JavaScript Answers

How to convert string to title case with Lodash and JavaScript?

Sometimes, we want to convert string to title case with Lodash and JavaScript.

In this article, we’ll look at how to convert string to title case with Lodash and JavaScript.

How to convert string to title case with Lodash and JavaScript?

To convert string to title case with Lodash and JavaScript, we call the startCase function.

For instance, we write

const s = _.startCase(_.toLower(str));

to call startCase with a string to convert the _.toLower(str) lower case string to a title case string.

Conclusion

To convert string to title case with Lodash and JavaScript, we call the startCase function.

Categories
JavaScript Answers

How to check for multiple conditions for JavaScript .includes() method?

Sometimes, we want to check for multiple conditions for JavaScript .includes() method.

In this article, we’ll look at how to check for multiple conditions for JavaScript .includes() method.

How to check for multiple conditions for JavaScript .includes() method?

To check for multiple conditions for JavaScript .includes() method, we use the array some method.

For instance, we write

const str1 = "hi hello, how do you do?";
const conditions = ["hello", "hi", "howdy"];
const test1 = conditions.some((el) => str1.includes(el));

to call conditions.some with a callback to see if str1 has any one of 'hello', 'hi' and 'howdy' in the str1 string.

We call str1.includes to see if the el entry in conditions is in the string.

Conclusion

To check for multiple conditions for JavaScript .includes() method, we use the array some method.

Categories
JavaScript Answers

How to check if string contains characters and whitespace with JavaScript?

Sometimes, we want to check if string contains characters and whitespace with JavaScript.

In this article, we’ll look at how to check if string contains characters and whitespace with JavaScript.

How to check if string contains characters and whitespace with JavaScript?

To check if string contains characters and whitespace with JavaScript, we check if there’re non-whitespace characters in the string.

For instance, we write

if (/\S/.test(myString)) {
  // ...
}

to call /\S/.test with myString to check if myString has any non-whitespace characters.

Conclusion

To check if string contains characters and whitespace with JavaScript, we check if there’re non-whitespace characters in the string.