How to spliting a string
-
-
Hey Guys, I have a string consisting of zeros and ones like 10101010 I want to read each letter then convert it to code and use it ... I've checked the function Split() but it requires a character, and as u see mine doesn't have any. Regards, K
-
Hey Guys, I have a string consisting of zeros and ones like 10101010 I want to read each letter then convert it to code and use it ... I've checked the function Split() but it requires a character, and as u see mine doesn't have any. Regards, K
-
Just use - ToCharArray()
string strTest = "1100110101";
char[] cList = strTest.ToCharArray();modified on Friday, April 16, 2010 7:33 AM
This is okay unless you don't want to allocate more memory in the process. Using
string.Substring()
would be a viable alternative. (I still marked your response as a "good answer". :) ).45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
Hey Guys, I have a string consisting of zeros and ones like 10101010 I want to read each letter then convert it to code and use it ... I've checked the function Split() but it requires a character, and as u see mine doesn't have any. Regards, K
I have to ask - what does the string represent? A series of flags? If so, you could convert it to an integer, and then access it with logical AND (&) and logical OR (|) operators. Math operations are always faster/more efficient than string operations. To convert the string to an int, do this:
string myString = "10101010";
int flags = Convert.ToInt32(myString, 2);At that point, you can do this:
int myVal = 4;
if ((myVal & flags) == myVal)
{
// do something if 4 is a set value
}.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
I have to ask - what does the string represent? A series of flags? If so, you could convert it to an integer, and then access it with logical AND (&) and logical OR (|) operators. Math operations are always faster/more efficient than string operations. To convert the string to an int, do this:
string myString = "10101010";
int flags = Convert.ToInt32(myString, 2);At that point, you can do this:
int myVal = 4;
if ((myVal & flags) == myVal)
{
// do something if 4 is a set value
}.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
I have to ask - what does the string represent? A series of flags? If so, you could convert it to an integer, and then access it with logical AND (&) and logical OR (|) operators. Math operations are always faster/more efficient than string operations. To convert the string to an int, do this:
string myString = "10101010";
int flags = Convert.ToInt32(myString, 2);At that point, you can do this:
int myVal = 4;
if ((myVal & flags) == myVal)
{
// do something if 4 is a set value
}.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001John Simmons / outlaw programmer wrote:
Math operations are always faster/more efficient than string operations.
Unless you run out of fingers and toes, then have to resort to paper and pencil. ;P
I know the language. I've read a book. - _Madmatt
-
This is okay unless you don't want to allocate more memory in the process. Using
string.Substring()
would be a viable alternative. (I still marked your response as a "good answer". :) ).45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
I have access to each character of my string ! :D each character is a code, I'm trying to store days for a class so for instance its 1000000, it means its only Saturday, or 1010100 means it's on Saturday, Monday and Wednesday. Regards, K
Well, then you can do that more cleanly if you want...
[Flags]
public enum DayFlags
{
Saturday = 64,
Sunday = 32,
Monday = 16,
Tuesday = 8,
Wednesday = 4,
Thursday = 2,
Friday = 1
}public static bool IsFlagSet(this DayFlags en, DayFlags flag)
{
return ((int)en & (int)flag) != 0;
}DayFlags days = (DayFlags)Convert.ToInt32(str, 2);
if (days.IsFlagSet(DayFlags.Sunday)) { ... }
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
John Simmons / outlaw programmer wrote:
Math operations are always faster/more efficient than string operations.
Unless you run out of fingers and toes, then have to resort to paper and pencil. ;P
I know the language. I've read a book. - _Madmatt