Today a person posted a question in forum to split a string without using any build function and display a string using index. I am posting this programe in blog for all users whoever need this logics in feature.
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SplitStringWithoutUsingSplitMethod
- {
- class Program
- {
- static void Main(string[] args)
- {
- string str = "This,is,C#,Corner,Website";
- 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();
- }
- }
- }

Gaurav BhattPosted Jul 5, 2024, 11:51 AM
This code is incomplete as it is not displaying last letter whuch is website
Bala vigneshPosted Nov 10, 2021, 11:40 AM
Hi Ramesh Palanivel I tried your method but the last string doesn't get added to the arraylist as there is no comma to specify the ending you need to specify a condition to add the last character of the string
Logesh PalaniPosted May 27, 2019, 11:44 AM
If (option < arrayList.Count && option > 0) Modify to if (option <= arrayList.Count && option > 0) below also Console.WriteLine("Enter the no 1 to " + arrayList.Count +1)
Ramesh PalanivelPosted Apr 20, 2017, 7:16 AM
Hi Trin,Here I have hardcoded delimeter as comma(,), we can set it as space instead of comman if the delimeter is space. If we have double space , we can ask the user to define their delimeter in run time and we can split using that. If you want example I will post?
Former memberPosted Apr 20, 2017, 6:45 AM
How your logic would work if delimeter is space ? suppose there could be multiple space between two word.......how do you handle?