Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Yesterday i face one program in my interview. They give sentence like this.

They ask to split this para into single lines.

Input is

STR = Welcome to our company, This company is the MNC company , We have many branches all over world.

It is a one paragraph. But i just enter only three lines. Here the comma should be terminated

The output should be
1) Welcome to our company

2) This company is the MNC company

3) We have many branches all over world

they asked to use two dimension array for this and use string functions. But i don't know how to split using string function. I just use for loop for this program(But that is not correct.). They did not accept that. Please any one help me how to do this?
Posted

You can try like as below.

C#
using System;

class Program
{
    static void Main()
    {
    string s = "Welcome to our company, This company is the MNC company , We have many branches all over world";
    //
    // Split string on spaces.
    // ... This will separate all the words.
    //
    string[] words = s.Split(',');
    foreach (string word in words)
    {
        Console.WriteLine(word);
    }
    }
}


Output

Welcome to our company

This company is the MNC company

We have many branches all over world

For more info check this:

http://www.dotnetperls.com/split[^]

I hope this will help to you.
 
Share this answer
 
Comments
Vijaydhas 4-Oct-13 5:25am    
Thank you..!! It's Working.. :-) .. Useful and also easy to understand..!! Thank you one again..
Sampath Lokuge 4-Oct-13 5:46am    
Glad to hear that it helped :)
Vijaydhas 4-Oct-13 5:28am    
Is it possible in two dimensional array??
Sampath Lokuge 4-Oct-13 5:55am    
Here is a good example of 2D string Array.Check that.
http://www.dotnetperls.com/2d-array
Vijaydhas 4-Oct-13 6:17am    
Thank you..!!
The actual "breaking the string up" bit is simple: String.Split will do that.
The complexity is adding a numbered prefix to each line, and outputting them without a loop - which is not really possible, because even Linq methods (which could do it quite handily) contain a loop - it's just a hidden one.

But, this would have worked:
C#
string s = "Welcome to our company, This company is the MNC company , We have many branches all over world.";
string[] lines = s.Split(',');
string result = string.Join("\n\n", lines.Select((l, index) => string.Format("{0}) {1}", index + 1, l.Trim())));
Console.WriteLine(result);
 
Share this answer
 
Comments
Orcun Iyigun 4-Oct-13 4:41am    
My 5.
Vijaydhas 4-Oct-13 5:23am    
Thank you..!! It's working..!! :-) .. Can you explain this??
OriginalGriff 4-Oct-13 5:35am    
What is there to explain - it's a pretty simple Linq method!
Vijaydhas 4-Oct-13 5:29am    
Is it possible in two dimensional array??
OriginalGriff 4-Oct-13 5:35am    
How you you use a 2D array for this - you only have the one list of lines...what you you put in a 2D array at all?
C#
string STR = "Welcome to our company, This company is the MNC company , We have many branches all over world" 

string[] n = STR.Split(',');

string first=n[0].ToString();

string second=n[1].ToString();

string third=n[2].ToString();
 
Share this answer
 
v2
Comments
Thanks7872 4-Oct-13 4:51am    
Here,no need to use ToString() after n[]...
Ranesh M Raj 4-Oct-13 5:12am    
I no it,
no need for conversion because string[] n = STR.Split(','); returning string value
Vijaydhas 4-Oct-13 5:22am    
Thank you..!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900