i have 1 2 : 3 , now i have to remove the ":" , after that need split it .
after split, my expected output is shown below:

1 2 3
1 2
3

may i know how to remove the ":" and split it?

hope anyone can help me?

Recommended Answers

All 11 Replies

This how:

string a = "1 2 : 3";
            string b = a.Replace(":", "");  //removes :
            string[] split = b.Split(' ');   //splits to 3 characters

Mitja

Or Remove:

string str = "1 2 : 3";
            int ind = str.IndexOf(':');
            str = str.Remove(ind, 1);
            string[] split = str.Split(' ');

i had try your coding. your coding is work.
may i know how to display my output in message box?

MessageBox.Show(MyString);

But that would mean you have to concatenate your strings again.
Perhaps better is to assign your strings to the Text property of some Labels you put on your Form.

i had already success to remove the ":".
now my problem is 1 2 3, i just wan to split the 3 only.
after split the 3, 1 2 still maintain.
i wan the output is shown below :
1 2 3
1 2
3

May i know to to split the 3 only?

string str = "1 2 : 3";
str = str.Replace(":","");
string[] array = str.Split(' ');
string third = array[2];
string secnond = array[1];


//to get only 3 out of the whole string
stirng three = str.Substring(str.Lenght -1, 1);

i had try your code many times, but cannot run. i need to store the data in to arrayList. anyway, thanks ur help^^

This produces ouput as disired by the OP but read the comments:

string str = "1 2 : 3";
            int ind = str.IndexOf(':'); 
            str = str.Remove(ind, 2); // 2 to remove not only : but also extra space!
            //str = str.Replace(":", "");
            //with two spaces next to each other in str, Split would
            //produce an extra empty string in the string array!
            //test it by using 1 in Remove or use Replace
            string[] array = str.Split(' '); 
            Console.WriteLine("Output:");
            for (int i = 0; i < array.Length; i++)
			{
                Console.Write(array[i] + " ");
			}
            Console.WriteLine();
            for (int i = 0; i < array.Length - 1; i++)
            {
                Console.Write(array[i] + " ");
            }
            Console.WriteLine();
            Console.Write(array[array.Length - 1]);
            Console.ReadKey();
string str = "1 2 : 3";
            string[] s = NewString(str).Split(',');
            foreach (string strings in s)
            {
                if (!String.IsNullOrWhiteSpace(strings))
                {
                    MessageBox.Show(strings);
                }
            }

now the newstring method

private string NewString(string str)
        {
            string s = str.Replace(" ",",");
            return s.Replace(":", ",");
        }

also

private class stringlist:List<string>
{}

instead of MessageBox,Show method use

stringlist sl = new stringlist();
sl.Add(strings);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.