Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Term Entry] C# Strings .ToCharArray() #5793

Merged
merged 11 commits into from
Dec 14, 2024
113 changes: 113 additions & 0 deletions content/c-sharp/concepts/strings/terms/tochararray/tochararray.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
---
Title: '.ToCharArray()'
Description: 'Converts a string into a character array in C#. Each character in the string becomes an element in the array.'
PragatiVerma18 marked this conversation as resolved.
Show resolved Hide resolved
Subjects:
- 'Computer Science'
- 'Code Foundations'
Tags:
- 'Strings'
- 'Methods'
CatalogContent:
- 'learn-c-sharp'
- 'paths/computer-science'
---

The **`.ToCharArray()`** method in C# is used to convert a string into an array of characters. Each character in the string becomes an element in the resulting array, allowing for manipulation or iteration of individual characters.
PragatiVerma18 marked this conversation as resolved.
Show resolved Hide resolved

## Syntax

```pseudo
char[] arrayName = stringName.ToCharArray();
```

- `stringName`: The string to be converted into a character array.
- `arrayName`: The resulting array containing individual characters from the string.

### Optional Parameters

The `.ToCharArray()` method also allows for optional parameters to specify a substring to convert:
PragatiVerma18 marked this conversation as resolved.
Show resolved Hide resolved

```pseudo
char[] substringArray = stringName.ToCharArray(startIndex, length);
```

- `startIndex`: The zero-based index where the substring starts.
- `length`: The number of characters to include in the resulting array.
PragatiVerma18 marked this conversation as resolved.
Show resolved Hide resolved

## Examples

### Split into Individual Characters

Here's an example of using `.ToCharArray()` to split a string into individual characters:

```cs
string greeting = "Hello, World!";
char[] charArray = greeting.ToCharArray();

foreach (char c in charArray)
{
Console.WriteLine(c);
}
PragatiVerma18 marked this conversation as resolved.
Show resolved Hide resolved
```

The output of the above code will be as follows:

```shell
H
e
l
l
o
,

W
o
r
l
d
!
```

### Converting a Substring to a Character Array

```cs
string phrase = "Convert this string!";
char[] partialArray = phrase.ToCharArray(8, 4);

foreach (char c in partialArray)
{
Console.WriteLine(c);
}
PragatiVerma18 marked this conversation as resolved.
Show resolved Hide resolved
```

The output of the above code will be as follows:

```shell
t
h
i
s
```

## Codebyte Example

Try this example to see how `.ToCharArray()` works in C#:
PragatiVerma18 marked this conversation as resolved.
Show resolved Hide resolved

```codebyte/csharp
using System;

class Program
{
static void Main()
{
string example = "Learn C#";
char[] charArray = example.ToCharArray();

Console.WriteLine("Character Array:");
foreach (char c in charArray)
{
Console.Write(c + " ");
}
}
}
```
Loading