C# 检查字符串是否为回文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9790749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a string is a palindrome
提问by ankur
I have a string as input and have to break the string in two substrings. If the left substring equals the right substring then do some logic.
我有一个字符串作为输入,必须将字符串分成两个子字符串。如果左子串等于右子串,那么做一些逻辑。
How can I do this?
我怎样才能做到这一点?
Sample:
样本:
public bool getStatus(string myString)
{
}
Example: myString = "ankYkna", so if we break it into two substring it would be:
left-part = "ank",
right-part = "ank"(after reversal).
示例:myString = "ankYkna",因此如果我们将其分成两个子字符串,它将是:
left-part = "ank",
right-part = "ank"(反转后)。
采纳答案by ionden
public static bool getStatus(string myString)
{
string first = myString.Substring(0, myString.Length / 2);
char[] arr = myString.ToCharArray();
Array.Reverse(arr);
string temp = new string(arr);
string second = temp.Substring(0, temp.Length / 2);
return first.Equals(second);
}
回答by Thomas Levesque
回答by T. Kiley
That is non-trivial, there is no built in method to do that for you, you'll have to write your own. You will need to consider what rules you would like to check, like you implicitly stated you accepted reversing of one string. Also, you missed out the middle character, is this only if odd length?
这很重要,没有内置的方法可以为您做到这一点,您必须自己编写。您需要考虑要检查哪些规则,就像您隐式声明您接受反转一个字符串一样。另外,您错过了中间字符,这仅是奇数长度吗?
So you will have something like:
所以你会有类似的东西:
if(myString.length % 2 = 0)
{
//even
string a = myString.substring(0, myString.length / 2);
string b = myString.substring(myString.length / 2 + 1, myString.lenght/2);
if(a == b)
return true;
//Rule 1: reverse
if(a == b.reverse()) //can't remember if this is a method, if not you'll have to write that too
return true;
etc, also doing whatever you want for odd strings
等等,也可以为奇数字符串做任何你想做的事情
回答by Balazs Tihanyi
int length = myString.Length;
for (int i = 0; i < length / 2; i++)
{
if (myString[i] != myString[length - i - 1])
return false;
}
return true;
回答by Adrian Iftode
Using LINQ and off course far from the best solution
使用 LINQ 并远离最佳解决方案
var original = "ankYkna";
var reversed = new string(original.Reverse().ToArray());
var palindrom = original == reversed;
回答by Balazs Tihanyi
Just for fun:
只是为了好玩:
return myString.SequenceEqual(myString.Reverse());
回答by Ernesto Cejas
A single line of code using Linq
一行代码使用 Linq
public static bool IsPalindrome(string str)
{
return str.SequenceEqual(str.Reverse());
}
回答by Tabish Habib
//This c# method will check for even and odd lengh palindrome string
//这个c#方法将检查偶数和奇数长度的回文字符串
public static bool IsPalenDrome(string palendromeString)
{
bool isPalenDrome = false;
try
{
int halfLength = palendromeString.Length / 2;
string leftHalfString = palendromeString.Substring(0,halfLength);
char[] reversedArray = palendromeString.ToCharArray();
Array.Reverse(reversedArray);
string reversedString = new string(reversedArray);
string rightHalfStringReversed = reversedString.Substring(0, halfLength);
isPalenDrome = leftHalfString == rightHalfStringReversed ? true : false;
}
catch (Exception ex)
{
throw ex;
}
return isPalenDrome;
}
回答by Tabish Habib
This C# method will check for even and odd length palindrome string (Recursive Approach):
此 C# 方法将检查偶数和奇数长度的回文字符串(递归方法):
public static bool IsPalindromeResursive(int rightIndex, int leftIndex, char[] inputString)
{
if (rightIndex == leftIndex || rightIndex < leftIndex)
return true;
if (inputString[rightIndex] == inputString[leftIndex])
return IsPalindromeResursive(--rightIndex, ++leftIndex, inputString);
else
return false;
}
回答by kennydust
public Boolean IsPalindrome(string value)
{
var one = value.ToList<char>();
var two = one.Reverse<char>().ToList();
return one.Equals(two);
}

