简体   繁体   English

字符串中的第一个字母增加,最后一个字母减少

[英]First letter in a string to increase, last letter to decrease

I'm trying to create a simple encoder, where the first letter of every word becomes it's successor (A = B) and the last letter becomes its predecessor (B = A). 我正在尝试创建一个简单的编码器,每个单词的第一个字母成为它的后继(A = B),最后一个字母成为它的前任(B = A)。

Like so: 像这样:

 KEY: The first letter goes up, the last letter goes backwards. </p> If it is the first letter in the string, <span style='color: red;'>x</span> should be converted to <span style='color: blue;'>y</span>, as seen below: </p> <span style='color: red;'>Aa</span> = <span style='color: blue;'>Bb</span> </p> <span style='color: red;'>Bb</span> = <span style='color: blue;'>Cc</span> </p> <span style='color: red;'>Cc</span> = <span style='color: blue;'>Dd</span> </p> <span style='color: red;'>Dd</span> = <span style='color: blue;'>Ee</span> </p> and so on.... </p> If it is the last letter in the string, <span style='color: lightgreen;'>x</span> should be converted to <span style='color: orange;'>y</span>, as seen below: </p> <span style='color: lightgreen;'>Aa</span> = <span style='color: orange;'>Zz</span> </p> <span style='color: lightgreen;'>Bb</span> = <span style='color: orange;'>Aa</span> </p> <span style='color: lightgreen;'>Cc</span> = <span style='color: orange;'>Bb</span> </p> <span style='color: lightgreen;'>Dd</span> = <span style='color: orange;'>Cc</span> </p> and so on.... </p> <div class="encode"> The fox jumped over the dog. = Uhd gow kumpec pveq uhd eoh. </div> 

I'm trying to accomplish this using javaScript, however, I am open to using any language that will make this possible. 我正在尝试使用javaScript来实现这一点,但是,我愿意使用任何能够实现这一目标的语言。

This code is the gist of what I'm looking for, but I need to target the first and last letter of every word in a div: 这段代码是我正在寻找的要点,但我需要定位div中每个单词的第一个和最后一个字母:

var strRandomString = "I have 2 apples and 6 oranges and 3 grapes";
strRandomString.replace(/apples|oranges/g, function(m) {
    // `m` is a matched string.
    return m === 'apples' ? 'oranges' : 'apples';
})
// => "I have 2 oranges and 6 apples and 3 grapes"

Unfortunately, I do not have any actual sample javaScript code (but the above code can count as an attempt, a failed one), as I've been researching for about half an hour and couldn't find a viable solution. 不幸的是,我没有任何实际的示例javaScript代码(但上面的代码可以算作尝试,失败的代码),因为我已经研究了大约半个小时,但找不到可行的解决方案。

I've scoured the following threads for help: 我已经搜索了以下主题以寻求帮助:

https://codereview.stackexchange.com/questions/32620/replacing-every-letter-in-a-string-with-the-letter-following-it-in-the-alphabet https://codereview.stackexchange.com/questions/32620/replacing-every-letter-in-a-string-with-the-letter-following-it-in-the-alphabet

Promoting letters in a string to previous letter in java 将字符串中的字母提升为java中的上一个字母

https://coderanch.com/t/552363/java/Replacing-characters-string https://coderanch.com/t/552363/java/Replacing-characters-string

http://www.dreamincode.net/forums/topic/326746-question-shifting-characters-to-the-next-letter-in-the-alphabet/ http://www.dreamincode.net/forums/topic/326746-question-shifting-characters-to-the-next-letter-in-the-alphabet/

https://www.extendoffice.com/documents/excel/4027-excel-increase-letter-by-one.html (yep, I was so desperate that I was about to use excel). https://www.extendoffice.com/documents/excel/4027-excel-increase-letter-by-one.html (是的,我非常绝望,我即将使用excel)。

This works for a word, but also a phrase 这适用于一个单词,但也适用于短语

function transform(phrase)
{ var i,last,letters,words=phrase.split(/\s+/);
  for(i=0;i<words.length;i++)
  { letters=words[i].split('');
    last=letters.length-1;
    letters[0]=letters[0].charCodeAt(0)<122?String.fromCharCode(letters[0].charCodeAt(0)+1):'a';
    letters[last]=97<letters[last].charCodeAt(0)?String.fromCharCode(letters[last].charCodeAt(0)-1):'z';
    words[i]=letters.join('');
  }
  return words.join(' ');
}

console.log(transform('apple zebra variables'));
//outputs 'bppld aebrz wariabler'

To make it obvious, I simply created some functions then call them. 为了使它显而易见,我只是创建了一些函数然后调用它们。

Issues with this, single letter words like "a" get double messed with so "a" becomes "z" then back to "a" as the first and last letter. 与此相关的问题,像“a”这样的单字母单词变得双重混乱,因此“a”变为“z”,然后返回“a”作为第一个和最后一个字母。

 // I used this to remove the empty "" from the array, there might be better ways that altering the prototype but that might be opinion. Array.prototype.clean = function(deleteValue) { for (var i = 0; i < this.length; i++) { if (this[i] == deleteValue) { this.splice(i, 1); i--; } } return this; }; // wrap to begin using same case function prevLetter(letter) { if (letter === 'a') { return 'z'; } if (letter === 'A') { return 'Z'; } return String.fromCharCode(letter.charCodeAt(0) - 1); } // wrap to end using same case function nextLetter(letter) { if (letter === 'z') { return 'a'; } if (letter === 'Z') { return 'A'; } return String.fromCharCode(letter.charCodeAt(0) + 1); } // last letter of word function getLastLetter(myword) { return myword.slice(-1); } // first letter of word function getFirstLetter(myword) { return myword.charAt(0); } //split sentence into array by the ;,. and space function splitString(mystring) { return mystring.split(/[ ;,.]+/); } // replace a character (first,last) function setCharAt(mystring, index, chr) { if (index > mystring.length - 1) return mystring; return mystring.substr(0, index) + chr + mystring.substr(index + 1); } var mySentence = "The big, brown; cow ran over a zebra. I get this"; // words in the sentence to mess with var splitS = splitString(mySentence).clean(""); for (var i = 0; i < splitS.length; i++) { var newS = setCharAt(splitS[i], 0, nextLetter(getFirstLetter(splitS[i]))); var newSE = setCharAt(newS, newS.length - 1, prevLetter(getLastLetter(newS))); splitS[i] = newSE; } // the words in the array now are messed with console.log(splitS); 

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

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