user175084
user175084

Reputation: 4630

break a string in parts

I have a string "pc1|pc2|pc3|"

I want to get each word on different line like:

pc1

pc2

pc3

I need to do this in C#...

any suggestions??

Upvotes: 0

Views: 288

Answers (6)

Philippe Leybaert
Philippe Leybaert

Reputation: 171814

 string[] parts = s.Split(new [] {'|'}, StringSplitOptions.RemoveEmptyEntries);

 foreach (string part in parts)
    Console.WriteLine(part);

Upvotes: 12

Jon
Jon

Reputation: 6046

string fullString = "pc1|pc2|pc3|";
foreach (string pc in fullString.Split('|'))
   Console.WriteLine(pc);

Upvotes: 0

bruno conde
bruno conde

Reputation: 48265

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

foreach (string word in parts)
{
  Console.WriteLine(word);
}

Upvotes: 4

LukeH
LukeH

Reputation: 269418

string withNewLines = original.Replace("|", Environment.NewLine);

Upvotes: 4

mculp
mculp

Reputation: 2687

string words[] = string.Split("|");
foreach (string word in words)
{
  Console.WriteLine(word);
}

Upvotes: 0

Bryan McLemore
Bryan McLemore

Reputation: 6493

string s = "pc1|pc2|pc3|";
string[] words = s.Split('|');

Upvotes: 1

Related Questions