get values from array and add into one string - c#

Object Book has Author which has property Name of type string.
I want to iterate trough all Authors and add it's Name string to the one string (not array) separated by comma, so this string should be at the as
string authorNames = "Author One, Author two, Author three";
string authorNames = string.Empty;
foreach(string item in book.Authors)
{
string fetch = item.Name;
??
}

You can use the string.Join function with LINQ
string authorNames = string.Join(", ", book.Authors.Select(a => a.Name));

You can use
string authors = String.Join(", ", book.Authors.Select(a => a.Name));

LINQ is the way to go in C#, but for explanatory purposes here is how you could code it explicitly:
string authorNames = string.Empty;
for(int i = 0; i < book.Authors.Count(); i++)
{
if(i > 0)
authorNames += ", ";
authorNames += book.Authors[i].Name;
}

You could also loop through them all, and append them to authorNames and add a comma in the end, and when it's done simply trim of the last comma.
string authorNames = string.Empty;
foreach(string author in book.Authors)
{
string authorNames += author.Name + ", ";
}
authorNames.TrimEnd(',');

Using LinQ, there are plenty of ways to merge multiple string into one string.
book.Authors.Select(x => x.Name).Aggregate((x, y) => x + ", " + y);
To anwsers James' comment
[TestMethod]
public void JoinStringsViaAggregate()
{
var mystrings = new[] {"Alpha", "Beta", "Gamma"};
var result = mystrings.Aggregate((x, y) => x + ", " + y);
Assert.AreEqual("Alpha, Beta, Gamma", result);
}

Related

Linq lambda foreach

