简体   繁体   English

JavaScript substr(),获取字符串中间的字符

[英]JavaScript substr(), Get Characters in the Middle of the String

I have a string, var str = "Runner, The (1999)"; 我有一个字符串,var str =“ Runner,The(1999)”;

Using substr(), I need to see if ", The" is contained in str starting from 7 characters back, then if it is, remove those characters and put them in the start. 使用substr(),我需要查看str中是否包含“,The”(从后面的7个字符开始),如果是,则删除这些字符并将其放在开头。 Something like this: 像这样:

if (str.substr(-7) === ', The') // If str has ', The' starting from 7 characters back...
{
    str = 'The ' + str.substr(-7, 5); // Add 'The ' to the start of str and remove it from middle.
}

The resulting str should equal "The Runner (1999)" 产生的str应等于“ The Runner(1999)”。

Please, no regular expressions or other functions. 请不要使用正则表达式或其他函数。 I'm trying to learn how to use substr. 我正在尝试学习如何使用substr。

var str = "Runner, The (1999)";
if(str.indexOf(", The") != -1) {
    str = "The "+str.replace(", The","");
}

Here you go, using only substr as requested: 在这里,仅按要求使用substr

var str = "Runner, The (1999)";

if(str.substr(-12, 5) === ', The') {
    str = 'The ' + str.substr(0, str.length - 12) + str.substr(-7);
}

alert(str);

Working JSFiddle 工作中的JSFiddle

It should be noted that this is not the best way to achieve what you want (especially using hardcoded values like -7 – almost never as good as using things like lastIndexOf , regex, etc). 应当指出,这不是实现所需目标的最佳方法(尤其是使用像-7这样的硬编码值–几乎从来没有像使用lastIndexOf ,regex等之类的东西那样好)。 But you wanted substr , so there it is. 但是您想要substr ,所以就可以了。

If you want to use just substr: 如果您只想使用substr:

var a = "Runner, The (1999)"
var newStr;
if (str.substr(-7) === ', The')
    newStr= 'The ' +a.substr(0,a.length-a.indexOf('The')-4) + a.substr(a.indexOf('The')+3)

Use a.substr(0,a.length-a.indexOf('The')-4) to obtain the words before "The" and a.substr(a.indexOf('The')+3) to obtain the words after it. 使用a.substr(0,a.length-a.indexOf('The')-4)获得“ The”之前的单词,并使用a.substr(a.indexOf('The')+3)获得单词之后。

So, you say that the solution should be limited to only substr method. 因此,您说解决方案应仅限于substr方法。 There will be different solutions depending on what you mean by: 根据您的意思会有不同的解决方案:

", The" is contained in str starting from 7 characters back “,The”包含在str中,从7个字符开始

If you mean that it's found exactly in -7 position, then the code could look like this (I replaced -7 with -12, so that the code returned true): 如果您的意思是完全位于-7位,那么代码可能看起来像这样(我将-7替换为-12,以便代码返回true):

function one() {
  var a = "Runner, The (1999)";
  var b = ", The";
  var c = a.substr(-12, b.length);
  if (c == b) {
    a = "The " + a.substr(0, a.length - 12) +
    a.substr(a.length - 12 + b.length);
  }
}

If, however, substring ", The" can be found anywhere between position -7 and the end of the string, and you really need to use only substr, then check this out: 但是,如果子字符串“,The”可以在位置-7和字符串末尾之间的任何位置找到,而您实际上需要使用substr,则检查一下:

function two() {
  var a = "Runner, The (1999)";
  var b = ", The";
  for (var i = a.length - 12; i < a.length - b.length; i++) {
    if (a.substr(i, b.length) == b) {
      a = "The " + a.substr(0, i) + a.substr(i + b.length);
      break;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM