简体   繁体   English

java替换字符串中的char

[英]java replacing char in string

  firstLetter = word.charAt(0);
  lastLetter = word.charAt((word.length()) - 1);
  noFirstLetter = word.substring(1);
  newWord = noFirstLetter + firstLetter;
  if(word.equalsIgnoreCase(newWord))

So im trying to take the first letter of the word and if I take the first letter of the word away and move it to the end it should be equal to the same word. 因此,我试图取下单词的第一个字母,如果我取下单词的第一个字母并将其移到末尾,则它应等于同一单词。 My code here isn't working. 我的代码在这里不起作用。 For example if the user entered "dresser" if you move the "d" to the end of the word you get the word "dresser" again. 例如,如果用户输入“ dresser”,而将“ d”移到单词的末尾,则再次得到单词“ dresser”。 That is what im trying to check 那就是我试图检查的

I think what you are trying to do is to remove the first character, and then check if rest of the characters are symmetrical around the center of the word.(OK even it is complicated for me) 我认为您要尝试删除的是第一个字符,然后检查其余字符是否围绕单词中心对称(即使对我来说也很复杂)

EG: 例如:

dresser => (drop d) => resser => (add d to the right) => resserd (read from right to left and it is dresser again). 梳妆台=>(放置d)=>梳妆台=>(向右添加d)=>梳妆台(从右到左读取,它又是梳妆台)。

after you drop the first letter: 删除第一个字母后:

resser (there are even number of letters in the word) 梳妆台(单词中字母的偶数)

r e s s e r
    |_|
  |_____|
|_________|

since they are symmetrical, you can say that that word would be Palindromic if you move D from left to right (or vice versa). 由于它们是对称的,因此可以说,如果您从左向右移动D(反之亦然),则该单词将是回文。

The whole thing above could be horribly wrong if I misunderstood your question at the first place. 如果我首先误解了您的问题,那么上面的整个事情可能是非常错误的。 But I assumed, your question was NOT a simple substring question. 但是我认为,您的问题不是一个简单的子字符串问题。

Cheers. 干杯。

Okay, so I'm presuming that you want to take a string , move it's first index to the end and then reverse the string . 好的,我假设您要获取一个string ,将其第一个索引移到末尾,然后反转该string If you do this to a word such as 'dresser' then you end up with the same value. 如果对诸如“ dresser”之类的单词执行此操作,则最终将获得相同的值。

Something like this could work, currently untested: 这样的事情可能会起作用,目前未经测试:

public StringBuilder reverseWord(String word){
    firstLetter = word.charAt(0); //find the first letter
    noFirstLetter = word.substring(1); //removing the first index
    newWord = noFirstLetter + firstLetter; //move first index to end
    return new StringBuilder(newWord).reverse().toString(); //reverse
}

if(reverseWord("dresser").equals("dresser")){
     //do something
}

Edit: As Jose has pointed out, it is important to check the length of the actual parameter by invoking length() on word . 编辑:正如Jose指出的,通过在word上调用length()来检查实际参数的长度很重要。

If you move 'd' to the end of the word "dresser" you get "resserd" which is not equal to "dresser". 如果将“ d”移到单词“ dresser”的末尾,则会得到“ resserd”,它不等于“ dresser”。

If you move 'd' to the end and then reverse the word then they are equal. 如果将“ d”移到末尾然后反转单词,则它们相等。

So, assuming you consider strings equals even if they are reversed, the code you want would be : 因此,假设即使字符串被反转,您都认为字符串等于,您想要的代码将是:

boolean testPass = false;
if ( word.length()==0 )
  testPass = true;
else
{
  firstLetter = word.charAt(0);
  noFirstLetter = word.substring(1);
  newWord = noFirstLetter + firstLetter;
  reversed = new StringBuilder(newWord).reverse().toString()
  if (word.equalsIgnoreCase(newWord) || word.equalsIgnoreCase(reversed))
    testPass = true;
}
if ( testPass )
  // Do something

Take notice of the important check of word having lenght 0. Otherwise word.charAt(0) will throw an exception. 注意长度为0的单词的重要检查。否则word.charAt(0)将引发异常。

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

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