在不使用任何内置函数的情况下在javascript中反转字符串

时间:2017-08-15 10:28:43

标签: javascript

 function reverse1(str){
  var a = "";
  for(var i = 0; i <= str.length/2; i++){
    a = str[i];
    str[i] = str[str.length-i-1];
    str[str.length-i-1] = a;
  }
  return str;
}
var str = "abcdef";
reverse1(str);

我想在不使用任何内置函数的情况下反转字符串,我希望它能够更改原始字符串本身,但它不能正常工作。 语言是Javascript。

7 个答案:

答案 0 :(得分:2)

这是不使用任何javascript内置函数的最简单方法。

function reverse1(str){
  let r = "";
  for(let i = str.length-1; i >= 0; i--){
    r += str[i];
  }
  return r;
}

console.log(reverse1("javascript"))

答案 1 :(得分:0)

forloop迭代从高到低i--反转用于递减i的值

&#13;
&#13;
function reverse1(str) {
str = str.trim();
var res ="";
   for(var i = str.length-1; i >= 0; i--){
      res +=str[i];
  }
  return res;
}
var str = "abcdef";
console.log(reverse1(str))
&#13;
&#13;
&#13;

答案 2 :(得分:0)

Javascript字符串为immutable,您不能简单地用另一个字符替换字符

&#13;
&#13;
function reverse1(str){
  var  len = str.length, result = "";
  for(var i = 0; i <= len-1; i++){
    result = result + str[len-i-1];
  }
  return result;
}
var str = "abcdef";
str = reverse1(str);
console.log(str);
&#13;
&#13;
&#13;

您始终可以创建新字符串并通过

返回

答案 3 :(得分:0)

创建一个新字符串并将原始字符串中的所有字符向后添加到其中:

function reverse1(str){
  var r = "";
  for(var i = str.length - 1; i >= 0; i--){
    r += str.charAt(i);
  }
  return r;
}

然后说:

str = reverse1(str);

答案 4 :(得分:0)

如果您不想在这里使用内置功能,那么

var string = 'hello';
function reverse(str) {
  if(!str.trim() || 'string' !== typeof str) {
    return;
  }
  var length=str.length;
  s='';
  while(length > 0) {
    length--;
    s+= str[l];
  }
  return s;
}

console.log(reverse(string));

答案 5 :(得分:0)

import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import {
  Anchor,
  Button,
  Card,
  Col,
  Descriptions,
  Drawer,
  Form,
  Input,
  PageHeader,
  Row,
  Spin,
  Steps
} from "antd";
import { Content } from "antd/es/layout/layout";
const { Step } = Steps;
const { Link } = Anchor;
const renderContent = (column = 3) => (
  <Descriptions size="small" column={column}>
    <Descriptions.Item label="Book Number">
      <a>421421</a>
    </Descriptions.Item>
    <Descriptions.Item label="Book Number">
      {" "}
      <a>Avbg Qu</a>
    </Descriptions.Item>
    <Descriptions.Item label="Book NumberNumber">
      <a>2017-01-10</a>
    </Descriptions.Item>
    <Descriptions.Item label="Book Number">
      <a>2017-10-10</a>
    </Descriptions.Item>
    <Descriptions.Item label="Book Number">
      {" "}
      <a>Accounts</a>
    </Descriptions.Item>
    <Descriptions.Item label="Book Number">
      {" "}
      <a>LPGFH</a>
    </Descriptions.Item>
  </Descriptions>
);

