How to find the middle character of a string in javascript

// the most amazing

const getMiddle = s => s.substr(s.length - 1 >>> 1, (~s.length & 1) + 1);

// should return "dd"

console.log(getMiddle('middle'))

// >>> is an unsigned right shift bitwise operator. It's equivalent to division by 2, with truncation, as long as the length of the string does not exceed the size of an integer in Javascript.

// About the ~ operator, let's rather start with the expression n & 1. This will tell you whether an integer n is odd (it's similar to a logical and, but comparing all of the bits of two numbers). The expression returns 1 if an integer is odd. It returns 0 if an integer is even.

// If n & 1 is even, the expression returns 0.

// If n & 1 is odd, the expression returns 1.

// ~n & 1 inverts those two results, providing 0 if the length of the string is odd, and 1 if the length of the sting is even. The ~ operator inverts all of the bits in an integer, so 0 would become -1, 1 would become -2, and so on (the leading bit is always the sign).

// Then you add one, and you get 0+1 (1) characters if the length of the string is odd, or 1+1 (2) characters if the length of the string is even.

@author by jacobb

the link of the source is: https://codepen.io/jacobwarduk/pen/yJpAmK

This file contains 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

// Awesome
function getMiddle(s)
{
return s.substr(Math.ceil(s.length / 2 - 1), s.length % 2 === 0 ? 2 : 1);
}
// Usual
function GetMiddle(s)
{
var middle = Math.floor(s.length/2);
if (s.length % 2 == 0)
return s[middle-1] + s[middle];
else
return s[middle];
}
/*
Kata.getMiddle("test") should return "es"
Kata.getMiddle("testing") should return "t"
Kata.getMiddle("middle") should return "dd"
Kata.getMiddle("A") should return "A"
*/

Description

Javascript String Get word middle character

 P:Return the middle character of the word. 

If the word's length is odd, return the middle character.

If the word's length is even, return the middle 2 characters.

Examples:

getMiddle("test") should return "es"
getMiddle("testing") should return "t"
getMiddle("middle") should return "dd"
getMiddle("A") should return "A"

function getMiddle(s)
{
  const i = Math.floor(s.length / 2);
  let result = s[i];  
  if(s.length % 2 === 0 && i > 0) {
    result = s[i-1] + result;//from   w  w  w  . j  a  v  a  2s.com
  }
  return result;
}

const action = getMiddle("c");
console.log(action);

PreviousNext

Related

  • Javascript String Type check if a variable is a string
  • Javascript String Find bad word
  • Javascript String Get common characters between two String values
  • Javascript String is Isogram
  • Javascript String Validate Braces

How do you find the middle character in a string?

Approach:.
Get the string whose middle character is to be found..
Calculate the length of the given string..
Finding the middle index of the string..
Now, print the middle character of the string at index middle using function charAt() in Java..

How do you substring a string in Javascript?

The substring() method extracts characters, between two indices (positions), from a string, and returns the substring. The substring() method extracts characters from start to end (exclusive). The substring() method does not change the original string.

Does string contain substring Javascript?

Use the String. includes() method to check if a string contains a substring, e.g. myString. includes('substring') .