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,933 questions

51,870 answers

573 users

How to copy string into array of char in C#

3 Answers

0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string s = "Consol Application";
                char[] arr = new char[3];
                
                s.CopyTo(7, arr, 0, 3);
                Console.WriteLine(arr); // App

                // just to see the string characters index
                for (int i = 0; i < s.Length; i++) 
                    Console.WriteLine("s[{0}] = {1}", i, s[i]);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

/*
run:

App
s[0] = C
s[1] = o
s[2] = n
s[3] = s
s[4] = o
s[5] = l
s[6] =
s[7] = A
s[8] = p
s[9] = p
s[10] = l
s[11] = i
s[12] = c
s[13] = a
s[14] = t
s[15] = i
s[16] = o
s[17] = n

*/


answered Mar 22, 2015 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string s = "Consol Application";
                char[] arr = new char[s.Length];
                
                s.CopyTo(0, arr, 0, s.Length);
                Console.WriteLine(arr); // Consol Application

                // just to see the characters in arr
                for (int i = 0; i < arr.Length; i++) 
                    Console.WriteLine("arr[{0}] = {1}", i, arr[i]);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

/*
run:

Consol Application
arr[0] = C
arr[1] = o
arr[2] = n
arr[3] = s
arr[4] = o
arr[5] = l
arr[6] =
arr[7] = A
arr[8] = p
arr[9] = p
arr[10] = l
arr[11] = i
arr[12] = c
arr[13] = a
arr[14] = t
arr[15] = i
arr[16] = o
arr[17] = n

*/


answered Mar 22, 2015 by avibootz
0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                char[] arr = new char[3];
                
                "abc".CopyTo(0, arr, 0, 3);
                Console.WriteLine(arr); // abc

                // just to see the characters in arr
                for (int i = 0; i < arr.Length; i++) 
                    Console.WriteLine("arr[{0}] = {1}", i, arr[i]);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

/*
run:

abc
arr[0] = a
arr[1] = b
arr[2] = c

*/


answered Mar 22, 2015 by avibootz

Related questions

1 answer 142 views
142 views asked Aug 15, 2022 by avibootz
1 answer 110 views
1 answer 121 views
1 answer 155 views
1 answer 154 views
1 answer 132 views
2 answers 169 views
169 views asked Aug 15, 2022 by avibootz
...