当一个字符串被传递给一个带有返回语句的函数时,你如何在JavaScript中反转它,而不使用内置函数(.reverse(), . charat()等)?
当前回答
你可以尝试这样做。我相信还有重构的空间。我无法使用分裂函数。也许有人知道不分裂的方法。
代码设置,可以把它放在你的。js库
使用它的代码(有客户端代码,只是因为它在浏览器中测试过):
var sentence = "My Stack is Overflowing."
document.write(sentence.reverseLetters() + '<br />');
document.write(sentence.reverseWords() + '<br />');
代码片段:
String.prototype.aggregate = function(vals, aggregateFunction) { var temp = ''; for (var i = vals.length - 1; i >= 0; i--) { temp = aggregateFunction(vals[i], temp); } return temp; } String.prototype.reverseLetters = function() { return this.aggregate(this.split(''), function(current, word) { return word + current; }) } String.prototype.reverseWords = function() { return this.aggregate(this.split(' '), function(current, word) { return word + ' ' + current; }) } var sentence = "My Stack is Overflowing." document.write(sentence.reverseLetters() + '<br />'); document.write(sentence.reverseWords() + '<br />');
其他回答
整个“将字符串反向”是一个过时的C程序员面试问题,被他们面试的人(可能是为了报复?)会问。不幸的是,它的“到位”部分不再起作用,因为几乎所有托管语言(JS, c#等)中的字符串都使用不可变字符串,因此无法在不分配任何新内存的情况下移动字符串。
虽然上面的解决方案确实反转了字符串,但它们在不分配更多内存的情况下不会这样做,因此不满足条件。您需要直接访问分配的字符串,并能够操作其原始内存位置,以便将其反向。
就我个人而言,我真的很讨厌这类面试问题,但遗憾的是,我相信在未来几年里我们还会继续看到它们。
我知道这是一个已经被很好地回答过的老问题,但为了自娱自乐,我写了下面的反向函数,并想把它分享给其他人,以防它对其他人有用。它处理代理对和组合标记:
function StringReverse (str)
{
var charArray = [];
for (var i = 0; i < str.length; i++)
{
if (i+1 < str.length)
{
var value = str.charCodeAt(i);
var nextValue = str.charCodeAt(i+1);
if ( ( value >= 0xD800 && value <= 0xDBFF
&& (nextValue & 0xFC00) == 0xDC00) // Surrogate pair)
|| (nextValue >= 0x0300 && nextValue <= 0x036F)) // Combining marks
{
charArray.unshift(str.substring(i, i+2));
i++; // Skip the other half
continue;
}
}
// Otherwise we just have a rogue surrogate marker or a plain old character.
charArray.unshift(str[i]);
}
return charArray.join('');
}
感谢Mathias、Punycode和其他各种参考资料,让我了解了JavaScript字符编码的复杂性。
有多种方法,你可以检查下面的,
1. 传统的for循环(递增):
函数reverseString (str) { let stringRev =""; For(令i= 0;我< str.length;我+ +){ stringRev = str[i]+stringRev; } 返回stringRev; } alert (reverseString(“Hello World !”);
2. 传统的for循环(递减):
函数反向字符串(str){ 让 revstr = “”; for(let i = str.length-1, i>=0, i--){ Revstr = Revstr+ str[i]; } 返回修订版; } alert(reverseString(“Hello World!”));
3.使用for-of循环
函数反向字符串(str){ 让 strn =“”; for(let char of str){ strn = char + strn; } 返回 STRN; } alert(reverseString(“Get Well soon”));
4. 使用forEach/高阶数组方法:
函数反向字符串(str){ 让 revSrring = “”; str.split(“”).forEach(function(char){ revSrring = char + revSrring; }); 返回转速Srring; } alert(reverseString(“Learning JavaScript”));
5. ES6标准:
函数反向字符串(str){ 让 revSrring = “”; str.split(“”).forEach(char => revSrring = char + revSrring); 返回转速Srring; } alert(reverseString(“Learning JavaScript”));
6. 最新的方式:
函数反向字符串(str){ return str.split(“”).reduce(function(revString, char){ 返回字符 + revString; }, ""); } alert(reverseString(“Learning JavaScript”));
7. 你也可以用下面的方法得到结果,
函数反向字符串(str){ return str.split(“”).reduce((revString, char)=> char + revString, “”); } alert(reverseString(“Learning JavaScript”));
看来我已经迟到3年了…
不幸的是,正如已经指出的那样,你不能。参见JavaScript字符串是不可变的吗?我需要一个“字符串生成器”在JavaScript?
你能做的下一个最好的事情是创建一个“视图”或“包装器”,它接受一个字符串并重新实现你正在使用的字符串API的任何部分,但假装字符串是反向的。例如:
var identity = function(x){return x};
function LazyString(s) {
this.original = s;
this.length = s.length;
this.start = 0; this.stop = this.length; this.dir = 1; // "virtual" slicing
// (dir=-1 if reversed)
this._caseTransform = identity;
}
// syntactic sugar to create new object:
function S(s) {
return new LazyString(s);
}
//We now implement a `"...".reversed` which toggles a flag which will change our math:
(function(){ // begin anonymous scope
var x = LazyString.prototype;
// Addition to the String API
x.reversed = function() {
var s = new LazyString(this.original);
s.start = this.stop - this.dir;
s.stop = this.start - this.dir;
s.dir = -1*this.dir;
s.length = this.length;
s._caseTransform = this._caseTransform;
return s;
}
//We also override string coercion for some extra versatility (not really necessary):
// OVERRIDE STRING COERCION
// - for string concatenation e.g. "abc"+reversed("abc")
x.toString = function() {
if (typeof this._realized == 'undefined') { // cached, to avoid recalculation
this._realized = this.dir==1 ?
this.original.slice(this.start,this.stop) :
this.original.slice(this.stop+1,this.start+1).split("").reverse().join("");
this._realized = this._caseTransform.call(this._realized, this._realized);
}
return this._realized;
}
//Now we reimplement the String API by doing some math:
// String API:
// Do some math to figure out which character we really want
x.charAt = function(i) {
return this.slice(i, i+1).toString();
}
x.charCodeAt = function(i) {
return this.slice(i, i+1).toString().charCodeAt(0);
}
// Slicing functions:
x.slice = function(start,stop) {
// lazy chaining version of https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/slice
if (stop===undefined)
stop = this.length;
var relativeStart = start<0 ? this.length+start : start;
var relativeStop = stop<0 ? this.length+stop : stop;
if (relativeStart >= this.length)
relativeStart = this.length;
if (relativeStart < 0)
relativeStart = 0;
if (relativeStop > this.length)
relativeStop = this.length;
if (relativeStop < 0)
relativeStop = 0;
if (relativeStop < relativeStart)
relativeStop = relativeStart;
var s = new LazyString(this.original);
s.length = relativeStop - relativeStart;
s.start = this.start + this.dir*relativeStart;
s.stop = s.start + this.dir*s.length;
s.dir = this.dir;
//console.log([this.start,this.stop,this.dir,this.length], [s.start,s.stop,s.dir,s.length])
s._caseTransform = this._caseTransform;
return s;
}
x.substring = function() {
// ...
}
x.substr = function() {
// ...
}
//Miscellaneous functions:
// Iterative search
x.indexOf = function(value) {
for(var i=0; i<this.length; i++)
if (value==this.charAt(i))
return i;
return -1;
}
x.lastIndexOf = function() {
for(var i=this.length-1; i>=0; i--)
if (value==this.charAt(i))
return i;
return -1;
}
// The following functions are too complicated to reimplement easily.
// Instead just realize the slice and do it the usual non-in-place way.
x.match = function() {
var s = this.toString();
return s.apply(s, arguments);
}
x.replace = function() {
var s = this.toString();
return s.apply(s, arguments);
}
x.search = function() {
var s = this.toString();
return s.apply(s, arguments);
}
x.split = function() {
var s = this.toString();
return s.apply(s, arguments);
}
// Case transforms:
x.toLowerCase = function() {
var s = new LazyString(this.original);
s._caseTransform = ''.toLowerCase;
s.start=this.start; s.stop=this.stop; s.dir=this.dir; s.length=this.length;
return s;
}
x.toUpperCase = function() {
var s = new LazyString(this.original);
s._caseTransform = ''.toUpperCase;
s.start=this.start; s.stop=this.stop; s.dir=this.dir; s.length=this.length;
return s;
}
})() // end anonymous scope
演示:
> r = S('abcABC')
LazyString
original: "abcABC"
__proto__: LazyString
> r.charAt(1); // doesn't reverse string!!! (good if very long)
"B"
> r.toLowerCase() // must reverse string, so does so
"cbacba"
> r.toUpperCase() // string already reversed: no extra work
"CBACBA"
> r + '-demo-' + r // natural coercion, string already reversed: no extra work
"CBAcba-demo-CBAcba"
最重要的是——下面是用纯数学来完成的,每个角色只访问一次,而且是在必要的时候:
> 'demo: ' + S('0123456789abcdef').slice(3).reversed().slice(1,-1).toUpperCase()
"demo: EDCBA987654"
> S('0123456789ABCDEF').slice(3).reversed().slice(1,-1).toLowerCase().charAt(3)
"b"
如果应用于一个非常大的字符串,如果您只取其中相对较小的部分,则可以节省大量的时间。
Whether this is worth it (over reversing-as-a-copy like in most programming languages) highly depends on your use case and how efficiently you reimplement the string API. For example if all you want is to do string index manipulation, or take small slices or substrs, this will save you space and time. If you're planning on printing large reversed slices or substrings however, the savings may be small indeed, even worse than having done a full copy. Your "reversed" string will also not have the type string, though you might be able to fake this with prototyping.
The above demo implementation creates a new object of type ReversedString. It is prototyped, and therefore fairly efficient, with almost minimal work and minimal space overhead (prototype definitions are shared). It is a lazy implementation involving deferred slicing. Whenever you perform a function like .slice or .reversed, it will perform index mathematics. Finally when you extract data (by implicitly calling .toString() or .charCodeAt(...) or something), it will apply those in a "smart" manner, touching the least data possible.
注意:上面的字符串API是一个例子,可能不能完美地实现。你也可以只使用你需要的1-2个函数。
function reverse(str){
var s = "";
for (var i = str.length - 1; i >= 0; i--){
s += str[i];
}
return s;
};
reverse("your string comes here")
推荐文章
- 使用jQuery改变输入字段的类型
- 在JavaScript中,什么相当于Java的Thread.sleep() ?
- 在Lua中拆分字符串?
- 使用jQuery以像素为整数填充或边距值
- 检查是否选择了jQuery选项,如果没有选择默认值
- Next.js React应用中没有定义Window
- 如何重置笑话模拟函数调用计数之前,每次测试
- 如何强制一个功能React组件渲染?
- 在javascript中从平面数组构建树数组
- 将Dropzone.js与其他字段集成到现有的HTML表单中
- 如何在Python中按字母顺序排序字符串中的字母
- 如何在AngularJS中观察路由变化?
- JavaScript DOM删除元素
- 将dd-mm-yyyy字符串转换为日期
- Javascript复选框onChange