ReactDOM.render(
  <div className={"abs-main-body  "}>
    <div>
      <PageHeader
        className="site-page-header-responsive box_shadow margin_bottom_7px "
        title="Enter Fixed Asset Details "
      >
        <Content>{renderContent()}</Content>
      </PageHeader>
    </div>
    <div className={"abs-board-header "}>
      <Row gutter={[8, 8]}>
        <Col span={12}>
          <div className={"ad-side"}>
            <Card
              title="Current Status"
              className="box_shadow"
              style={{ flex: 1, width: "100%" }}
            >
              <Anchor
                style={{ width: "100%" }}
                showInkInFixed={false}
                affix={false}
              >
                <Steps direction="vertical" size="small" current={1}>
                  <Step
                    title={<Link href="#one" title="Invoice Details" />}
                    description="This is a description."
                  />
                  <Step
                    title="In Progress"
                    description="This is a description."
                  />
                  <Step title="Waiting" description="This is a description." />
                  <Step title="Waiting" description="This is a description." />
                  <Step title="Waiting" description="This is a description." />
                  <Step title="Waiting" description="This is a description." />
                  <Step title="Waiting" description="This is a description." />

                  <Step title="Waiting" description="This is a description." />
                  <Step title="Waiting" description="This is a description." />
                  <Step title="Waiting" description="This is a description." />
                  <Step title="Waiting" description="This is a description." />
                  <Step title="Waiting" description="This is a description." />
                  <Step title="Waiting" description="This is a description." />
                </Steps>
              </Anchor>
            </Card>
          </div>
        </Col>
        <Col span={12}>
          <div>
            Section-1
            <br />
            <p>
              n publishing and graphic design, Lorem ipsum is a placeholder text
              commonly used to demonstrate the visual form of a document or a
              typeface without relying on meaningful content. Lorem ipsum may be
              used as a placeholder before final copy is available. n publishing
              and graphic design, Lorem ipsum is a placeholder text commonly
              used to demonstrate the visual form of a document or a typeface
              without relying on meaningful content. Lorem ipsum may be used as
              a placeholder before final copy is available. n publishing and
              graphic design, Lorem ipsum is a placeholder text commonly used to
              demonstrate the visual form of a document or a typeface without
              relying on meaningful content. Lorem ipsum may be used as a
              placeholder before final copy is available.
            </p>
          </div>
          <div>
            Section-2
            <br />
            <p>
              n publishing and graphic design, Lorem ipsum is a placeholder text
              commonly used to demonstrate the visual form of a document or a
              typeface without relying on meaningful content. Lorem ipsum may be
              used as a placeholder before final copy is available. n publishing
              and graphic design, Lorem ipsum is a placeholder text commonly
              used to demonstrate the visual form of a document or a typeface
              without relying on meaningful content. Lorem ipsum may be used as
              a placeholder before final copy is available. n publishing and
              graphic design, Lorem ipsum is a placeholder text commonly used to
              demonstrate the visual form of a document or a typeface without
              relying on meaningful content. Lorem ipsum may be used as a
              placeholder before final copy is available.
            </p>
          </div>
          <div>
            Section-3
            <br />
            <p>
              n publishing and graphic design, Lorem ipsum is a placeholder text
              commonly used to demonstrate the visual form of a document or a
              typeface without relying on meaningful content. Lorem ipsum may be
              used as a placeholder before final copy is available. n publishing
              and graphic design, Lorem ipsum is a placeholder text commonly
              used to demonstrate the visual form of a document or a typeface
              without relying on meaningful content. Lorem ipsum may be used as
              a placeholder before final copy is available.
            </p>
          </div>
          <div>
            Section-4
            <br />
            <p>
              n publishing and graphic design, Lorem ipsum is a placeholder text
              commonly used to demonstrate the visual form of a document or a
              typeface without relying on meaningful content. Lorem ipsum may be
              used as a placeholder before final copy is available. n publishing
              and graphic design, Lorem ipsum is a placeholder text commonly
              used to demonstrate the visual form of a document or a typeface
              without relying on meaningful content. Lorem ipsum may be used as
              a placeholder before final copy is available.
            </p>
          </div>
          <div>
            Section-5
            <br />
            <p>
              n publishing and graphic design, Lorem ipsum is a placeholder text
              commonly used to demonstrate the visual form of a document or a
              typeface without relying on meaningful content. Lorem ipsum may be
              used as a placeholder before final copy is available. n publishing
              and graphic design, Lorem ipsum is a placeholder text commonly
              used to demonstrate the visual form of a document or a typeface
              without relying on meaningful content. Lorem ipsum may be used as
              a placeholder before final copy is available.
            </p>
          </div>
          <div>
            Section-6
            <br />
            <p>
              n publishing and graphic design, Lorem ipsum is a placeholder text
              commonly used to demonstrate the visual form of a document or a
              typeface without relying on meaningful content. Lorem ipsum may be
              used as a placeholder before final copy is available. n publishing
              and graphic design, Lorem ipsum is a placeholder text commonly
              used to demonstrate the visual form of a document or a typeface
              without relying on meaningful content. Lorem ipsum may be used as
              a placeholder before final copy is available. n publishing and
              graphic design, Lorem ipsum is a placeholder text commonly used to
              demonstrate the visual form of a document or a typeface without
              relying on meaningful content. Lorem ipsum may be used as a
              placeholder before final copy is available.
            </p>
          </div>
          <div id="one">
            Section-7
            <br />
            <p>
              n publishing and graphic design, Lorem ipsum is a placeholder text
              commonly used to demonstrate the visual form of a document or a
              typeface without relying on meaningful content. Lorem ipsum may be
              used as a placeholder before final copy is available. n publishing
              and graphic design, Lorem ipsum is a placeholder text commonly
              used to demonstrate the visual form of a document or a typeface
              without relying on meaningful content. Lorem ipsum may be used as
              a placeholder before final copy is available. n publishing and
              graphic design, Lorem ipsum is a placeholder text commonly used to
              demonstrate the visual form of a document or a typeface without
              relying on meaningful content. Lorem ipsum may be used as a
              placeholder before final copy is available.v
            </p>
          </div>
        </Col>
      </Row>
    </div>
  </div>,
  document.getElementById("container")
);

答案 6 :(得分:-2)

我认为这个问题更好的答案是here。为什么char不能改变立场。

Duplicate

&#13;
&#13;
function reverse(s){
    return s.split("").reverse().join("");
}
var originalString = "ABCD";
console.log(reverse(originalString));
console.log(originalString);
originalString = reverse(originalString);
console.log(originalString);
&#13;
&#13;
&#13;