String manipulation in C#: split on `/` - c#

I need to extract headstone data from an inscription file (structured text file). From this file I am supposed to extract the name of a deceased person, date of birth (or age) and also personal messages. The application should be able to analyse a raw text file and then extract information and display it in tabular form.
The raw text files looks like this:
In loving memory of/JOHN SMITH/who died on 13.02.07/at age 40/we will
miss you/In loving memory of/JANE AUSTIN/who died on 10.06.98/born on
19.12.80/hope you are well in heaven.
Basically / is a delimiter and the name of a deceased person is always in capital letters. I have tried to use String.Split() and substring methods but I can't get it to work for me; I can only get raw data without the delimiter (Environment.Newline) but I don't know how to extract specific information.

You'll need something like:
Open your data file (System.IO)
For each line, do (tip: pick a stream where you can read line by line)
Split them by "/" getting a string[]
Arrays in C# starts at 0; so splitted[1] will be that name, ans so on
Store that information in a managed collection (maybe to use generics?)
Display that collection in a tabular view

Check out How to: Use Regular Expressions to Extract Data Fields. I know the example is in C++, but the Regex and Match classes should help you get started
Here is another good example of parsing out individual sentences.

I would have thought something like this would do the trick
private static void GetHeadStoneData()
{
using(StreamReader read = new StreamReader("YourFile.txt"))
{
const char Delimter = '/';
string data;
while((data = read.ReadLine()) != null)
{
string[] headStoneInformation = data.Split(Delimter);
//Do your stuff here, e.g. Console.WriteLine("{0}", headStoneInformation[0]);
}
}
}

Related

Click a listbox item and display textfile text in labels

