Click here to Skip to main content
15,899,126 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hye
I want to split the string into An array of string but i have to do it without using string.Split() method c# provides

Anyone who could help me? thanx
Posted
Comments
[no name] 6-Sep-13 7:57am    
So what other methods of the string class do you suppose that you could use to split a string? You did go look it up didn't you?
Marvin Ma 6-Sep-13 8:00am    
Would like to know that, too.
Why would you like to reinvent the wheel ?
Pheonyx 6-Sep-13 7:59am    
This sounds a bit like homework to me, well actually it stinks of it, but that aside I have the following questions:

1) As ThePhantomUpvoter asked, have you looked at the other String class methods?
2) Why can't you use Split ?
raeeschaudhary 6-Sep-13 8:02am    
because m asked not to use
Harshil_Raval 6-Sep-13 9:15am    
ok, then use string[] array = System.Text.RegularExpressions.Regex.Split("yourstring", "stringseperator");

Well, as a solution I suggest you check out the following link:

MSDN String Class Definition[^]
On there, Look at the methods:

IndexOf, SubString, Replace.

With them you could easily run some for loop and replicate the "split" function.

Give your homework a go and see how you get on.
 
Share this answer
 
You can crate your own split method as

C#
public String[] SplitText(String txt, char[] delim)
        {
            if (txt == null)
                return new String[0]; // or exception
            if (delim == null || delim.Length == 0)
                return new String[0]; // or exception

            char[] text = txt.ToCharArray();
            string[] result = new string[1]; // If there is no delimiter in the string, return the whole string
            int part = 0;
            int itemInArray = 1;

            for (int i = 0; i < text.Length; i++)
            {
                if (IsIn(delim, text[i]))
                {
                    Array.Resize(ref result, ++itemInArray); // Is it consider as a framework method ???
                    part++;
                }
                else
                    result[part] += text[i];
            }
            return result;
        }
        public static Boolean IsIn(char[] delim, char c)
        {
            for (int i = 0; i < delim.Length; i++)
                if (c == delim[i])
                    return true;
            return false;
        }



and then call it like this


C#
char[] delimter = new char[1];
delimter[0] = '-';
string[] b = SplitText("Your-string-goes-here", delimter);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900