473,385 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

splitting lines in a file

hi,
am trying to split lines in a file in such a way
where ever and or dot(.) comes in a file the line should get splitted

my code is working only for .(dot)not for (and)

code is as shown below
Expand|Select|Wrap|Line Numbers
  1. static void Main(string[] args)
  2. {
  3.   int i = 1;
  4.   foreach (string line1 in File.ReadAllLines(@"g:/employee.txt"))
  5.   {
  6.     string[] srs = line1.Split('and','.');
  7.     foreach (string dot in srs)
  8.     {
  9.         Console.WriteLine("{0}:{1}",i,dot);
  10.         i++;
  11.     }
  12.   }
  13.   Console.ReadLine();
  14. }

with regards
aphoorva
Oct 25 '11 #1
2 2281
arie
64
In c# there is no String.Split() method that has two "separators", one of the type of char, and second of the type of string, as its parameters (list of Split() methods: http://msdn.microsoft.com/en-us/library/y7h14879.aspx )

To split by char (e.g. '.') you do:
Expand|Select|Wrap|Line Numbers
  1. string[] srs = line1.Split('.');
To split by string you can also do:
Expand|Select|Wrap|Line Numbers
  1. string[] srs = Regex.Split(line1, "and");
I don't know if there is a method in framework to split string using two different separators, I guess you can write your own. Or there can be some regex pattern that you can use with Regex.Split() method, but I know about regex patterns only that they are out there :(

Some split examples: http://www.dotnetperls.com/string-split
Oct 26 '11 #2
arie
64
I found the regex pattern that does the trick:

Expand|Select|Wrap|Line Numbers
  1. string pattern = @"((and)|(\.))";
  2. string[] srs = Regex.Split(line1, pattern);
The only problem is that it includes every "and" and "." in the table srs so you'll have to remove them later.
Oct 26 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: tgiles | last post by:
Hi, all. Been staring at this for a couple of hours now and I find myself completely bewildered. I suppose it doesn't help that I'm a php newbie. Nevertheless, I throw myself at your mercy. I...
1
by: Mothra | last post by:
I'm trying to parse a log file using ';' as a newline and then wherever I find items inside '(..)', indenting those lines thus: foo;bar(foo;bar(foobar);foo)foobar becomes foo bar(
2
by: Brian | last post by:
if i wanted to split the below SQL statement on two lines, what would the syntax be.... SQL = "UPDATE gallery WHERE g_id = " & Request.Form("g_id") & "SET g_name = " & Request.Form("g_name") &...
1
by: Andy Britcliffe | last post by:
Hi I'm faced with the situation where I could have a single physical file that could contain multiplie XML documents e.g file.txt contains the following: <?xml version="1.0"...
2
by: Afifov | last post by:
How can I split a file into several files each with a fixed size? I recall that on linux there is a command that has a counter for file size in bytes but cant seem to remember. Help is...
1
by: Skc | last post by:
I have used a program called chainsaw to split the file into manageable chunks, but with issues as the file contains crlf (carriage return line feeds on every line) and have found chainsaw totally...
3
by: tac-tics | last post by:
I know about os.path.split(), but Is there any standard function for "fully" splitting a file's pathname? A function that is the opposite of the os.path.join() function? For example: In the...
7
by: Peter Machell | last post by:
I have an application where I need to take a query from an existing database and send it to a web api. Here's a cut down version of my existing code: for foo in mssql.fetch_array(); bar = foo...
4
by: dafydd | last post by:
I got a multiline SQL file that I would like to split by ; and read into an array. However when I try to do my @array = split(";", <FH>); and iterate over each element I get just the first...
0
by: bdmir | last post by:
Hello, I have a text file(tab delim) with data and I want to create report. Here is the content of text file: HD John Smith j.smith@testmail.com CT service 1 5$ CT service2 1 6$ CT service3 4...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.