我有一个字符串作为输入,必须在两个子串中打破字符串。如果左子串等于右子串,则执行一些逻辑。
我该怎么做?
样品:
public bool getStatus(string myString)
{
}
示例:myString = "ankYkna"
,所以如果我们将它分成两个子字符串,它将是:
left-part = "ank"
,
right-part = "ank"
(逆转后)。
答案 0 :(得分:62)
只是为了好玩:
return myString.SequenceEqual(myString.Reverse());
答案 1 :(得分:22)
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);
}
答案 2 :(得分:14)
int length = myString.Length;
for (int i = 0; i < length / 2; i++)
{
if (myString[i] != myString[length - i - 1])
return false;
}
return true;
答案 3 :(得分:12)
使用LINQ并且远离最佳解决方案
var original = "ankYkna";
var reversed = new string(original.Reverse().ToArray());
var palindrom = original == reversed;
答案 4 :(得分:5)
使用Linq
public static bool IsPalindrome(string str)
{
return str.SequenceEqual(str.Reverse());
}
答案 5 :(得分:4)
public static bool IsPalindrome(string value)
{
int i = 0;
int j = value.Length - 1;
while (true)
{
if (i > j)
{
return true;
}
char a = value[i];
char b = value[j];
if (char.ToLower(a) != char.ToLower(b))
{
return false;
}
i++;
j--;
}
}
答案 6 :(得分:2)
//这个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;
}
答案 7 :(得分:1)
字符串扩展方法,易于使用:
public static bool IsPalindrome(this string str)
{
str = new Regex("[^a-zA-Z]").Replace(str, "").ToLower();
return !str.Where((t, i) => t != str[str.Length - i - 1]).Any();
}
答案 8 :(得分:1)
private void CheckIfPalindrome(string str)
{
//place string in array of chars
char[] array = str.ToCharArray();
int length = array.Length -1 ;
Boolean palindrome =true;
for (int i = 0; i <= length; i++)//go through the array
{
if (array[i] != array[length])//compare if the char in the same positions are the same eg "tattarrattat" will compare array[0]=t with array[11] =t if are not the same stop the for loop
{
MessageBox.Show("not");
palindrome = false;
break;
}
else //if they are the same make length smaller by one and do the same
{
length--;
}
}
if (palindrome) MessageBox.Show("Palindrome");
}
答案 9 :(得分:1)
public static bool IsPalindrome(string word)
{
//first reverse the string
string reversedString = new string(word.Reverse().ToArray());
return string.Compare(word, reversedString) == 0 ? true : false;
}
答案 10 :(得分:1)
这种方式既简洁又简洁;过程很快。
Func<string, bool> IsPalindrome = s => s.Reverse().Equals(s);
答案 11 :(得分:0)
General command line usage:
b2 [options] [properties] [targets]
Options, properties and targets can be specified in any order.
Important Options:
* --clean Remove targets instead of building
* -a Rebuild everything
* -n Don't execute the commands, only print them
* -d+2 Show commands as they are executed
* -d0 Suppress all informational messages
* -q Stop at first error
* --reconfigure Rerun all configuration checks
* --debug-configuration Diagnose configuration
* --debug-building Report which targets are built with what properties
* --debug-generator Diagnose generator search/execution
Further Help:
The following options can be used to obtain additional documentation.
* --help-options Print more obscure command line options.
* --help-internal Boost.Build implementation details.
* --help-doc-options Implementation details doc formatting.
答案 12 :(得分:0)
使用常规方法找到回文的最短技术
public string IsPalindrome(string Word)
{
int i = 0, j = Word.Length - 1;
for (; i < Word.Length - 1 && j >= 0 && Word[i] == Word[j]; i++, j--) ;
return j == 0 ? "Palindrome" : "Not Palindrome";
}
答案 13 :(得分:0)
docker run -d --rm --name my_keyring_test -e MYSQL_ALLOW_EMPTY_PASSWORD=true file_keyringed_mysql:latest
答案 14 :(得分:0)
提供的各种答案有很多原因是错误的,主要是由于误解了回文。大多数人只能正确识别回文症的一部分。
来自Merriam-Webster
单词,经文或句子(例如“我当时见过厄尔巴岛”)
来自Wordnik
一个单词,词组,经文或句子向后或向前读相同。例如:一个人,一个计划,一条运河,巴拿马!
考虑非平凡的回文,例如“马拉雅拉姆语”(这是一种适当的语言,因此适用命名规则,应大写)或回文句子,例如“我看见的是汽车还是猫?”或“尼克松中没有'X'”。
在任何文献中这些都是公认的回文。
我正在从图书馆提供完整的解决方案,该图书馆提供了我主要的作者,因此该解决方案适用于String
和ReadOnlySpan<Char>
,因为这是我的要求已强加给图书馆。但是,仅凭此确定纯String
的解决方案即可。
public static Boolean IsPalindrome(this String @string) =>
!(@string is null) && @string.AsSpan().IsPalindrome();
public static Boolean IsPalindrome(this ReadOnlySpan<Char> span) {
// First we need to build the string without any punctuation or whitespace or any other
// unrelated-to-reading characters.
StringBuilder builder = new StringBuilder(span.Length);
foreach (Char s in span) {
if (!(s.IsControl()
|| s.IsPunctuation()
|| s.IsSeparator()
|| s.IsWhiteSpace()) {
_ = builder.Append(s);
}
}
String prepped = builder.ToString();
String reversed = prepped.Reverse().Join();
// Now actually check it's a palindrome
return String.Equals(prepped, reversed, StringComparison.CurrentCultureIgnoreCase);
}
在测试特定语言而不是您自己的语言时,您将需要该变量的变体也接受一个CultureInfo
参数,而是在{{1 }}。
这是项目单元测试证明它有效的方法。
答案 15 :(得分:0)
public static bool IsPalindrome(string str)
{
int i = 0;
int a = 0;
char[] chr = str.ToCharArray();
foreach (char cr in chr)
{
Array.Reverse(chr);
if (chr[i] == cr)
{
if (a == str.Length)
{
return true;
}
a++;
i++;
}
else
{
return false;
}
}
return true;
}
答案 16 :(得分:0)
在C#中:
public bool EhPalindromo(string text)
{
var reverseText = string.Join("", text.ToLower().Reverse());
return reverseText == text;
}
答案 17 :(得分:0)
call instance void [mscorlib]System.Object::.ctor()
答案 18 :(得分:0)
public bool Solution(string content)
{
int length = content.Length;
int half = length/2;
int isOddLength = length%2;
// Counter for checking the string from the middle
int j = (isOddLength==0) ? half:half+1;
for(int i=half-1;i>=0;i--)
{
if(content[i] != content[j])
{
return false;
}
j++;
}
return true;
}
答案 19 :(得分:0)
这是检查回文的一种简便有效的方法。
bool checkPalindrome(string inputString) {
int length = inputString.Length;
for(int i = 0; i < length/2; i++){
if(inputString[i] != inputString[length-1-i]){
return false;
}
}
return true;
}
答案 20 :(得分:0)
using System;
class Program
{
/// <summary>
/// Determines whether the string is a palindrome.
/// </summary>
public static bool IsPalindrome(string value)
{
int min = 0;
int max = value.Length - 1;
while (true)
{
if (min > max)
{
return true;
}
char a = value[min];
char b = value[max];
// Scan forward for a while invalid.
while (!char.IsLetterOrDigit(a))
{
min++;
if (min > max)
{
return true;
}
a = value[min];
}
// Scan backward for b while invalid.
while (!char.IsLetterOrDigit(b))
{
max--;
if (min > max)
{
return true;
}
b = value[max];
}
if (char.ToLower(a) != char.ToLower(b))
{
return false;
}
min++;
max--;
}
}
static void Main()
{
string[] array =
{
"A man, a plan, a canal: Panama.",
"A Toyota. Race fast, safe car. A Toyota.",
"Cigar? Toss it in a can. It is so tragic.",
"Dammit, I'm mad!",
"Delia saw I was ailed.",
"Desserts, I stressed!",
"Draw, O coward!",
"Lepers repel.",
"Live not on evil.",
"Lonely Tylenol.",
"Murder for a jar of red rum.",
"Never odd or even.",
"No lemon, no melon.",
"Senile felines.",
"So many dynamos!",
"Step on no pets.",
"Was it a car or a cat I saw?",
"Dot Net Perls is not a palindrome.",
"Why are you reading this?",
"This article is not useful.",
"...",
"...Test"
};
foreach (string value in array)
{
Console.WriteLine("{0} = {1}", value, IsPalindrome(value));
}
}
}
答案 21 :(得分:0)
public static bool palindrome(string t)
{
int i = t.Length;
for (int j = 0; j < i / 2; j++)
{
if (t[j] == t[i - j-1])
{
continue;
}
else
{
return false;
break;
}
}
return true;
}
答案 22 :(得分:0)
protected bool CheckIfPalindrome(string text)
{
if (text != null)
{
string strToUpper = Text.ToUpper();
char[] toReverse = strToUpper.ToCharArray();
Array.Reverse(toReverse );
String strReverse = new String(toReverse);
if (strToUpper == toReverse)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
以最简单的方式使用它。
答案 23 :(得分:0)
这不重要,没有内置的方法为你做,你必须自己编写。您将需要考虑要检查的规则,例如您隐含地声明您接受了一个字符串的反转。而且,你错过了中间字符,这只是奇数长度?
所以你会有类似的东西:
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;
等,也为奇数字符串做任何你想要的事情
答案 24 :(得分:0)
static void Main(string[] args)
{
string str, rev="";
Console.Write("Enter string");
str = Console.ReadLine();
for (int i = str.Length - 1; i >= 0; i--)
{
rev = rev + str[i];
}
if (rev == str)
Console.Write("Entered string is pallindrome");
else
Console.Write("Entered string is not pallindrome");
Console.ReadKey();
}
答案 25 :(得分:0)
在所有解决方案中,也可以尝试以下方法:
public static bool IsPalindrome(string s)
{
return s == new string(s.Reverse().ToArray());
}
答案 26 :(得分:0)
这是一种绝对简单的方法,
这是一个很好的方式,因为你不必投注和双打。你可以使用ToString()方法将它们传递给字符串表示形式的方法。
public static bool IsPalindrome(string word)
{
string spare = word;
string reversal = null;
while (word.Length > 0)
{
reversal = string.Concat(reversal, word.LastOrDefault());
word = word.Remove(word.Length - 1);
}
return spare.Equals(reversal);
}
所以从你的主要方法来看, 对于偶数和奇数长度的字符串,您只需将整个字符串传递给方法。
答案 27 :(得分:0)
如果您只是需要检测回文,可以使用正则表达式进行检查,如here所述。可能不是最有效的方法,但是......
答案 28 :(得分:0)
由于回文也包括数字,单词,句子以及这些的任意组合,并且应该忽略标点符号和大小写,(See Wikipedia Article) 我提出这个解决方案:
$data['debugger'] = 'this is the default information controller';
答案 29 :(得分:0)
这个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;
}
答案 30 :(得分:0)
public bool IsPalindroom(string input)
{
input = input.ToLower();
var loops = input.Length / 2;
var higherBoundIdx = input.Length - 1;
for (var lowerBoundIdx = 0; lowerBoundIdx < loops; lowerBoundIdx++, higherBoundIdx--)
{
if (input[lowerBoundIdx] != input[higherBoundIdx])
return false;
}
return true;
}
答案 31 :(得分:0)
class Program
{
static void Main(string[] args)
{
string s, revs = "";
Console.WriteLine(" Enter string");
s = Console.ReadLine();
for (int i = s.Length - 1; i >= 0; i--) //String Reverse
{
Console.WriteLine(i);
revs += s[i].ToString();
}
if (revs == s) // Checking whether string is palindrome or not
{
Console.WriteLine("String is Palindrome");
}
else
{
Console.WriteLine("String is not Palindrome");
}
Console.ReadKey();
}
}
答案 32 :(得分:0)
public Boolean IsPalindrome(string value)
{
var one = value.ToList<char>();
var two = one.Reverse<char>().ToList();
return one.Equals(two);
}
答案 33 :(得分:-1)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class palindrome
{
static void Main(string[] args)
{
Console.Write("Enter a number:");
string panstring = Console.ReadLine();
Palindrome(panstring);
Console.ReadKey();
}
static int index = 0;
public static void Palindrome(string strexcluding)
{
try
{
string reversecounter = string.Empty;
for (int i = strexcluding.Length - 1; i >= 0; i--)
{
if (strexcluding[i].ToString() != null)
reversecounter += strexcluding[i].ToString();
}
if (reversecounter == strexcluding)
{
Console.WriteLine("Palindrome Number: " + strexcluding);
}
else
{
Sum(strexcluding);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public static void Sum(string stringnumber)
{
try
{
index++;
string number1 = stringnumber;
string number2 = stringnumber;
string[] array = new string[number1.Length];
string obtained = string.Empty;
string sreverse = null;
Console.WriteLine(index + ".step : " + number1 + "+" + number2);
for (int i = 0; i < number1.Length; i++)
{
int temp1 = Convert.ToInt32(number1[number1.Length - i - 1].ToString());
int temp2 = Convert.ToInt32(number2[number2.Length - i - 1].ToString());
if (temp1 + temp2 >= 10)
{
if (number1.Length - 1 == number1.Length - 1 - i)
{
array[i] = ((temp1 + temp2) - 10).ToString();
obtained = "one";
}
else if (number1.Length - 1 == i)
{
if (obtained == "one")
{
array[i] = (temp1 + temp2 + 1).ToString();
}
else
{
array[i] = (temp1 + temp2).ToString();
}
}
else
{
if (obtained == "one")
{
array[i] = ((temp1 + temp2 + 1) - 10).ToString();
}
else
{
array[i] = ((temp1 + temp2) - 10).ToString();
obtained = "one";
}
}
}
else
{
if (obtained == "one")
array[i] = (temp1 + temp2 + 1).ToString();
else
array[i] = (temp1 + temp2).ToString();
obtained = "Zero";
}
}
for (int i = array.Length - 1; i >= 0; i--)
{
if (array[i] != null)
sreverse += array[i].ToString();
}
Palindrome(sreverse);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}