I had a list of strings
var li = new List<string>()
{
"word1", "word2", "word3", "word4", "word5", "word6",
};
I want to concatinate some some character in each word output list will be will be
word1#,word2#,word3#,word4#,word5#,word6#
How about doing this:
li = li.Select(x => x + "#").ToList();
If you mean you want to output the string "word1#,word2#,word3#,word4#,word5#,word6#", then, after the above line, do this:
var output = String.Join(",", li);
Try this
var word = string.Join("#,",li)+"#";
It will join your strings separated with #,
Related
I have some code as below:
char[] separator = { ',' };
String[] idList = m_idsFromRequest.Split(separator); //m_idsFromRequest="1,2....";
List<string> distinctIdList = idList.Distinct().ToList();
m_distinctIdList = distinctIdList.ToArray();
m_idsFromRequest = string.Join(" ", distinctIdList);
currently m_idsFromRequest = ["1","2".........."] is as such.
I want to make it ["0|1","0|2".........."] like append "0|" in each element . I wanted to know id I could do it without the foreach loop.
You can use Select and String.Join:
var idsPrependedWithZero = m_idsFromRequest.Split(separator)
.Distinct() // same as in your code
.Select(id => $"0|{id}");
string result = string.Join(",", idsPrependedWithZero);
char[] separator = { ',' };
string m_idsFromRequest = "1,2,3,4,5,6";
String[] idList = m_idsFromRequest.Split(separator); //m_idsFromRequest="1,2....";
List<string> distinctIdList = idList.Distinct().Select(t => "0|" + t).ToList();
m_idsFromRequest = string.Join(",", distinctIdList);
Just added the .Select() after .Distinct(). Inside the .Select you can transform each of the item in the list.
Apart from that i also added "," in your string.Join. Because you want it to be join by comma.
I have two strings consisting of several words (Comma separated). If string 1 consisting of words (; separated) which is to be ignored if it presents in string 2, below are the strings
var compToIgnore="Samsung,Motorola,Amazon";
var allCompanies="Godrej,Samsung,Videocon,Huawei,Motorola,Alibaba,Amazon";
var icm = compToIgnore.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var c in listOfCompanies)
{
if (c.BrandId.StartsWith("A2"))
{
var item = allCompanies.Where(x => !icm.Contains(x));
var result = string.Join(",", item);
}
}
In result variable I wanted to have final result as "Godrej,Videocon,Huawei,Alibaba"
I don't see any proper result with above code
You can try Linq in order to query allCompanies:
using System.Linq;
...
var compToIgnore = "Samsung,Motorola,Amazon";
var allCompanies = "Godrej,Samsung,Videocon,Huawei,Motorola,Alibaba,Amazon";
string result = string.Join(",", allCompanies
.Split(',')
.Except(compToIgnore.Split(',')));
Note, that Except removes all duplicates. If you want to preserve them, you can use HashSet<string> and Where instead of Except:
HashSet<string> exclude = new HashSet<string>(compToIgnore.Split(','));
string result = string.Join(",", allCompanies
.Split(',')
.Where(item => !exclude.Contains(item)));
Try following :
string compToIgnore = "Samsung,Motorola,Amazon";
string allCompanies = "Godrej,Samsung,Videocon,Huawei,Motorola,Alibaba,Amazon";
string[] icm = compToIgnore.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
string[] ac = allCompanies.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
string results = string.Join(",", ac.Where(x => !icm.Contains(x)));
I am new to programming languages. I have a requirement where I have to return a record based on a search string.
For example, take the following three records and a search string of "Cal":
University of California
Pascal Institute
California University
I've tried String.Contains, but all three are returned. If I use String.StartsWith, I get only record #3. My requirement is to return #1 and #3 in the result.
Thank you for your help.
If you're using .NET 3.5 or higher, I'd recommend using the LINQ extension methods. Check out String.Split and Enumerable.Any. Something like:
string myString = "University of California";
bool included = myString.Split(' ').Any(w => w.StartsWith("Cal"));
Split divides myString at the space characters and returns an array of strings. Any works on the array, returning true if any of the strings starts with "Cal".
If you don't want to or can't use Any, then you'll have to manually loop through the words.
string myString = "University of California";
bool included = false;
foreach (string word in myString.Split(' '))
{
if (word.StartsWith("Cal"))
{
included = true;
break;
}
}
I like this for simplicity:
if(str.StartsWith("Cal") || str.Contains(" Cal")){
//do something
}
You can try:
foreach(var str in stringInQuestion.Split(' '))
{
if(str.StartsWith("Cal"))
{
//do something
}
}
You can use Regular expressions to find the matches. Here is an example
//array of strings to check
String[] strs = {"University of California", "Pascal Institute", "California University"};
//create the regular expression to look for
Regex regex = new Regex(#"Cal\w*");
//create a list to hold the matches
List<String> myMatches = new List<String>();
//loop through the strings
foreach (String s in strs)
{ //check for a match
if (regex.Match(s).Success)
{ //add to the list
myMatches.Add(s);
}
}
//loop through the list and present the matches one at a time in a message box
foreach (String matchItem in myMatches)
{
MessageBox.Show(matchItem + " was a match");
}
string univOfCal = "University of California";
string pascalInst = "Pascal Institute";
string calUniv = "California University";
string[] arrayofStrings = new string[]
{
univOfCal, pascalInst, calUniv
};
string wordToMatch = "Cal";
foreach (string i in arrayofStrings)
{
if (i.Contains(wordToMatch)){
Console.Write(i + "\n");
}
}
Console.ReadLine();
}
var strings = new List<string> { "University of California", "Pascal Institute", "California University" };
var matches = strings.Where(s => s.Split(' ').Any(x => x.StartsWith("Cal")));
foreach (var match in matches)
{
Console.WriteLine(match);
}
Output:
University of California
California University
This is actually a good use case for regular expressions.
string[] words =
{
"University of California",
"Pascal Institute",
"California University"
}
var expr = #"\bcal";
var opts = RegexOptions.IgnoreCase;
var matches = words.Where(x =>
Regex.IsMatch(x, expr, opts)).ToArray();
The "\b" matches any word boundary (punctuation, space, etc...).
I have a string that is like this:
Item1;Item2;Item3
but could also be
Item1
Is there a slick .net method to get that into a List?
string.split is your friend...
var yourString = "Item1;Item2;Item3";
var result = new List<string>(yourString.Split(';'));
LINQ has a way to bring the array to string, too:
var inputString = "item1;item2;item3";
var asList = inputString.Split( ';' ).ToList();
var input = "Item1;Item2;Item3";
var list = input.Split(new[] {";"}, StringSplitOptions.None).ToList();
Here's how I'd do that:
string[] arr = str.Split( new char[] { ';' } );
List<string> list = new List<string>( arr );
In C#
var parameters =
from line in parameterTextBox.Lines
select new {name = line.Split(' ').First(), value = line.Split(' ').Skip(1)};
Is there a way to do this without having to split twice?
you can store the split in a let clause
var parameters =
from line in parameterTextBox.Lines
let split = line.Split(' ')
select new {name = split.First(), value = split.Skip(1)};
Sure.
var parameters = from line in parameterTextBox.Lines
let words = line.Split(' ')
select new { name = words.First(), words.skip(1) };
string Str= "one all of the rest";
Match m = Regex.match(Str,"(\w*) (\w.*)");
string wordone = m.Groups[1];
string wordtwo = m.Groups[2];
You might try this:
private Dictionary<string, string> getParameters(string[] lines)
{
Dictionary<string, string> results = new Dictionary<string, string>();
foreach (string line in lines)
{
string pName = line.Substring(0, line.IndexOf(' '));
string pVal = line.Substring(line.IndexOf(' ') + 1);
results.Add(pName, pVal);
}
return results;
}