Concatenate Strings In For Each Loop
I am using two for each loops to concatenate values into ONE variable so I can write the value to Word in a Find/Replace method. My issue is that the below syntax will write the values to the Console twice so this is written to the Console
Shirt - SH11
Hat - HA22
Socks - SO33
Shirt - SH11
Hat - HA22
Shirt - SH11
What I desire to be written to the console is the values just once like the below:
Shirt - SH11
Hat - HA22
Socks - SH11
This is my syntax:
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("ItemsSold", typeof(string));
table.Columns.Add("ItemIDs", typeof(string));
table.Rows.Add("James Jo", "Shirt; Hat; Socks;", "SH11; HA22; SO33");
var itemsSold = table.Rows[0].Field<string>("ItemsSold").Split(new string {"; "}, StringSplitOptions.RemoveEmptyEntries);
var ItemIDs = table.Rows[0].Field<string>("ItemIDs").Split(new string {"; "}, StringSplitOptions.RemoveEmptyEntries);
string displayformat = "";
foreach (string is in itemsSold)
{
foreach (string id in ItemIDs)
{
string s = is.Replace(";", "");
string d = id.Replace(";", "");
displayformat += s + " - " + d + Environment.NewLine;
}
}
Console.WriteLine(displayformat);
c# linq
add a comment |
I am using two for each loops to concatenate values into ONE variable so I can write the value to Word in a Find/Replace method. My issue is that the below syntax will write the values to the Console twice so this is written to the Console
Shirt - SH11
Hat - HA22
Socks - SO33
Shirt - SH11
Hat - HA22
Shirt - SH11
What I desire to be written to the console is the values just once like the below:
Shirt - SH11
Hat - HA22
Socks - SH11
This is my syntax:
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("ItemsSold", typeof(string));
table.Columns.Add("ItemIDs", typeof(string));
table.Rows.Add("James Jo", "Shirt; Hat; Socks;", "SH11; HA22; SO33");
var itemsSold = table.Rows[0].Field<string>("ItemsSold").Split(new string {"; "}, StringSplitOptions.RemoveEmptyEntries);
var ItemIDs = table.Rows[0].Field<string>("ItemIDs").Split(new string {"; "}, StringSplitOptions.RemoveEmptyEntries);
string displayformat = "";
foreach (string is in itemsSold)
{
foreach (string id in ItemIDs)
{
string s = is.Replace(";", "");
string d = id.Replace(";", "");
displayformat += s + " - " + d + Environment.NewLine;
}
}
Console.WriteLine(displayformat);
c# linq
useitemsSold.Zip(ItemIDs, (l, r) => l + " - " + r + Environment.NewLine)
– RajN
Jan 2 at 0:36
@RajN wouldnt that return an IEnumerable ?
– Anu Viswan
Jan 2 at 0:49
Please read stackoverflow.com/questions/6638044/… to learn about constructing strings.
– Alexei Levenkov
Jan 2 at 3:36
add a comment |
I am using two for each loops to concatenate values into ONE variable so I can write the value to Word in a Find/Replace method. My issue is that the below syntax will write the values to the Console twice so this is written to the Console
Shirt - SH11
Hat - HA22
Socks - SO33
Shirt - SH11
Hat - HA22
Shirt - SH11
What I desire to be written to the console is the values just once like the below:
Shirt - SH11
Hat - HA22
Socks - SH11
This is my syntax:
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("ItemsSold", typeof(string));
table.Columns.Add("ItemIDs", typeof(string));
table.Rows.Add("James Jo", "Shirt; Hat; Socks;", "SH11; HA22; SO33");
var itemsSold = table.Rows[0].Field<string>("ItemsSold").Split(new string {"; "}, StringSplitOptions.RemoveEmptyEntries);
var ItemIDs = table.Rows[0].Field<string>("ItemIDs").Split(new string {"; "}, StringSplitOptions.RemoveEmptyEntries);
string displayformat = "";
foreach (string is in itemsSold)
{
foreach (string id in ItemIDs)
{
string s = is.Replace(";", "");
string d = id.Replace(";", "");
displayformat += s + " - " + d + Environment.NewLine;
}
}
Console.WriteLine(displayformat);
c# linq
I am using two for each loops to concatenate values into ONE variable so I can write the value to Word in a Find/Replace method. My issue is that the below syntax will write the values to the Console twice so this is written to the Console
Shirt - SH11
Hat - HA22
Socks - SO33
Shirt - SH11
Hat - HA22
Shirt - SH11
What I desire to be written to the console is the values just once like the below:
Shirt - SH11
Hat - HA22
Socks - SH11
This is my syntax:
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("ItemsSold", typeof(string));
table.Columns.Add("ItemIDs", typeof(string));
table.Rows.Add("James Jo", "Shirt; Hat; Socks;", "SH11; HA22; SO33");
var itemsSold = table.Rows[0].Field<string>("ItemsSold").Split(new string {"; "}, StringSplitOptions.RemoveEmptyEntries);
var ItemIDs = table.Rows[0].Field<string>("ItemIDs").Split(new string {"; "}, StringSplitOptions.RemoveEmptyEntries);
string displayformat = "";
foreach (string is in itemsSold)
{
foreach (string id in ItemIDs)
{
string s = is.Replace(";", "");
string d = id.Replace(";", "");
displayformat += s + " - " + d + Environment.NewLine;
}
}
Console.WriteLine(displayformat);
c# linq
c# linq
edited Jan 2 at 1:17
Tony Mathew
148111
148111
asked Jan 2 at 0:30
Doctor FordDoctor Ford
638
638
useitemsSold.Zip(ItemIDs, (l, r) => l + " - " + r + Environment.NewLine)
– RajN
Jan 2 at 0:36
@RajN wouldnt that return an IEnumerable ?
– Anu Viswan
Jan 2 at 0:49
Please read stackoverflow.com/questions/6638044/… to learn about constructing strings.
– Alexei Levenkov
Jan 2 at 3:36
add a comment |
useitemsSold.Zip(ItemIDs, (l, r) => l + " - " + r + Environment.NewLine)
– RajN
Jan 2 at 0:36
@RajN wouldnt that return an IEnumerable ?
– Anu Viswan
Jan 2 at 0:49
Please read stackoverflow.com/questions/6638044/… to learn about constructing strings.
– Alexei Levenkov
Jan 2 at 3:36
use
itemsSold.Zip(ItemIDs, (l, r) => l + " - " + r + Environment.NewLine)– RajN
Jan 2 at 0:36
use
itemsSold.Zip(ItemIDs, (l, r) => l + " - " + r + Environment.NewLine)– RajN
Jan 2 at 0:36
@RajN wouldnt that return an IEnumerable ?
– Anu Viswan
Jan 2 at 0:49
@RajN wouldnt that return an IEnumerable ?
– Anu Viswan
Jan 2 at 0:49
Please read stackoverflow.com/questions/6638044/… to learn about constructing strings.
– Alexei Levenkov
Jan 2 at 3:36
Please read stackoverflow.com/questions/6638044/… to learn about constructing strings.
– Alexei Levenkov
Jan 2 at 3:36
add a comment |
2 Answers
2
active
oldest
votes
If you really want to do this in loop, you need to modify it as
for(int i=0;i<itemsSold.Count();i++)
{
string s = itemsSold[i].Replace(";", "");
string d = ItemIDs[i].Replace(";", "");
displayformat += s + " - " + d + Environment.NewLine;
}
But, please note that this can be easily done with the Zip Method.
Instead of the entire Loop, you can write
displayformat = string.Join(Environment.NewLine,
itemsSold.Zip(ItemIDs,
(items, ids) => $"{items} - {ids}").Replace(";",string.Empty));
Console.WriteLine(displayformat);
You can read on Enumerable.Zip in here
Don't forget a final.Replace(";", "")on the produced string. Also the template string should be$"{items} - {ids}".
– Enigmativity
Jan 2 at 1:26
@Enigmativity thanks for pointing out, have changed it accordingly
– Anu Viswan
Jan 2 at 1:37
The Replace() is only needed at all because of the sloppy Split() earlier in the question code, but c'est la vie
– Joel Coehoorn
Jan 2 at 1:51
@AnuViswan - I'd just do the.Replace(...)once at the end. Keep the code cleaner. Just a suggestion. :-)
– Enigmativity
Jan 2 at 4:27
add a comment |
You can also use Distinct() key word.
hold the value into list and filter by Distinct()
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000033%2fconcatenate-strings-in-for-each-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you really want to do this in loop, you need to modify it as
for(int i=0;i<itemsSold.Count();i++)
{
string s = itemsSold[i].Replace(";", "");
string d = ItemIDs[i].Replace(";", "");
displayformat += s + " - " + d + Environment.NewLine;
}
But, please note that this can be easily done with the Zip Method.
Instead of the entire Loop, you can write
displayformat = string.Join(Environment.NewLine,
itemsSold.Zip(ItemIDs,
(items, ids) => $"{items} - {ids}").Replace(";",string.Empty));
Console.WriteLine(displayformat);
You can read on Enumerable.Zip in here
Don't forget a final.Replace(";", "")on the produced string. Also the template string should be$"{items} - {ids}".
– Enigmativity
Jan 2 at 1:26
@Enigmativity thanks for pointing out, have changed it accordingly
– Anu Viswan
Jan 2 at 1:37
The Replace() is only needed at all because of the sloppy Split() earlier in the question code, but c'est la vie
– Joel Coehoorn
Jan 2 at 1:51
@AnuViswan - I'd just do the.Replace(...)once at the end. Keep the code cleaner. Just a suggestion. :-)
– Enigmativity
Jan 2 at 4:27
add a comment |
If you really want to do this in loop, you need to modify it as
for(int i=0;i<itemsSold.Count();i++)
{
string s = itemsSold[i].Replace(";", "");
string d = ItemIDs[i].Replace(";", "");
displayformat += s + " - " + d + Environment.NewLine;
}
But, please note that this can be easily done with the Zip Method.
Instead of the entire Loop, you can write
displayformat = string.Join(Environment.NewLine,
itemsSold.Zip(ItemIDs,
(items, ids) => $"{items} - {ids}").Replace(";",string.Empty));
Console.WriteLine(displayformat);
You can read on Enumerable.Zip in here
Don't forget a final.Replace(";", "")on the produced string. Also the template string should be$"{items} - {ids}".
– Enigmativity
Jan 2 at 1:26
@Enigmativity thanks for pointing out, have changed it accordingly
– Anu Viswan
Jan 2 at 1:37
The Replace() is only needed at all because of the sloppy Split() earlier in the question code, but c'est la vie
– Joel Coehoorn
Jan 2 at 1:51
@AnuViswan - I'd just do the.Replace(...)once at the end. Keep the code cleaner. Just a suggestion. :-)
– Enigmativity
Jan 2 at 4:27
add a comment |
If you really want to do this in loop, you need to modify it as
for(int i=0;i<itemsSold.Count();i++)
{
string s = itemsSold[i].Replace(";", "");
string d = ItemIDs[i].Replace(";", "");
displayformat += s + " - " + d + Environment.NewLine;
}
But, please note that this can be easily done with the Zip Method.
Instead of the entire Loop, you can write
displayformat = string.Join(Environment.NewLine,
itemsSold.Zip(ItemIDs,
(items, ids) => $"{items} - {ids}").Replace(";",string.Empty));
Console.WriteLine(displayformat);
You can read on Enumerable.Zip in here
If you really want to do this in loop, you need to modify it as
for(int i=0;i<itemsSold.Count();i++)
{
string s = itemsSold[i].Replace(";", "");
string d = ItemIDs[i].Replace(";", "");
displayformat += s + " - " + d + Environment.NewLine;
}
But, please note that this can be easily done with the Zip Method.
Instead of the entire Loop, you can write
displayformat = string.Join(Environment.NewLine,
itemsSold.Zip(ItemIDs,
(items, ids) => $"{items} - {ids}").Replace(";",string.Empty));
Console.WriteLine(displayformat);
You can read on Enumerable.Zip in here
edited Jan 2 at 5:46
answered Jan 2 at 0:43
Anu ViswanAnu Viswan
5,6152525
5,6152525
Don't forget a final.Replace(";", "")on the produced string. Also the template string should be$"{items} - {ids}".
– Enigmativity
Jan 2 at 1:26
@Enigmativity thanks for pointing out, have changed it accordingly
– Anu Viswan
Jan 2 at 1:37
The Replace() is only needed at all because of the sloppy Split() earlier in the question code, but c'est la vie
– Joel Coehoorn
Jan 2 at 1:51
@AnuViswan - I'd just do the.Replace(...)once at the end. Keep the code cleaner. Just a suggestion. :-)
– Enigmativity
Jan 2 at 4:27
add a comment |
Don't forget a final.Replace(";", "")on the produced string. Also the template string should be$"{items} - {ids}".
– Enigmativity
Jan 2 at 1:26
@Enigmativity thanks for pointing out, have changed it accordingly
– Anu Viswan
Jan 2 at 1:37
The Replace() is only needed at all because of the sloppy Split() earlier in the question code, but c'est la vie
– Joel Coehoorn
Jan 2 at 1:51
@AnuViswan - I'd just do the.Replace(...)once at the end. Keep the code cleaner. Just a suggestion. :-)
– Enigmativity
Jan 2 at 4:27
Don't forget a final
.Replace(";", "") on the produced string. Also the template string should be $"{items} - {ids}".– Enigmativity
Jan 2 at 1:26
Don't forget a final
.Replace(";", "") on the produced string. Also the template string should be $"{items} - {ids}".– Enigmativity
Jan 2 at 1:26
@Enigmativity thanks for pointing out, have changed it accordingly
– Anu Viswan
Jan 2 at 1:37
@Enigmativity thanks for pointing out, have changed it accordingly
– Anu Viswan
Jan 2 at 1:37
The Replace() is only needed at all because of the sloppy Split() earlier in the question code, but c'est la vie
– Joel Coehoorn
Jan 2 at 1:51
The Replace() is only needed at all because of the sloppy Split() earlier in the question code, but c'est la vie
– Joel Coehoorn
Jan 2 at 1:51
@AnuViswan - I'd just do the
.Replace(...) once at the end. Keep the code cleaner. Just a suggestion. :-)– Enigmativity
Jan 2 at 4:27
@AnuViswan - I'd just do the
.Replace(...) once at the end. Keep the code cleaner. Just a suggestion. :-)– Enigmativity
Jan 2 at 4:27
add a comment |
You can also use Distinct() key word.
hold the value into list and filter by Distinct()
add a comment |
You can also use Distinct() key word.
hold the value into list and filter by Distinct()
add a comment |
You can also use Distinct() key word.
hold the value into list and filter by Distinct()
You can also use Distinct() key word.
hold the value into list and filter by Distinct()
answered Jan 2 at 11:15
Satya Satya
13
13
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000033%2fconcatenate-strings-in-for-each-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
use
itemsSold.Zip(ItemIDs, (l, r) => l + " - " + r + Environment.NewLine)– RajN
Jan 2 at 0:36
@RajN wouldnt that return an IEnumerable ?
– Anu Viswan
Jan 2 at 0:49
Please read stackoverflow.com/questions/6638044/… to learn about constructing strings.
– Alexei Levenkov
Jan 2 at 3:36