0

I have a text file and contain text below:

Contact Name        |       Contact Number

Muhammad Hassan Ali Khan|       123456
Danish Abdul Ghani      |       165484
Adeel Siddiqui          |       865697
Muhammad Faisal Bilal   |       569745
Arslan                  |       145236

and I want to dispaly it as it is in the gridview and i am using the following code: for checking that i am right working for getting text from a file.

protected void Page_Load(object sender, EventArgs e)
        {
            string line = "";
            StreamReader reader = new StreamReader("c:\\Contacts.txt");
            while ((line = reader.ReadLine()) != null)
            {
                string[] arr = line.Split('\t\t|\t\t');
            }
        }

But at this line "string[] arr = line.Split('\t\t|\t\t');" error occur:

Error: Too many characters in character literal

How to remove this error. Kindly suggest me. waiting for reply. Thanks

1
  • The apostrophe in Split('') indicates a type of Char`, which can only hold a single character. Commented Jan 17, 2014 at 10:33

4 Answers 4

3

You can do this

 string line = "";
 StreamReader reader = new StreamReader("c:\\Contacts.txt");
 while ((line = reader.ReadLine()) != null)
 {
      string[] arr = line.Split('|');
      arr = arr.Select(x => x.Trim()).ToArray();
 }
Sign up to request clarification or add additional context in comments.

1 Comment

This is the only answer that will actually work properly as the original solution of \t\t|\t\t is not correct anyway based on the provided source data.
1

try this;

string[] arr = line.Split(new string[] { "\t\t|\t\t" }, StringSplitOptions.RemoveEmptyEntries);

Edit: ' encapsulates a character, " is for strings.

1 Comment

ojlovecd: your line of code is working but it gets only the first line Contact Name | Contact Number and I want to get all above text,
1

You have to pass an array of characters to the String.Split() method. The characters from array will be used as separators for splitting.

As the method Split() has a keyword params in its signature, you can omit the direct creation of the array and just pass characters separated by comma.

The examples:

string[] arr = line.Split('|');
string[] another = line.Split('|','-',':','!');

It's well worth a note that one of the overloaded versions of Split() accepts a StringSplitOptions enumeration.

I would recommend you use it with StringSplitOption.RemoveEmptyEntries option. That will prevent appearing of empty elements in the result array. However, this version of the method does not have params keyword, so you do have to create the array.

var arr = mystring.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

1 Comment

Because the String.Split() method actually has a signature with keyword 'params' e.g. String.Split(params char[] separator). So you can omit creation of the array, or even call it like mystring.Split('-',':','|');
0

If you need to split on multiple characters, use Regex:

   line.Split(value, "'\t\t|\t\t'");

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.