Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Given a string like string s = "a(b)cd(ef)",
what is the best way to get a list/array containing all the following strings:
"acd",
"abcd",
"acdef",
"abcdef"
Additional information:
The order of the result doesn't matter.
The amount of parenthesis is unknown
I have tried to do this with RegEx, but didn't get very far.
There are many ways to do this, however here is an example using a mask array to keep track of the permutation state, some regex, and a few helper methods
Given
// work out the states of the combination
public static bool GetNextState(bool[] states)
{
for (var i = 0; i < states.Length; i++)
if ((states[i] = !states[i]) == true)
return false;
return true;
}
// Create the combination
public static string Join(string[] split, string[] matches, bool[] states)
{
var sb = new StringBuilder();
for (var i = 0; i < states.Length; i++)
sb.Append(split[i] + (states[i]? matches[i]:""));
sb.Append(split.Last());
return sb.ToString();
}
// enumerate the results
public static IEnumerable<string> Results(string input)
{
var matches = Regex.Matches(input, #"\(.+?\)")
.Select(x => x.Value.Trim('(', ')'))
.ToArray();
var split = Regex.Split(input, #"\(.+?\)");
var states = new bool[matches.Length];
do {
yield return Join(split, matches, states);
} while (!GetNextState(states));
}
Usage
string s = "a(b)cd(ef)";
foreach (var result in Results(s))
Console.WriteLine(result);
Output
acd
abcd
acdef
abcdef
Full Demo Here
Michael Randall's answer is correct, but after some more thinking I was finally able to come up with a (probably not very good) working solution as well
string s = "a(b)cd(ef)";
List<string> result = new List<string>();
int amount = s.Split('(').Length;
amount = (int)Math.Pow(2, amount - 1);
for (int i = 0; i < amount; i++)
{
string temp = string.Copy(s);
string binaryString = Convert.ToString(i, 2).PadLeft(amount, '0');
string tempResult = "";
for (int j = 0; j < binaryString.Length && !temp.Equals(""); j++)
{
Regex regex = new Regex(#"[^(]*");
tempResult += regex.Match(temp).Value;
if (binaryString[binaryString.Length - 1 - j].Equals('1'))
{
string regexStr = #"\(([^)]*)\)";
regex = new Regex(regexStr);
tempResult += regex.Match(temp).Value;
}
if (temp.Contains(')'))
temp = temp.Substring(temp.IndexOf(')') + 1);
else temp = "";
}
result.Add(tempResult.Replace("(", "").Replace(")", ""));
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I want to pick class names of all classes from given input code. However my code is picking just one class name. What should I do?
I am working on a Windows form in C#. I have tried to it do with a foreach loop but it is not working
private void btnDetect_Click(object sender, EventArgs e)
{
// splitting code
string mystr = richTextBox1.Text;
if (mystr.ToLower().Contains("class"))
{
string[] splitText = mystr.Split(new char[] { ' ' });
foreach (string word in splitText)
{
int classindex = Array.FindIndex(splitText, r => r.Contains("class"));
string className = splitText[classindex + 1];
MessageBox.Show(className);
}
}
else
MessageBox.Show("class not found");
}
}
I expect the output to show all the class names in the input, but the output I get is only the first class name
Array.FindIndex always returns the first index because the condition never changes.
You could use:
string[] tokens = mystr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
List<string> classList = new List<string>();
for (int i = 0; i < tokens.Length - 1; i++)
{
if (tokens[i].Equals("class", StringComparison.InvariantCultureIgnoreCase))
classList.Add(tokens[++i]);
}
you could use this method :
public IEnumerable<string> GetClasses(string str)
{
if (!str.Contains("Class", StringComparison.OrdinalIgnoreCase)) yield break;
string[] words =str.Split(' ', StringSplitOptions.RemoveEmptyEntries);
int wordsCount = words.Length;
for (var index = 0; index < wordsCount; index++)
{
string word = words[index];
if (!word.Equals("Class", StringComparison.OrdinalIgnoreCase)) continue;
if (wordsCount > index + 1) continue;
if (words[index + 1].Equals("Class", StringComparison.OrdinalIgnoreCase)) continue;
yield return words[index + 1];
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a requirement to find and extract a number contained within a string.
For example, from these strings:
"O:2275000 BF:3060000 D:3260000 E:3472000 I:3918000 T:4247000 UF:4777000 A:4904000 AD:5010000 X:5243000 G:21280000"
extract :
1.2275000
2.3060000
3.3260000
....
It would be :
string temp = yourText;
List<int> numbers = new List<int>();
Regex re = new Regex(#"\d+");
Match m = re.Match(temp);
while (m.Success)
{
numbers.Add(Convert.ToInt32(m.Value));
temp = temp.Substring(m.Index + m.Length);
m = re.Match(temp);
}
First of all, you mentioned "from these strings", though you gave a single string. I am not clear about this part.
Secondly, what do you mean by extract? Do you want to find the position of a number in the string? If yes then you can simply use string search as following
string str = "O:2275000 BF:3060000 D:3260000";
int index = str.IndexOf("3060000");
if (index != -1)
{
Console.WriteLine(index);
}
else
{
Console.WriteLine("Not Found");
}
Or if the problem is stated like that: you were given a string and you want to extract the numbers out of it, then you can do it like so:
List<decimal> findNumbers(string str)
{
List<decimal> x = new List<decimal>();
string tokens = "";
foreach (char ch in str)
{
if (Char.IsNumber(ch))
{
tokens = tokens + ch;
}
if (!Char.IsNumber(ch) && !String.IsNullOrEmpty(tokens))
{
decimal num = Convert.ToDecimal(tokens);
x.Add(Convert.ToDecimal(num));
tokens = "";
}
}
if (String.IsNullOrEmpty(tokens))
{
x.Add(Convert.ToDecimal(tokens));
}
return x;
}
this function returns the list of numbers available in the string.
We can try using string split here:
string input = "O:2275000 BF:3060000 D:3260000";
string[] parts = input.Split(' ');
string[] numbers = parts
.Select(s => s.Split(':')[1])
.ToArray();
foreach (string n in numbers)
{
Console.WriteLine(n);
}
This prints:
2275000
3060000
3260000
You can do this in a single line with Linq.
string numbers = "O:2275000 BF:3060000 D:3260000 E:3472000 I:3918000 T:4247000 UF:4777000 A:4904000 AD:5010000 X:5243000 G:21280000";
List<int> list = numbers.Split(' ').Select(x => Convert.ToInt32(string.Concat(x.Where(Char.IsDigit)))).ToList();
Tim Biegeleisen's answer is correct except it does not produce floating point output as you mentioned in your question. In his answere, just replace foreach loop with for statement, like this:
string input = "O:2275000 BF:3060000 D:3260000";
string[] parts = input.Split(' ');
string[] numbers = parts
.Select(s => s.Split(':')[1])
.ToArray();
for(int i = 0; i < numbers.Length; i++)
{
Console.WriteLine("{0}.{1}", i+1, numbers[i]);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
What I need is to get the common part between two words and get the differences too.
Examples:
Scenario 1
word1 = Testimonial
word2 = Test
Will return
Common part Test, difference imonial
Scenario 2
word1 = Test
word2 = Testimonial
Will return
Common part Test, difference imonial
Scenario 3
word1 = Testimonial
word2 = Tesla
Will return
Common part Tes, difference timonial and la
The common part of both words are always on the beginning.
In other words, I need to preserve the begin of the word until the words get different, than I need to get the differences.
I'm trying to do that avoid using a lot of if's and for's.
Thank you guys.
LINQ alternative:
string word1 = "Testimonial", word2 = "Tesla";
string common = string.Concat(word1.TakeWhile((c, i) => c == word2[i]));
string[] difference = { word1.Substring(common.Length), word2.Substring(common.Length) };
class Program
{
static void Main(string[] args)
{
string word1 = "Testimonial";
string word2 = "Tesla";
string common = null;
string difference1 = null;
string difference2 = null;
int index = 0;
bool same = true;
do
{
if (word1[index] == word2[index])
{
common += word1[index];
++index;
}
else
{
same = false;
}
} while (same && index < word1.Length && index < word2.Length);
for (int i = index; i < word1.Length; i++)
{
difference1 += word1[i];
}
for (int i = index; i < word2.Length; i++)
{
difference2 += word2[i];
}
Console.WriteLine(common);
Console.WriteLine(difference1);
Console.WriteLine(difference2);
Console.ReadLine();
}
}
You can use Intersect and Except to get it:
static void Main(string[] args)
{
var string1 = "Testmonial";
var string2 = "Test";
var intersect = string1.Intersect(string2);
var except = string1.Except(string2);
Console.WriteLine("Intersect");
foreach (var r in intersect)
{
Console.Write($"{r} ");
}
Console.WriteLine("Except");
foreach (var r in except)
{
Console.Write($"{r} ");
}
Console.ReadKey();
}
Note that this is a simple solution. For example:
Except would not work if you change the order of the strings, like :
"Test".Except("Testmonial");
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this code:
List<string> dirParts = new List<string>();
int index = 0;
for (int i = 0; i < dirName.Length; i++)
{
index = dirName.IndexOf("/");
string dironly = dirName.Substring(index, dirName.Length - index);
dirParts.Add(dironly);
}
dirName for example contains d/1/2/3/4/5
So I need that in the List dirParts in the end I will have in index 0 d, in index 1 d/1, in index 2 d/1/2, in index 3 d/1/2/3, in index 4 d/1/2/3/4, in index 5 d/1/2/3/4/5
So when I look on the List it should look like this:
d
d/1
d/1/2
d/1/2/3
d/1/2/3/4
d/1/2/3/4/5
Possible implementation:
List<string> dirParts = new List<string>();
int index = -1;
while (true) {
index = dirName.IndexOf("/", index + 1);
if (index < 0) {
dirParts.Add(dirName);
break;
}
else
dirParts.Add(dirName.Substring(0, index));
}
Apart from the fact that this kind of path manipulation is best left to the library, lest your code breaks on strange exceptional directory names, your code works as it does because you look everytime for the same "/".
You should search after the last one:
index = dirName.IndexOf("/", index+1);
If you really want to do it that way:
for (int i = 0; i < dirName.Length; i++) {
index = dirName.IndexOf("/", index);
if (index == -1)
i = index = dirName.Length; //last one
dirParts.Add(dirName.Substring(0, index++));
}
If it's a directory, I like to use Path library personally. The only issue would be the use of / over \ in directory name, so you'd have to call .Replace before inserting:
var orig = "d/1/2/3/4/5"; // Original string
List<String> dirParts = new List<String>(new[]{ orig }); // start out with list of original
// Get the directory below this one. (drop off last folder)
String part = Path.GetDirectoryName(orig);
while (!String.IsNullOrEmpty(part))
{
// add it to the first entry in the list
dirParts.Insert(0, part.Replace('\\','/'));
// get the next parent folder.
part = Path.GetDirectoryName(part);
}
/* dirParts = [ "d", "d/1", "d/1/2", ... ] */
However, if you're just looking for the down and dirty LINQ approach:
var orig = "d/1/2/3/4/5";
String[] parts = orig.Split('/');
var dirParts = Enumerable.Range(1, parts.Length)
.Select (e => String.Join("/", parts.Take(e)));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this array, for example (the size is variable):
x = ["10111", "10122", "10250", "10113"]
I need to find the longest string that is a substring of each array element ("10" in this case) (it need not to be a prefix of the strings). I have to remove it from all the strings. The output for this example would be:
x=["111","222","250","113"] //common value = "10"
This extension finds the longest most common substring(s). Note that "1" is also contained in every string even more often than "10". (C# only):
public static class StringExtensions
{
public static IEnumerable<string> GetMostCommonSubstrings(this IList<string> strings)
{
if (strings == null)
throw new ArgumentNullException("strings");
if (!strings.Any() || strings.Any(s => string.IsNullOrEmpty(s)))
throw new ArgumentException("None string must be empty", "strings");
var allSubstrings = new List<List<string>>();
for (int i = 0; i < strings.Count; i++)
{
var substrings = new List<string>();
string str = strings[i];
for (int c = 0; c < str.Length - 1; c++)
{
for (int cc = 1; c + cc <= str.Length; cc++)
{
string substr = str.Substring(c, cc);
if (allSubstrings.Count < 1 || allSubstrings.Last().Contains(substr))
substrings.Add(substr);
}
}
allSubstrings.Add(substrings);
}
if (allSubstrings.Last().Any())
{
var mostCommon = allSubstrings.Last()
.GroupBy(str => str)
.OrderByDescending(g => g.Key.Length)
.ThenByDescending(g => g.Count())
.Select(g => g.Key);
return mostCommon;
}
return Enumerable.Empty<string>();
}
}
Now it's easy:
string[] x = new[] { "10111", "10122", "10250", "10113" };
string mostCommonSubstring = x.GetMostCommonSubstrings().FirstOrDefault();
if (mostCommonSubstring != null)
{
for (int i = 0; i < x.Length; i++)
x[i] = x[i].Replace(mostCommonSubstring, "");
}
Console.Write(string.Join(", ", x));
output:
111, 122, 250, 113
DEMO
Edit: If you just want to find the longest common substring without taking the frequency of occurrence into account you can use this optimzed approach(O(n) operation) using a HashSet<string>:
public static string GetLongestCommonSubstring(this IList<string> strings)
{
if (strings == null)
throw new ArgumentNullException("strings");
if (!strings.Any() || strings.Any(s => string.IsNullOrEmpty(s)))
throw new ArgumentException("None string must be empty", "strings");
var commonSubstrings = new HashSet<string>(strings[0].GetSubstrings());
foreach (string str in strings.Skip(1))
{
commonSubstrings.IntersectWith(str.GetSubstrings());
if (commonSubstrings.Count == 0)
return null;
}
return commonSubstrings.OrderByDescending(s => s.Length).First();
}
public static IEnumerable<string> GetSubstrings(this string str)
{
if (string.IsNullOrEmpty(str))
throw new ArgumentException("str must not be null or empty", "str");
for (int c = 0; c < str.Length - 1; c++)
{
for (int cc = 1; c + cc <= str.Length; cc++)
{
yield return str.Substring(c, cc);
}
}
}
Use it in this way:
string[] x = new[] { "101133110", "101233210", "102533010", "101331310" };
string longestCommon = x.GetLongestCommonSubstring(); // "10"
Try this: (I suppose the common string should be at the beginning):
string[] x = {"10111","10222","10250","10113"};
string common = x[0];
foreach(var i in x){
while(!i.StartsWith(common)){
common = common.Substring(0,common.Length-1);
if(common == "") break;
}
}
x = x.Select(a=>a.Substring(common.Length)).ToArray();
Find the maximum number of times a substring of length 1 appears. This is a simple O(n^2) search. Call this maximum number of occurrence K.
In your example, this is "1", "0", and K=5.
Now you know that all substrings of length 2 cannot appear in more than K input strings. Also, any substring that occurs K times must be made of the length 1 substrings that occured K times. Search the length 1 substrings for substrings of length 2 that exist K times, this is again a simple O(n^2) search.
Repeat for longer lengths until no more substrings exist in K inputs.