C
C#4mo ago
aCasualNon

how do i split for example t he string "dog" into "d" "o" "g"

Help.
6 Replies
Kiel
Kiel4mo ago
.ToCharArray() is probably the simplest way to do it but...this will be a char[] and not string[] which is probably not what you want?
Pobiega
Pobiega4mo ago
Or just foreach over the string
Kiel
Kiel4mo ago
(will still iterate over chars but I digress)
Pobiega
Pobiega4mo ago
Depends on what the goal is here
oke
oke4mo ago
string testStr = "this is a string"

// Foreach loop
foreach (var c in testStr)
{
// Do something with 'c'
}

// For loop (should only be used when you NEED the index of the char, otherwise use foreach)
char[] tmpCArray = new char[testStr.Length];
for (int i = 0; i < testStr.Length; i++)
{
// Do something with 'i'
tmpCArray[i] = testStr[i];
}

// Via string.ToCharArray()
char[] cArray = testStr.ToCharArray();

// Copying the string into a char[]
char[] cArray1 = new char[testStr.Length];
testStr.CopyTo(cArray1, 0);

// With LINQ
char[] cArray2 = myString.Select(c => c).ToArray();
string testStr = "this is a string"

// Foreach loop
foreach (var c in testStr)
{
// Do something with 'c'
}

// For loop (should only be used when you NEED the index of the char, otherwise use foreach)
char[] tmpCArray = new char[testStr.Length];
for (int i = 0; i < testStr.Length; i++)
{
// Do something with 'i'
tmpCArray[i] = testStr[i];
}

// Via string.ToCharArray()
char[] cArray = testStr.ToCharArray();

// Copying the string into a char[]
char[] cArray1 = new char[testStr.Length];
testStr.CopyTo(cArray1, 0);

// With LINQ
char[] cArray2 = myString.Select(c => c).ToArray();
aCasualNon
aCasualNon4mo ago
ty
Want results from more Discord servers?
Add your server
More Posts
✅ TreeView scrollbar isn't visibleI have a tree view, and I want it to be scrollable when there are many items. I have tried to put iUnit testing not working, doesn't hit multiple test classesThe first test class runs perfectly fine, but the second one doesnt even get hit. I cant debug or seASP.NET Identity Login ErrorWhen trying to sign in using SignInManager, I get a SqlException: Invalid column name 'UserId1'. WhHow do we read network/handshake messages?Hello friends, I have been given the following task: - I should establish a connection to a WebSockeCan different validation attributed be applied to a field based on the active profile?Hello, In `Development` I would need to apply a different validation attribute to a DTO field, withAndroid Emulatorhow to fix this guys please help me.AS2 over http C# Send message with signature to mendelson server.Hi guys) I am new in this channel. Can you please help me with AS2 over http PKCS#7 .p7s file) Yeah.Persistent error with dotnet cli and installing maui workspaceI've tried time and again now without joy. There is precious little info on this issue out there (onneed tips to improve my logical skills and .net c# skills as I'm a intermediate dev.Hi,I want to improve my logical and .net c# skills so i want please suggest me or guide me about to Issue with Azure AD B2C Custom Domains: Unexpected Redirect Behavior and Domain DependencyHey everyone, I'm hopeful that there are experienced individuals with Azure AD B2C expertise who can