Trying for wrap each string in array but it doesn't works, means foreach loop, please explain why
string s = "keepsakes,table runners,outdoor accessories";
List<string> keys = s.Split(',').ToList();
keys.ForEach(x => x = String.Concat("%", x, "%"));
s = String.Join(",", keys);
Console.WriteLine(s);
need to get "%keepsakes%,%table runners%,%outdoor accessories%"
UPD:
Thanks a lot for suggestions(it's a same way)
but some one can answer why this is works and not works under:
object
public class MyItems
{
public string Item { get; set; }
}
and func
string s = "keepsakes,table runners,outdoor accessories";
List<MyItems> keys = s.Split(',').ToList().Select(x => new MyItems(){ Item = x }).ToList();
keys.ForEach(x => x.Item = String.Concat("%", x.Item, "%"));
s = String.Join(",", keys.Select(x => x.Item).ToList());
Console.WriteLine(s);
You are not modifying the list within the ForEach, you are just creating strings that are assigned to the local variable x but then thrown away. You could use a for-loop:
for(int i = 0; i < keys.Count; i++)
{
keys[i] = String.Concat("%", keys[i], "%");
}
For what it's worth, here the shorter LINQ version which also circumvents the core issue:
s = string.Join(",", s.Split(',').Select(str => "%" + str + "%"));
You can use Join and Select and Format
string s = "keepsakes,table runners,outdoor accessories";
var output = string.Join(",", s.Split(',').Select(x => string.Format("%{0}%", x)));
you can do easier: replace each comma with %,%
string s = "keepsakes,table runners,outdoor accessories";
string s2 = "%" + s.Replace("," , "%,%") + "%";
Another approach could be using Regex instead of LINQ:
string s = "keepsakes,table runners,outdoor accessories";
string pattern = "\\,+";
string replacement = "%,";
Regex rgx = new Regex(pattern);
string result = string.Format("%{0}%", rgx.Replace(s, replacement));
Edit:
The reason why it works using a class to assign the string it is because when you use the foreach in your first instance:
keys.ForEach(x => x = String.Concat("%", x, "%"));
x, elements of keys, which is a string, is a reference passed by value to the function ForEach. Take this as an example:
var myString = "I'm a string";
Console.WriteLine(myString);
ChangeValue(myString);
Console.WriteLine(myString);
void ChangeValue(string s)
{
s = "something else";
}
If you run that snippet you'll see that myString won't be changed inside the method ChangeValue because we are trying to replace the reference. The same thing happens for the method ForEach, this is the main reason you cannot change the value of your list within the ForEach.
Instead if you do:
class MyClass{
public string aString;
}
void ChangeValue(MyClass s)
{
s.aString = "something else";
}
var myClass = new MyClass();
myClass.aString = "I'm a string";
Console.WriteLine(myClass.aString);
ChangeValue(myClass);
Console.WriteLine(myClass.aString);
You acknowledge that in the second Console.WriteLine the value of the field aString will be changed to "something else". Here is a good explanation of how reference types are passed by value
Just another approach (without lambdas):
string result = string.Concat("%", s, "%").Replace(",", "%,%");
List<>.ForEach cannot be used to change the contents of the list. You can either create a new list or use a for loop.
keys = keys.Select(x => "%" + x + "%").ToList();
or
for(int i = 0; i < keys.Count; i++)
{
keys[i] = "%" + keys[i] + "%";
}
As others have noted, the lambda passed to List.ForEach does not return a value.
LINQ is lazy, but String.Join will force enumeration:
var res = String.Join(",", input.Split(',').Select(s => "%" + s + "%"));
The ForEach doesn't change your list of string, it will only perform an action using each string. Instead you can do it this way :
string s = "keepsakes,table runners,outdoor accessories";
List<string> keys = s.Split(',').Select(x => String.Concat("%", x, "%")).ToList();
s = String.Join(",", keys);
Console.WriteLine(s);

How to remove comma separated value from a string?

I want to remove a comma separated value from the string..
suppose I have a string like this
string x="r, v, l, m"
and i want to remove r from the above string, and reform the string like this
string x="v, l, m"
from the above string i want to remove any value that my logic throw and reform the string.
it should remove the value and comma next to it and reform the string...
The below is specific to my code..
I want to remove any value that I get from the logic, I want to remove it and comma next to it and reform the string with no empty space on the deleted item.. How can I achieve this?
offIdColl = my_Order.CustomOfferAppliedonOrder.TrimEnd(',');
if (offIdColl.Split(',').Contains(OfferID.ToString()))
{
// here i want to perform that operation.
}
Tombala, i applied it like this but it doesn't work..it returns true
if (!string.IsNullOrEmpty(my_Order.CustomOfferAppliedonOrder))
{
offIdColl = my_Order.CustomOfferAppliedonOrder.TrimEnd(',');
if (offIdColl.Split(',').Contains(OfferID.ToString()))
{
string x = string.Join(",", offIdColl.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries).ToList().Remove(OfferID.ToString()));
}
}
}
Just do something like:
List<String> Items = x.Split(",").Select(i => i.Trim()).Where(i => i != string.Empty).ToList(); //Split them all and remove spaces
Items.Remove("v"); //or whichever you want
string NewX = String.Join(", ", Items.ToArray());
Something like this?
string input = "r,v,l,m";
string output = String.Join(",", input.Split(',').Where(YourLogic));
bool YourLogic(string x)
{
return true;
}
var l = x.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
l.Remove(OfferID.ToString());
x = string.Join(",", l);
Edit: Sorry, you're right. Remove doesn't return the original list. You need multiple statements. But you don't need to trim the end "," implicitly. You can remove that statement from your code as well as the check to see if the item is there or not. The Remove will take it out if it was found or simply return false if it was not found. You don't have to check existence. So remove the TrimEnd from the first and get rid of the second line below:
offIdColl = my_Order.CustomOfferAppliedonOrder; //.TrimEnd(',');
//if (offIdColl.Split(',').Contains(OfferID.ToString()))
Not quite sure if this is what you mean, but this seems simplest and most readable:
string x = "r, v, l, m";
string valueToRemove = "r";
var result = string.Join(", ", from v in x.Split(',')
where v.Trim() != valueToRemove
select v);
Edit: like Bob Sammers pointed out, this only works in .NET 4 and up.
String input = "r, v, l, m, ";
string itemToReplace = "v, ";
string output = input.Replace(itemToReplace, string.Empty)
public void string Remove(string allStuff, string whatToRemove)
{
StringBuilder returnString = new StringBuilder();
string[] arr = allStuff.Split('');
foreach (var item in arr){
if(!item.Equals(whatToRemove)){
returnString.Append(item);
returnString.Append(", ");
}
}
return returnString.ToString();
}
So you want to delete an item (or replace it with a nother value) and join the string again with comma without space?
string x = "r, v, l, m,";
string value = "v";
string[] allVals = x.TrimEnd(',').Split(new []{','}, StringSplitOptions.RemoveEmptyEntries);
// remove all values:
x = string.Join(",", allVals.Where(v => v.Trim() != value));
// or replace these values
x = string.Join(",", allVals.Select(v => v.Trim() == value ? "new value" : v));
// If you want to remove ALL occurences of the item, say "a" you can use
String data = "a, b, c, d, a, e, f, q, a";
StringBuilder Sb = new StringBuilder();
foreach (String item in data.Split(',')) {
if (!item.Trim().Equals("a", StringComparison.Ordinal)) {
if (Sb.Length > 0)
Sb.Append(',');
Sb.Append(item);
}
}
data = Sb.ToString();
Its just single line of code in many ways, two of them are below:
string x = "r,v,l,m";
string NewX = String.Join(",", from i in x.Split(',') where i != String.Empty && i != "v" select i);
OR
string NewX = String.Join(",", x.Split(',').Select(i => i.Trim()).Where(i => i != String.Empty && i != "v"));
Not going about this right. Do you need to keep the string? I doubt you do. Just use a list instead. Can you have duplicates? If not:
offIdColl = my_Order.CustomOfferAppliedonOrder.TrimEnd(',').Split(',');
if (offIdColl.Contains(OfferID.ToString()))
{
offIdColl.Remove(OfferID.ToString());
}

Join rest of string array

