简体   繁体   中英

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. 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).

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).

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 . If you do this to a word such as 'dresser' then you end up with the same value.

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 .

If you move 'd' to the end of the word "dresser" you get "resserd" which is not equal to "dresser".

If you move 'd' to the end and then reverse the word then they are equal.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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