- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Iterating C# StringBuilder in a foreach loop
Firstly, set a string array and StringBuilder −
// string array string[] myStr = { "One", "Two", "Three", "Four" }; StringBuilder str = new StringBuilder("We will print now...").AppendLine();
Now, use foreach loop to iterate −
foreach (string item in myStr) { str.Append(item).AppendLine(); }
The following is the complete code −
Example
using System; using System.Text; public class Demo { public static void Main() { // string array string[] myStr = { "One", "Two", "Three", "Four" }; StringBuilder str = new StringBuilder("We will print now...").AppendLine(); // foreach loop to append elements foreach (string item in myStr) { str.Append(item).AppendLine(); } Console.WriteLine(str.ToString()); Console.ReadLine(); } }
Output
We will print now... One Two Three Four
- Related Articles
- How do you use ‘foreach’ loop for iterating over an array in C#?
- foreach Loop in C#
- Using foreach loop in arrays in C#
- PHP foreach Loop.
- Stripping last comma from a foreach loop in PHP?
- Multiple index variables in PHP foreach loop
- How does the Java “foreach” loop work?
- How to show a foreach loop using a flow chart in JavaScript?’
- How to use PowerShell break statement in foreach loop?
- PHP Casting Variable as Object type in foreach Loop
- How to use PSCustomObject in PowerShell foreach parallel loop?
- How to use the foreach loop parallelly in PowerShell?
- The internal working of the ‘foreach’ loop in PHP
- Clear a StringBuilder in C#
- Implement MongoDB toLowerCase() in a forEach loop to update the name of students?

Advertisements