Search Array and get substring result [closed] - c#

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
I have a string array as follows:
string[] stringArray = { "1122|false", "1123|true", "1124|true", "1125|false" };
In essence, it is broken down by id|active where for example id is say 1122 and active is true or false.
Say my id was 1123, how would I search this array to get the value of true in this case? I understand Substring needs to be used with IndexOf but not sure how to tie it together.

Little bit of LINQ and String.Split should do the trick.
string[] stringArray = { "1122|false", "1123|true", "1124|true", "1125|false" };
int id = 1123;
var itemWithGivenId = stringArray
.SingleOrDefault(s => int.Parse(s.Split('|')[0]) == id);
Console.WriteLine(bool.Parse(itemWithGivenId.Split('|')[1]));

Related

C# switch case Array contains string [closed]

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 4 years ago.
Improve this question
Is it possible to look up if a string in contained in an Array, with a switch case?
string text = "blalbac";
string arr[] = {"a","b","c"};
switch (text)
{
case arr.Contains(filename):
//do..
break;
}
I’m not sure what you’re trying to do. Do you want something like
foreach(string item in arr)
{
if(text.Contains(item))
{
...
}
}

How to concatenate two values into one [closed]

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 4 years ago.
Improve this question
I want to concatenate two combo boxes value into 1 column of GridView in C#. I am using this code but it not concatenate it..
private void linetotal_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode==Keys.Enter)
{
object[] values = { cbxclassstd.Text,cbxfee.Text,
feeamount.Text,disc.Text,linetotal.Text};
this.dgvvoucher.Rows.Add(values);
}
}
Use string.Join to concatenate your Combobox's values into one column:
this.dgvvoucher.Rows.Add(string.Join("", values));//Or string.Join(",", values)
And if you just need to concatenate first two items:
this.dgvvoucher.Rows.Add(string.Join("", new[] { values[0] , values[1] }));

Regex find duplicate values in this set [closed]

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 5 years ago.
Improve this question
I have an list of string values which looks like this:
GREEN,BLUE,BLUE
BLUE,BLUE,GREEN
GREEN,RED,RED
RED,BLUE,BLUE
BLUE,RED,RED
GREEN,BLUE,BLUE
RED,GREEN,BLUE
I will use a foreach to loop through each line and find unique values.
I need a regex that returns true is there are no color duplicates (RED,GREEN,BLUE) and false if there are color duplicates (RED,GREEN,RED).
What would the regex look like?
You can try using Linq instead of regular expressions:
using System.Linq;
...
string source = "BLUE,BLUE,GREEN";
// do we have three distinct items?
bool allDistinct = source.Split(',').Distinct().Count() >= 3;
Test:
List<string> list = new List<string>() {
"GREEN,BLUE,BLUE",
"BLUE,BLUE,GREEN",
"GREEN,RED,RED",
"RED,BLUE,BLUE",
"BLUE,RED,RED",
"GREEN,BLUE,BLUE",
"RED,GREEN,BLUE",
};
var result = list
.Select(source => $"{source,-15} {source.Split(',').Distinct().Count() >= 3}");
Console.Write(string.Join(Environment.NewLine, result));
Outcome:
GREEN,BLUE,BLUE False
BLUE,BLUE,GREEN False
GREEN,RED,RED False
RED,BLUE,BLUE False
BLUE,RED,RED False
GREEN,BLUE,BLUE False
RED,GREEN,BLUE True
Edit: Linq can help out in the generalized case:
bool allDistinct = !source
.Split(',')
.GroupBy(item => item, (k, s) => s.Skip(1).Any())
.Any(item => item);

C# Multi Spintax - Spin Text [closed]

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 9 years ago.
Improve this question
I got this class that I'm using for spin text.
public class Spinner
{
private static Random rnd = new Random();
public static string Spin(string str)
{
string regex = #"\{(.*?)\}";
return Regex.Replace(str, regex, new MatchEvaluator(WordScrambler));
}
public static string WordScrambler(Match match)
{
string[] items = match.Value.Substring(1, match.Value.Length - 2).Split('|');
return items[rnd.Next(items.Length)];
}
}
But I need it to be able to spin multi spintax text.
As example
{1|2} - {3|4}
Returns: 2 - 4
So it works.
But:
{{1|2}|{3|4}} - {{5|6}|{7|8}}
Returns: 2|4} - {5|7}
So it doesn't work if there is spintax inside spintax.
Any help? :)
Regular expressions are not good in dealing with nested structures, which means you should probably try a different approach.
In your example, {{1|2}|{3|4}} - {{5|6}|{7|8}} is the same as {1|2|3|4} - {5|6|7|8}, so maybe you don't need nested spintax.

LINQ- Select linq from string array [closed]

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 9 years ago.
Improve this question
How apply linq select to string array in C#
ex :
string[] result;
...
result.Select(..)
?
Thanks.
You pass in a lambda function that tells the system what you want to do with each string.
string[] result;
...
var newList = result.Select(s => {do something with s});
The function can do most anything that takes a string as an input and returns a value - it doesn't even have to return a string! For example, if the strings contained numeric characters, you could return a collection of numbers:
IEnumerable<int> newList = result.Select(s => int.Parse(s));
Note that the original array will not be changed.

Categories

Resources