Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

60
Views
how to get first character of second string in javascript?

how to get first 2 character of string in javascript like this "Hello world " => get this "wo"? another example "Osama Mohamed" => M.

7 months ago · Juan Pablo Isaza
2 answers
Answer question

0

you can use string.split(' '), this function will split you string into an array, for example:

let s = "Hello World";
console.log(s.split(" ")) // will log ["Hello", "World"]

then you can get the second word using array[index] when index the index of your desired word in said array.

now using string charAt we can get the first letter of the word.

now to put everything together:

let s = "Hello World"
let s_splited = s.split(" ") // ["Hello", "World"]
let second_word = s_splited[1]
console.log(second_word.charAt(0))

7 months ago · Juan Pablo Isaza Report

0

string = "Hello world";
split_string = string.split(" ");
second_word = split_string[1]
first_char = second_word[0]
console.log(first_char)

OR

string = "Hello world";
console.log(string.split(" ")[1][0])
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.