private static string SetValue(string input, string reference)
{
string[] sentence = input.Split(' ');
for(int word = 0; word<sentence.Length; word++)
{
if (sentence[word].Equals(reference, StringComparison.OrdinalIgnoreCase))
{
return String.Join(" ", sentence.subarray(word+1,sentence.Length))
}
}
}
How can I accomplish sentence.subarray(word+1,sentence.Length) easily or do this in another way?
String.Join has an overload specifically for this:
return String.Join(" ", sentence, word + 1, sentence.Length - (word + 1));
You can use an overload Where with index:
return string.Join(" ", sentence.Where((w, i) => i > word));
If you are strictly looking for a Subarray solution independent of the string.Join() function, and you are using a version of .NET with Linq support, then may I recommend:
sentence.Skip(word + 1);
Alternatively, you could use SkipWhile instead of your for loop.
private static string SetValue(string input, string reference)
{
var sentence = input.Split(" ");
// Skip up to the reference (but not the reference itself)
var rest = sentence.SkipWhile(
s => !s.Equals(reference, StringComparison.OrdinalIgnoreCase));
rest = rest.Skip(1); // Skip the reference
return string.Join(" ", rest);
}

Replace the first occurrence in a string

I have this string :
Hello my name is Marco
and I'd like to replace the first space (between Hello and my) with <br />. Only the first.
What's the best way to do it on C#/.NET 3.5?
public static class MyExtensions
{
public static string ReplaceFirstOccurrance(this string original, string oldValue, string newValue)
{
if (String.IsNullOrEmpty(original))
return String.Empty;
if (String.IsNullOrEmpty(oldValue))
return original;
if (String.IsNullOrEmpty(newValue))
newValue = String.Empty;
int loc = original.IndexOf(oldValue);
return original.Remove(loc, oldValue.Length).Insert(loc, newValue);
}
}
and use it like:
string str="Hello my name is Marco";
str.ReplaceFirstOccurrance("Hello", "<br/>");
str.ReplaceFirstOccurrance("my", "<br/>");
No need to add substrings, following will find the first space instance only.
From MSDN:
Reports the zero-based index of the first occurrence of the specified
string in this instance.
string x = "Hello my name is Marco";
int index = x.IndexOf(" ");
if (index >= 0)
{
x=x.Remove(index,1);
x = x.Insert(index, #"<br />");
}
Edit: If you are not sure if space will occur, some validations must come into place. I have edit the answer accordingly.
string tmp = "Hello my name is Marco";
int index = tmp.IndexOf(" ");
tmp = tmp.Substring(0, index) + "<br />" + tmp.Substring(index + 1);
Here you go, this would work:
var s = "Hello my name is Marco";
var firstSpace = s.IndexOf(" ");
var replaced = s.Substring(0,firstSpace) + "<br/>" + s.Substring(firstSpace+1);
You could make this into an extension method:
public static string ReplaceFirst(this string input, string find, string replace){
var first= s.IndexOf(find);
return s.Substring(0,first) + replace + s.Substring(first+find.Length);
}
And then the usage would be
var s = "Hello my name is Marco";
var replaced = s.ReplaceFirst(" ","<br/>");
string[] str = "Hello my name is Marco".Split(' ');
string newstr = str[0] + "<br /> " + string.Join(" ", str.Skip(1).ToArray());
Simply use
Replace("Hello my name is Marco", " ", "<br />", 1, 1)

Merging strings together with separator berween them in C#

I needed to merge strings that are inside List<string> together into oneliner. I came up with simple solution but I am not sure if it's the best way to go.
First version with problematic , on string start:
string benchmarkiUjemneDatyRazem = "";
foreach (string s in benchmarkiUjemne) {
benchmarkiUjemneDatyRazem = benchmarkiUjemneDatyRazem + "," + s;
}
Second version (Linq power) but still with ` :
string benchmarkiUjemneDatyRazem = benchmarkiUjemne.Aggregate("", (current, s) => current + "," + s);
Working version without , but amount of lines makes some pain in later reading it:
int b = 0;
string benchmarkiUjemneDatyRazem = "";
foreach (string s in benchmarkiUjemne) {
if (b == 0) {
b = 1;
benchmarkiUjemneDatyRazem = s;
continue;
}
benchmarkiUjemneDatyRazem = benchmarkiUjemneDatyRazem + "," + s;
}
Final version that I came up with was based on Linq with Subsitute to first char:
string benchmarkiUjemneDatyRazem = benchmarkiUjemne.Aggregate("", (current, s) => current + "," + s).Substring(1);
Is this good approach to this problem ? Or there's better way to actually do it? Like using StringBuilder or so?
If you're using .Net 4, you can use string.Join (in earlier versions this will work only if benchmarkiUjemne is a string[]):
string result = string.Join(",", benchmarkiUjemne);
If this is .Net 3.5 or older, you can still use it by calling ToArray on the list:
string result = string.Join(",", benchmarkiUjemne.ToArray());
Use string.Join:
var res = string.Join(",", benchmarkiUjemne);

Categories