Click here to Skip to main content
15,888,006 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
string a = 1,23,45; 6,7,8

i want to split the string after semi colon

I want the output as follows

1,23,45
6,7,8


for that how to do in csharp.

What I have tried:

string a = 1,23,45; 6,7,8

i want to split the string after semi colon

I want the output as follows

1,23,45
6,7,8


for that how to do in csharp
Posted
Updated 22-Mar-18 3:15am
v2

1. You've forgotten quotes
C#
string a = "1,23,45; 6,7,8";


2. If you want split string by a semicolon as mentioned in the question caption you can use String.Split[^] as follows. The result is array of strings
C#
var splitted = a.Split(';');


3. If you want output as in question you can use String.Replace[^]
C#
var replaced = a.Replace(";", Environment.NewLine);
 
Share this answer
 
v2
Look up the "String.Split" method. The documentation has a nice example.
 
Share this answer
 
See How to: Parse Strings Using String.Split (C# Guide) | Microsoft Docs[^]:
C#
string[] splitted = a.Split(';');
foreach (var part in splitted)
{
    System.Console.WriteLine($"<{part}>");
}
 
Share this answer
 

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