JavaScript反向字符串– 4种方法

In this tutorial I will talk about how you can reverse a string in JavaScript language.

在本教程中,我将讨论如何使用JavaScript语言反转字符串。

This is a very popular question i.e. asked in interviews. There are many ways in which we can reverse a string. Here I will discuss different ways in which we can achieve this.

这是一个非常受欢迎的问题,即在访谈中提出的问题。 我们可以通过多种方式来反转字符串。 在这里,我将讨论实现这一目标的不同方法。

JavaScript反向字符串 (JavaScript Reverse String)

使用Buitin功能 (Using Buitin Function)

To reverse a string using built-in methods we have to chain some methods like join, split and reverse.

要使用内置方法反转字符串,我们必须链接一些方法,如join,split和reverse。

For this first, we need to convert the string into an array by using split method then after this, we have to reverse that array then join the array using join method. You can check the below example.

首先,我们需要使用split方法将字符串转换为数组,然后,我们必须反转该数组,然后使用join方法将其加入数组。 您可以检查以下示例。

let Str="Hello";
let spltStr=Str.split("");
let reverseStr=spltStr.reverse();
let joinStr=reverseStr.join("");
console.log(joinStr);

You can do this in one line as given below.

您可以按照以下步骤在一行中完成此操作。

function reverseString(str) {
  return str.split("").reverse().join("");
}
 
reverseString("hello");

You can also create array instance by using Array.from inbuilt function.

您还可以使用Array.from内置函数创建数组实例。

First, use Array.from() to turn a string into an array, then Array.prototype.reverse() to reverse the array, and then Array.prototype.join() to make it back a string.

首先,使用Array.from()将字符串转换为数组,然后使用Array.prototype.reverse()反转数组,然后使用Array.prototype.join()使其返回字符串。

const reverseStr = str => Array.from(str).reverse().join('');
console.log(reverseStr('hello'));

使用循环 (Using Loops)

Loop is a process of repeating a block of code until a specific condition is met. So, we can reverse a string using for or while loop.

循环是重复代码块直到满足特定条件的过程。 因此,我们可以使用for或while循环来反转字符串。

For loop:

对于循环:

function reverseStr(str) {
	var newString = "";
	for (var i = str.length - 1; i >= 0; i--) {
	newString += str[i];
  }
  
  return newString;
}
 
console.log(reverseStr('helloWorld'))

While loop:

While循环:

var name = "Hello";
 
function reverseString(sampleStr) {
	if(!sampleStr.trim() || 'string' !== typeof sampleStr) {
		return;
	}
	
	let strLength=sampleStr.length, s='';
	while(strLength > 0) {
		strLength--;
		s+= sampleStr[strLength];
	}
	
	return s;
}
 
console.log(reverseString(name))

使用递归 (Using Recursion)

Recursion is a process in which function called itself either directly or indirectly.

递归是其中函数直接或间接调用自身的过程。

function reverseString(str) {
	if (str === "")
		return "";
	else
		return reverseString(str.substr(1)) + str.charAt(0);
}
 
console.log(reverseString('hello World'))

使用三元运算符 (Using Ternary Operator)

It is just a shorthand version of if conditional statement. In this operator, it requires at least 3 operands. You can increase the operators according to condition. Here I have used the substr() method and charAt method. They are inbuilt methods in javascript.

它只是if条件语句的简写形式。 在此运算符中,它至少需要3个操作数。 您可以根据情况增加运算符。 在这里,我使用了substr()方法和charAt方法。 它们是javascript中的内置方法。

Substr method returns the characters in a string beginning at the specified location through the specified number of characters.

Substr方法从指定位置开始通过指定数量的字符返回字符串中的字符。

“hello”.substr(1); // “ello”

“ hello” .substr(1); //“你好”

The charAt() method returns the specified character from a string.

charAt()方法从字符串中返回指定的字符。

“hello”.charAt(0); // “h”

“ hello” .charAt(0); // “H”

function reverseStr(str) {
	return (str === '') ? '' : reverseStr(str.substr(1)) + str.charAt(0);
}
 
console.log(reverseStr("hello"))

I hope you got the ideas of how to reverse a string in JavaScript. Comment down below if you have queries or know any other way to do this.

希望您对如何在JavaScript中反转字符串有想法。 如果您有疑问或其他任何实现方法,请在下面注释掉。

翻译自: https://www.thecrazyprogrammer.com/2019/10/javascript-reverse-string.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值