Am working on a project right now and almost stuck at a point. Say, I have a text document with contents like:
abc,123.xyz;praise;end,file,clear

Now I want abc , 123 . xyz ; praise ... in an array. I used split method with array of characters. Though I retrieved abc,123,xyz,praise,end,file,clear I couldn't retrieve , . ; ; , , ,. I need every different characters in text file.

#region Splitting
            string splitSamp = "abc,123.xyz;praise;end,file,clear";
            string[] splitSampArr = splitSamp.Split(',','.',';');
            string[] myText=new string[splitSamp.Length];
            int i=0;
            foreach (string splitSampArrVal in splitSampArr)
            {
                Console.WriteLine(splitSampArrVal);
                myText[i]=splitSampArrVal;
                i++;

            }
            for (i = 0; i < 10; i++)
            {
                Console.WriteLine(myText[i]);
            }
#endregion

I imported the files to a rich text document control using file dialog. I want the the string in a variable say myText (it is an array)

myText[0]=abc
myText[1]=,
mytext[2]=123
myText[3]=.
myText[4]=xyz
and soon

Any help soon will be appreciated. Thanks!

Recommended Answers

All 5 Replies

string str = "abc,123.xyz;praise;end,file,clear";
int index = 0;
for (int i = 0; i < str.Length; i++)
{
   //add str[i] to myText[index] as long as it is a letter or digit
   //increment index if punctuation encountered etc.
}

I think am confused a bit. How can I identify a character as letter or a punctuation mark? The problem for me lies over there. I even tried with toCharArray. Differentiating letters from marks is the problem for me!

The char structure has methods like IsDigit or IsLetter. Example.

So this is my final code snippet and I solved it with the help of this.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string[] myText = new string[100];
        int i = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
            Regex rex = new Regex("[a-z|A-Z|0-9]");
            string str = "abc,123.xyz;praise;end,file,clear";
            string tok = "";
            foreach (char ch in str.ToCharArray())
            {
                if (rex.IsMatch(Convert.ToString(ch)))
                {
                    //letter or digit - add it to the token string
                    tok += ch;
                }
                else
                {
                    //punctuation - display two tokens and clear string
                    textBox1.Text += tok + Environment.NewLine; 
                    textBox1.Text += ch + Environment.NewLine; 
                    myText[i] = tok;
                    i += 1;
                    myText[i] = Convert.ToString(ch);
                    i += 1;
                    tok = "";
                }
            }
            if (tok.Length > 0)
            {
                textBox1.Text += tok + Environment.NewLine;
                myText[i] = tok;
                i += 1;
            }

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int a = 0; a <= 99; a++)
            {
                textBox2.Text += a + " : " + myText[a] + Environment.NewLine;
                if (myText[a] == "praise")
                {
                    MessageBox.Show("Praise God");
                }
            }
        }
    }
}

Thanks again ddanbe for your help too..

Also have a look at this

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.