I'm getting stuck clicking a listbox item (the items in my listbox is .txt files in a folder) and displaying the values in the .txt file.
the values in the .txt files are all seperated with a "," and I want each Split item to display in labels on my form.
my file path is: System.AppDomain.CurrentDomain.BaseDirectory + "data"
my .txt file names are the names of the selected item in my listbox.
I have a basic idea of what should happen, but I have no clue how to express this in code.
My Code:
private void custList_MouseClick(object sender, MouseEventArgs e)
{
string foldr = System.AppDomain.CurrentDomain.BaseDirectory + "data";
string file = custList.SelectedIndex.ToString();
}
Thanx in advance
Before i go on answering this question, please read How to ask. This will help you better understand this community and ask better questions(to get better support).
Next on, do some research. From a first glance, it looks like you are asking someone to do the homework for you. Anyways, i am not here to be tough on you. I will point you out a few things. Try to understand them and utilize them.
Note that even though it may seem as such but i am not a fan of spoon-feeding so be sincere and do your research.
Let's start with your text file. As you mentioned, it contains values. C#, being a versatile & mature language has a lot of functions,methods,classes pre-built to help boost your programming experience. Such a method is ReadAllText, a part of the File class. In the simplest words, this method opens a text file, reads it, returns it's value. A sample use of this method could be :
string TextFromFile = File.ReadAllText(File_Path_Goes_Here);
Moving on... Your text file has multiple values separated by comma(,). In such cases, each value needs to read as a separate value upon retrieving or displaying. So, you want a List of the values, end of story. In C#, you have a wide range of generic lists to use from. As the values in the text file are simple strings, you can use a List<string> in this regard. A basic usage example of List<string> would be :
List<string> TestList1 = new List<string>();
TestList1.Add("First Value"); TestList1.Add("Second Value");
///or
List<string> TestList1 = new List<string>(){ "First Value", "Second Value" };
In your particular case, File.ReadAllLines is worth an example. The method opens a text file, reads it, closes it. Of course it returns the value read from the text file, as an array. So, when passing values to the generic list, you can simply make use of this method. Example :
...... new List<string>(File.ReadAllLines(Path_Of_File_Goes_Here));
The only twist here is that the values in your text file are in a line(maybe) and also are separated by comma. So, what do you think should work here ? ReadAllText or ReadAllLines ? I will leave it upto you.
Once values are read from the file, we can make use of the Split function to split the values upon each occurrence of a comma(,). A simple example :
List<string> NameList = "Josh,Riley".Split(',').ToList<string>();
And last but not the least, the headline of the question which doesn't seem to have anything to do with the post itself, here's what you can take a look at :
Control Click Event
ListBox.GetItemText
Tip: The SelectedItem property of the ListBox class returns or sets the selected item of a listbox.
I hope this has been helpful. Do pay attention to all that's mentioned above. It may be a bit hard to follow up with at first, but remember, Consistency is the hallmark of the unimaginative.
....Yeah, that's not my quote. Gotcha!
I don't know where you want to show the values of your text file, so I will just provide you what you need to retrieve those informations.
To get values from your file:
public string[] GetValues()
{
string[] values;
using(StreamReader sr = new StreamReader(Path.Combine(foldr, file))
{
string text = sr.ReadToEnd();
values = text.Split(',');
}
return values;
}
Then you can show them using an array:
public void Main()
{
string[] values = GetValues();
foreach(var value in values)
{
Console.WriteLine(value);
}
}
Hope this helps.

Store tab delimited clipboard data to list of objects in C#

I am new to C# but am working on a project at work. The task is to copy an array of values from one application to the clipboard and then to store those values in a list of objects in C#. I have created private properties for each field represented by the columns in the copied data, but i cannot figure out how to parse the data from the clipboard and store it in each property. Hopefully this makes sense. I'm having trouble even figuring out where to begin despite extensive searching. Any help would be greatly appreciated.
As suggested by Jack A. in the comments, use String.Split().
Something like...
if (Clipboard.ContainsText())
{
string str = Clipboard.GetText();
string[] values = str.Split("\t".ToCharArray());
// ... do something with "values" ...
xxx.aaa = values[0];
xxx.bbb = values[1];
xxx.ccc = values[2];
// etc...
}
else
{
MessageBox.Show("No Text in the Clipboard!");
}
You should probably check to make sure the right number of columns were present after splitting, though:
if (values.Length == 5)
{
// ... do something with "values" ...
}
I used this recently in another project: http://joshclose.github.io/CsvHelper/
Although you aren't using a CSV per se, you are using delimited data and it allows you to specify the delimeter. If you convert your text into a text reader similar to this post: In C#, how can I create a TextReader object from a string (without writing to disk), it will easily transfer the data to an object of your choosing once you have mapped it

How a can split file line with many spaces in C# asp?

I need your help. I have a txt file with many lines of information.
The headers of the file are
Date ReferenceNumber Description
13/06/2013 00000081985 TRF DESDE OTRO BCO 00000000000000972353
0105
Mount Money +50.000,00 344.514,74
Between Description and Mount are many spaces
Here's a image of the file
I need split this line to get all the attributes by separate.
I need, Date = 13/06/2013, ReferenceNumber = 00000081985, ....
I'm trying to use split C# function to separate by (' ') but i only can get the 2 first attributes =(
I hope you can help me! Thanks a lot.
You may want to look to see what the length of each field is because it does look like fixed length data. If so use the String.Substring Method, using the starting position and the max length of each field as inputs.
This looks like you're trying to deal with a fixed length file, which is essentially a file with data that is split based on its physical location in the file (each piece of data is expected to occupy a specific number of characters). Seems to be one of those lesser known functions, but check out TextFieldParser. It's a .NET class specifically made for this sort of thing.
Specifically, check out the property TextFieldType, which can be set to FixedWidth and given a width of each of those fields. Should do exactly what you want.
You can try to do something like this:
StreamReader sr = new StreamReader("path to text file");
string s = sr.ReadToEnd();
s = s.Replace(' ', '!'); //change the space sign with other sign
List<string> strList = s.Split('!').ToList();
strList.RemoveAll(t => t == "");
I know the solution isn't best but I hope it will help you.

C# Reading file and pulling out specific lines

Thanks in advance. What i currently have is a filedialog box selecting a text file. The text file contents will look like example.txt, and it needs to look like output.txt. NOTE: the string CoreDBConnectString= is 1 line all the way to ;Database=Source_DB.
example.txt
[Information]
Date=
CreatedBy=
Unique=eqwe-asd-123-as12-3
CoreDataSource=
CoreDBConnectString=Provider=SQLOLEDB.1;Server=Server;Integrated Security=SSPI;Database=Source_DB
NoteDataSource=SQLServer
NoteDBConnectString=Provider=Provider=SQLOLEDB.1;Server=Server;Integrated Security=SSPI;Database=Source_DB
CoreDBCaseID=99
NoteDBCaseID=99
Output.txt
Table=99 (Comes from CoreDBCaseID)
Server=Server (comes from the string CoreDBConnectString=)
Security=SSPI (comes from the string CoreDBConnectString=)
Database=Source_DB (comes from the string CoreDBConnectString=)
You can do something like this:
// Load the file contents
string contents = File.ReadAllText("example.txt");
// Obtain the data using regular expressions
string id = string id = Regex.Match(
contents,
#"CoreDBCaseID=(?<id>\d+)").Groups["id"].Value;
string server = string.Empty; // Add regex here
string security = string.Empty; // Add regex here
string database = string.Empty; // Add regex here
// Save the data in the new format
string[] data = new string[] {
String.Format("Table={0}", id),
String.Format("Server={0}", server),
String.Format("Security={0}", security),
String.Format("Database={0}", database)
};
File.WriteAllLines("output.txt", data);
And a quick way to learn those regular expressions:
Regular Expression Cheat Sheet
Regular-Expressions.info
You can use a positive lookahead to stop matching at the (;) character. Something like this:
#"Security=(?[\D]+)(?=;)"
That is an INI file, which is very old school. Nowadays, we programming folks use XML instead. Consequently, there is no built-in support for INI files in C#.
You can P/Invoke GetPrivateProfileString to read the data.
You can use WritePrivateProfileString to write the new data out if you don't mind the information section header like so :
[Information]
Table=99
Server=Server
Security=SSPI
Database=Source_DB
This CodeProject article on INI-file handling with C# may help.
I would use a combination of StreamReader.ReadLine and RegEx to read each line and extract the appropriate information.
Open the file and read it in one line at a time. You can use Regular Expressions (cheat sheet) to match and parse only the text you want to find.
INI files are old skool. Thus, someone has already written a class for it: http://www.codeproject.com/KB/cs/readwritexmlini.aspx. The class linked reads XML, INI, Registry, and Config Files.

Read fixed width record from text file

I've got a text file full of records where each field in each record is a fixed width. My first approach would be to parse each record simply using string.Substring(). Is there a better way?
For example, the format could be described as:
<Field1(8)><Field2(16)><Field3(12)>
And an example file with two records could look like:
SomeData0000000000123456SomeMoreData
Data2 0000000000555555MoreData
I just want to make sure I'm not overlooking a more elegant way than Substring().
Update: I ultimately went with a regex like Killersponge suggested:
private readonly Regex reLot = new Regex(REGEX_LOT, RegexOptions.Compiled);
const string REGEX_LOT = "^(?<Field1>.{6})" +
"(?<Field2>.{16})" +
"(?<Field3>.{12})";
I then use the following to access the fields:
Match match = reLot.Match(record);
string field1 = match.Groups["Field1"].Value;
Use FileHelpers.
Example:
[FixedLengthRecord()]
public class MyData
{
[FieldFixedLength(8)]
public string someData;
[FieldFixedLength(16)]
public int SomeNumber;
[FieldFixedLength(12)]
[FieldTrim(TrimMode.Right)]
public string someMoreData;
}
Then, it's as simple as this:
var engine = new FileHelperEngine<MyData>();
// To Read Use:
var res = engine.ReadFile("FileIn.txt");
// To Write Use:
engine.WriteFile("FileOut.txt", res);
Substring sounds good to me. The only downside I can immediately think of is that it means copying the data each time, but I wouldn't worry about that until you prove it's a bottleneck. Substring is simple :)
You could use a regex to match a whole record at a time and capture the fields, but I think that would be overkill.
Why reinvent the wheel? Use .NET's TextFieldParser class per this how-to for Visual Basic: How to read from fixed-width text files.
You may have to watch out, if the end of the lines aren't padded out with spaces to fill the field, your substring won't work without a bit of fiddling to work out how much more of the line there is to read. This of course only applies to the last field :)
Unfortunately out of the box the CLR only provides Substring for this.
Someone over at CodeProject made a custom parser using attributes to define fields, you might wanna look at that.
Nope, Substring is fine. That's what it's for.
You could set up an ODBC data source for the fixed format file, and then access it as any other database table.
This has the added advantage that specific knowledge of the file format is not compiled into your code for that fateful day that someone decides to stick an extra field in the middle.

Categories