1

First of all, here is my code:

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "test1,test2,test3";
        }
    }
}

So, I want to split test1 test2 and test3 using substrings and give it out in a console via console.writeline(). I want to use the "," for it. I'm not allowed to use string.split. Could you help me?

7
  • 1
    What is wrong with .Split()? Commented Sep 11, 2018 at 5:58
  • You can use Regulaer Expression Commented Sep 11, 2018 at 5:59
  • Easiest way is Split function and this is pre defined approach Commented Sep 11, 2018 at 6:04
  • Try Regex.Split. It's not String.Split :D Commented Sep 11, 2018 at 6:06
  • string is essentially an array of char. So you can very easily iterate through it, and accumulate the chars, until you reach a comma. Commented Sep 11, 2018 at 6:08

3 Answers 3

2

Here is a simple solution for you by using .IndexOf() and .Substring() methods.

string strInput = "test1,test2,test3,";
while (strInput.Length > 0)
{
    int commaIndex = strInput.IndexOf(',');
    if (commaIndex != -1)
    {
        Console.WriteLine(strInput.Substring(0, commaIndex));
        strInput = strInput.Substring(commaIndex + 1);
    }
    else
    {
        Console.WriteLine(strInput);
        strInput = "";
    }
}

Where,

String.IndexOf Method Reports the zero-based index of the first occurrence of a specified Unicode character or string within this instance. The method returns -1 if the character or string is not found in this instance. And String.Substring Method will help you to Retrieves a substring from the given instance.

Sign up to request clarification or add additional context in comments.

Comments

2

This will work!

 class Program  
    {  
        static void Main(string[] args)  
        {  

            string str = "Test,Data";  
            ArrayList arrayList = new ArrayList();  
            string Temp = "";  
            for (int i = 0; i < str.Length; i++)  
            {  

                if (str[i] != ',')  
                {  
                    Temp = Temp + str[i];  
                    continue;  
                }  


                arrayList.Add(Temp);  
                Temp = "";  
            }  
            Console.WriteLine("Enter the no 1 to " + arrayList.Count);  
            int option =Convert.ToInt32(Console.ReadLine());  
            if (option < arrayList.Count && option > 0)  
            {  
                Console.WriteLine(option+ " position is  = " +arrayList[option - 1]);  
            }  
            else  
            {  
                Console.WriteLine("Enter only 1 to " + arrayList.Count);  
            }  
            Console.ReadLine();  
        }  
    }  

3 Comments

Why you are not allowed ? can please tell specifically
its "like" a homework. I'm a trainee in developement, and my trainer gave me that task. He wants me to figure it out. string.split is not allowed because it would make it too easy. :D
Great @Vischi : You want your homework questions to be solved on stackoverflow. Cheers man.. ! But you will not learn any thing of coding, if you would not try to solve your homework yourself.
1

For a non-regex answer:

string str = "test1,test2,test3";
string currentSubStr = string.Empty;
foreach (char c in str)
{
    if (c == ',')
    {
        Console.WriteLine(currentSubStr);
        currentSubStr = string.Empty;
    }
    else
    {
        currentSubStr = currentSubStr + c;
    }
}

This works for a very simply comma separated list, but if you're dealing with a csv file then you would be better off using a proper csv parser.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.