C# (String.StartsWith && !String.EndsWith && !String.Contains) с использованием спискаC#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 C# (String.StartsWith && !String.EndsWith && !String.Contains) с использованием списка

Сообщение Anonymous »

У меня есть 2 списка, 2 строки и метод getCombinations(string), который возвращает все комбинации строк в виде списка.
Как проверить, что элемент subjectStrings не соответствует

Код: Выделить всё

StartWith && !EndsWith && !Contains
(или !StartWith && !EndsWith && contains и т. д.)
для всех комбинаций начинается с String, заканчивается с String и содержит String?
Вот мой код в StartWith && !EndsWith
(если вы хотите увидеть его работающим: http://ideone.com/y8JZkK)

Код: Выделить всё

using System;
using System.Collections.Generic;
using System.Linq;

public class Test
{
public static void Main()
{
List validatedStrings = new List();
List subjectStrings = new List()
{
"con", "cot", "eon", "net", "not", "one", "ten", "toe", "ton",
"cent", "cone", "conn", "cote", "neon", "none", "note", "once", "tone",
"cento", "conte", "nonce", "nonet", "oncet", "tenon", "tonne",
"nocent","concent", "connect"
}; //got a more longer wordlist

string startswithString = "co";
string endswithString = "et";

foreach(var z in subjectStrings)
{
bool valid = false;
foreach(var a in getCombinations(startswithString))
{
foreach(var b in getCombinations(endswithString))
{
if(z.StartsWith(a) && !z.EndsWith(b))
{
valid = true;
break;
}
}
if(valid)
{
break;
}
}
if(valid)
{
validatedStrings.Add(z);
}
}

foreach(var a in validatedStrings)
{
Console.WriteLine(a);
}
Console.WriteLine("\nDone");
}

static List getCombinations(string s)
{
//Code that calculates combinations
return Permutations.Permutate(s);
}
}

public class Permutations
{
private static List allCombinations;

private static void CalculateCombinations(string word, List temp)
{
if (temp.Count == word.Length)
{
List clone = temp.ToList();
if (clone.Distinct().Count() == clone.Count)
{
allCombinations.Add(clone);
}
return;
}

for (int i = 0; i < word.Length; i++)
{
temp.Add(word[i].ToString());
CalculateCombinations(word, temp);
temp.RemoveAt(temp.Count - 1);
}
}

public static List Permutate(string str)
{
allCombinations = new List();
CalculateCombinations(str, new List());
List  combinations = new List();
foreach(var a in allCombinations)
{
string c = "";
foreach(var b in a)
{
c+=b;
}
combinations.Add(c);
}
return combinations;
}
}
Выход:

Код: Выделить всё

con
cot
cone
conn
cote 

Подробнее здесь: [url]https://stackoverflow.com/questions/19341962/c-sharp-string-startswith-string-endswith-string-contains-using-a-list[/url]
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Как отличить message.content.startswith('$del') от message.content.startswith('$delall') при написании для бота Discord
    Гость » » в форуме Python
    0 Ответы
    116 Просмотры
    Последнее сообщение Гость
  • Google Truth.assertThat.contains не ведет себя так же, как List.contains.
    Anonymous » » в форуме Android
    0 Ответы
    48 Просмотры
    Последнее сообщение Anonymous
  • Почему у нас есть contains(Object o) вместо contains(E e)?
    Anonymous » » в форуме JAVA
    0 Ответы
    57 Просмотры
    Последнее сообщение Anonymous
  • Python использует проблему метода Endswith с удалением из списка [дубликат]
    Гость » » в форуме Python
    0 Ответы
    10 Просмотры
    Последнее сообщение Гость
  • Как удалить большую цепочку if-else-if с условием String::startswith
    Anonymous » » в форуме JAVA
    0 Ответы
    15 Просмотры
    Последнее сообщение Anonymous

Вернуться в «C#»