Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to create a string from one row of a two-dimensional character array in C#

3 Answers

0 votes
using System;
using System.Text;
 
public class CreateStringFromOneRow
{
    public static void Main()
    {
        char[,] array = {
            { 'a', 'a', 'a', 'a', 'a' },
            { 'b', 'b', 'b', 'b', 'b' },
            { 'c', 'c', 'c', 'c', 'c' }
        };
 
        // StringBuilder to hold the string of a row
        StringBuilder sb = new StringBuilder();
 
        // Iterate over the row and append characters to the StringBuilder
        int len = array.GetLength(1);
        for (int i = 0; i < len; i++) {
            sb.Append(array[1, i]); // row 1
        }
 
        // Convert StringBuilder to string
        string result = sb.ToString();
 
        Console.WriteLine("The string is: " + result);
    }
}
 
 
 
/*
run:
 
The string is: bbbbb
 
*/

 



answered Feb 6, 2025 by avibootz
edited Feb 6, 2025 by avibootz
0 votes
using System;
using System.Text;

public class CreateStringFromOneRow
{
    public static void Main()
    {
        
        char[][] jaggedArray = new char[3][];

        // Initialize the rows with different lengths
        jaggedArray[0] = new char[] { 'C', '#' }; 
        jaggedArray[1] = new char[] { 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g' }; 
        jaggedArray[2] = new char[] { 'l', 'a', 'n', 'g', 'u', 'a', 'g', 'e' }; 

        // StringBuilder to hold the string of a row
        StringBuilder sb = new StringBuilder();

        // Iterate over the row and append characters to the StringBuilder
        foreach (char ch in jaggedArray[1]) { // row 1
            sb.Append(ch); 
        }

        // Convert StringBuilder to string
        string result = sb.ToString();

        Console.WriteLine("The string is: " + result);
    }
}


/*
run:

The string is: programming

*/

 



answered Feb 6, 2025 by avibootz
0 votes
using System;

public class CreateStringFromOneRow
{
    public static void Main()
    {
        char[,] array = {
            { 'a', 'a', 'a', 'a', 'a' },
            { 'b', 'b', 'b', 'b', 'b' },
            { 'c', 'c', 'c', 'c', 'c' }
        };

        // Create a one-dimensional array to hold the characters of a row
        char[] rowArray = new char[array.GetLength(1)];

        // Copy a row of the 2D array to the one-dimensional array
        int len = array.GetLength(1);
        for (int i = 0; i < len; i++) {
            rowArray[i] = array[1, i]; // rows 1
        }

        // Convert the one-dimensional array to a string
        string result = new string(rowArray);

        Console.WriteLine("The string is: " + result);
    }
}



/*
run:

The string is: bbbbb

*/

 



answered Feb 6, 2025 by avibootz
...