I need to read a block of data from this text. The block starts with the T|DataObject.EShop.Tic.TicVente| line and ends with T|DataObject.EShop.Tic.TicPaiement|.
I only want the lines that start with D between the previous strings.
W|301500120100407213036|
M|SYP||
T|DataObject.EShop.Tic.TicVente|
C|ArtId|ArtRef|PrxInit|QteArt|PrxEntId|TvaId|TvaTaux|RetourId|RetourMagAchat|RetourTicExtIdAchat|RetourDatAchat|PosteNum|TicId|LigNum|PrxAppel|PrxPaye|DatMaj|
D|18250168145|1825016814503131|1690|-1|0934489998|1|0|C|150||20100406000000|1|009700001|1|1690|1690|20100407093455|
D|18250137020|1825013702002161|750|1|1002689999|1|0|||||1|009700001|2|750|750|20100407093455|
D|18260013233|1826001323336111|1990|1|0935689998|1|0|||||1|009700002|1|1990|1990|20100407103918|
T|DataObject.EShop.Tic.TicPaiement|
C|PosteNum|TicId|LigNum|PaieId|Mnt|DevId|MntDev|Info1|Info2|TransId|TransOK|DatMaj|
D|1|009700001|1|01|-940|SYP|-940|||||20100407093455|
D|1|009700002|1|01|4000|SYP|4000|||||20100407103918|
T|DataObject.EShop.Tic.TicVenteAnnulee|
C|PosteNum|Dat|SessId|CliTypId|CliId|UtilId|LotId|ArtId|ArtRef|PrxInit|QteArt|PrxEntId|TvaId|TvaTaux|RetourId|RetourMagAchat|RetourTicExtIdAchat|RetourDatAchat|TicId|LigNum|PrxAppel|PrxPaye|DatMaj|
D|1|20100407105721|0097001|||6|0150010097763|18250040037|1825004003704121|990|1|1002689999|1|0|||||009700004|1|990|990|20100407213033|T|DataObject.EShop.Tic.TicVenteAnnulee|
C|PosteNum|Dat|SessId|CliTypId|CliId|UtilId|LotId|ArtId|ArtRef|PrxInit|QteArt|PrxEntId|TvaId|TvaTaux|RetourId|RetourMagAchat|RetourTicExtIdAchat|RetourDatAchat|TicId|LigNum|PrxAppel|PrxPaye|DatMaj|
D|1|20100407105721|0097001|||6|0150010097763|18250040037|1825004003704121|990|1|1002689999|1|0|||||009700004|1|990|990|20100407213033|
What about Regular Expressions?
You said:
starts in "T|DataObject.EShop.Tic.TicVente|" line
So it will begin with "^T|DataObject.EShop.Tic.TicVente|$"
You said:
T|DataObject.EShop.Tic.TicPaiement|
So it will end with "$T|DataObject.EShop.Tic.TicPaiement|$"
What else do you need? Each line starts with D? OK... try this
Regex rgx = new Regex("^T|DataObject.EShop.Tic.TicVente|$(D[.]*)$T|DataObject.EShop.Tic.TicPaiement|$", RegexOptions.MultiLine);
Or you can easily parse
set a flag indicating you are searching for your starting string
read lines until you find your start string (or EOF)
set a flag that indicates you are searching for the closing string
read lines until you find your ending string (or EOF)
when ending string found, set flag that
print all lines read between starting string and ending string
Well, you could find the first reference of T|DataObject, verify that it is the type you are looking for, then look for the next reference of T|DataObject.
Seems like it's just some simple string manipulation.
Is all of this on a single line or are the object header separated by carriage returns as well?
UPDATE
This is by no means the best way to do this and is just one possible way:
String sRecords = "T|DataObject.Test|C|RecordHeaderId|D|123|D|234|T|DataObject.Test2|C|RecordHeaderId|D|2345|D2366";
// this will split the string into an array on the boundary of |.
// which means you'll have all of item individual items separated out.
String[] sArray = sRecords.Split('|');
List<String> objects = new List<string>();
String obj = String.Empty;
foreach (String s in sArray) {
// locate the T item which defines a new record definition.
// the danger is if your data contains a "T" value somewhere it shouldn't
if (s.Equals("T") && !String.IsNullOrEmpty(obj)) {
objects.Add(obj);
obj = String.Empty;
}
obj = String.Format("{0}|{1}", obj, s);
}
objects.Add(obj);
// at this point "objects" hold a list of strings, each defined by the record type.
foreach (String s in objects) {
listBox1.Items.Add(s);
}
Related
I need to replace multiple strings in a textfile. This is my code:
List<string> list = new List<string>();
string Text = File.ReadAllText(temp);
list.Add(Text.Replace("name", name));
list.Add(Text.Replace("name2", name2));
list.Add(Text.Replace("1.0000", CR));
list.Add(Text.Replace("0.6590", CG));
list.Add(Text.Replace("0.0000", CB));
foreach (string txt in list)
{
File.WriteAllText(path, txt);
}
When I debug I can see the strings beeing replaced one after one, but when the next string is about to be replaced, the last string will then go back to its old value. Is there a way to replace multiple strings in a textfile?
You don't need a list for this, but you do need to save the changes in the resulting string each time you do a replacement, otherwise you lose the changes.
The Replace method returns a new string with the replacement, so you can chain the calls to Replace and it will end up returning a string with all your changes.
Here's an example:
string text = File.ReadAllText(temp)
.Replace("name", name)
.Replace("name2", name2)
.Replace("1.0000", CR)
.Replace("0.6590", CG)
.Replace("0.0000", CB);
File.WriteAllText(path, txt);
So, I've been learning C# and I need to remove everything after the
":" character.
I've used a StreamReader to read the text file, but then I can't use the Split function, then I tried it by using an int function to import it, but then again I can't use the Split function?
What I want this to do is import a text file that's written like;
name:lastname
name2:lastname2
And so that it only shows name and name2.
I've been searching this for a couple of days but I can't seem to figure it out!
I don't know what I'm doing wrong and how to import the text file without using StreamReader or anything else.
Edit:
I'm trying to post something to a website that goes like;
example.com/q=(name without ":")
Edit 2:
StreamReader list = new StreamReader(#"list.txt");
string reader = list.ReadToEnd();
string[] split = reader.Split(":".ToCharArray());
Console.WriteLine(split);
gives output as;
System.String[]
You've got a few issues here. First, use File.ReadLines() instead of a StreamReader, its much simpler and easier:
IEnumerable<string> lines = File.ReadLines("path/to/file");
Next, your lines variable needs to be iterated so you can get to each line of the collection:
foreach (string line in lines)
{
//TODO: write split logic here
}
Then you have to split each line on the ':' character:
string[] split = line.Split(":");
Your split variable is an array of string (i.e string[]) which means you have to access a specific index of the array if you want to see its value. This is your second issue, if you pass split to Console.WriteLine() under the hood it just calls .ToString() on the object you have passed it, and with a string[] it won't automatically give you all the values, you have to write that yourself.
So if your line variable was: "name:Steve", the split variable would have two indexes and look like this:
//split[0] = "name"
//split[1] = "Steve"
I made a fiddle here that demonstrates.
I your file size small and your name:lastname in one line use:
var lines = File.ReadAllLines("filaPath");
foreach (var line in lines)
{
var array = line.Split(':');
if (array.Length > 0)
{
var name = array[0];
}
}
if name:lastname isn't in new line tell me how it's seprated
I'm making a simple text adventure in C# and I was wondering if it was possible to read certain lines from a .txt file and assign them to a string.
I am aware of how to read all the text from a .txt file but how exactly would I assign the contents of certain lines to a string?
Have you considered the ReadAllLines method?
It returns an array of lines from which you can choose your desired line.
So for eg, if you wish to choose the 3rd line (Assuming you have 3 lines in the file):
string[] lines = File.ReadAllLines(path);
string myThirdLine= lines[2];
Probably the easiest (and cheapest in terms of memory consumption) is File.ReadLines:
String stringAtLine10 = File.ReadLines(path).ElementAtOrDefault(9);
Note that it is null if there are less than 10 lines in the file. See: ElementAtOrDefault.
It's just the concise version of a StreamReader and a counter variable which increases on every line.
As an advanced alternative: ReadLines plus some LINQ:
var lines = File.ReadLines(myFilePath).Where(MyCondition).ToArray();
where MyCondition:
bool MyCondition(string line)
{
if (line == "something")
{
return true;
}
return false;
}
In case you don't want to load all lines atonce
using(StreamReader reader=new StreamReader(path))
{
String line;
while((line=reader.ReadLine())!=null)//process temp
}
Here's a example how you can assign the lines to a string, you can't decide which line is which via fields, you have to select them yourself.
which is the line of the string you want to assign.
For example, you want line one, you define which as one and not zero, you want line eight, you define which with eight.
string getWord(int which)
{
string readed = "";
using (Systen.IO.StreamReader read = new System.IO.StreamReader("PATH HERE"))
{
readed = read.ReadToEnd();
}
string[] toReturn = readed.Split('\n');
return toReturn[which - 1];
}
I'm simply trying to execute File.ReadAllLines against a specific file and, for every line, split on |. I have to use regex on this one.
This code below doesnt work, but you'll see what i'm trying to do:
string[] contents = File.ReadAllLines(filename);
string[] splitlines = Regex.Split(contents, '|');
foreach (string split in splitlines)
{
//Regex line = content.Split('|');
//content.Split('|');
string prefix = prefix = Regex.Match(line, #"(\S+)(\d+)").Groups[0].Value;
File.AppendAllText(workingdirform2 + "configuration.txt", prefix+"\r\n");
}
It's not entirely clear to me what you are trying to do, but there are a number of errors in your code. I have tried to guess what you are doing, but if this isn't what you want, please explain what you do want preferably with some examples:
string inputFilename = "input.txt";
string outputFilename = "output.txt";
using (StreamWriter streamWriter = File.AppendText(outputFilename))
{
using (StreamReader streamReader = File.OpenText(inputFilename))
{
while (true)
{
string line = streamReader.ReadLine();
if (line == null)
{
break;
}
string[] splitlines = line.Split('|');
foreach (string split in splitlines)
{
Match match = Regex.Match(split, #"\S+\d+");
if (match.Success)
{
string prefix = match.Groups[0].Value;
streamWriter.WriteLine(prefix);
}
else
{
// Handle match failed...
}
}
}
}
}
Key points:
You seem to want to perform an operation on each line, so you need to iterate over the lines.
Use the simple string.Split method if you want to split on a single character. Regex.Split doesn't accept a character and "|" has a special meaning in regular expressions so it wouldn't have worked anyway unless you escaped it.
You were opening and closing the output file multiple times. You should open it just once and keep it open until you have finished writing to it. The using keyword is useful here.
Use WriteLine instead of appending "\r\n".
If the input file is large, use a StreamReader instead of ReadAllLines.
If the match fails, your program will throw an exception. You probably should check match.Success before using the match and if this returns false, handle the error appropriately (skip the line, report a warning, throw an exception with an appropriate message, etc.)
You aren't actually using groups 1 and 2 in the regular expression, so you can remove the parentheses to save the regular expression engine from having to store results that you won't use anyway.
You should pass the original string to Regex.Split and not an array.
Looks like you are using line instead of split when settings the prefix. Without knowing more about your code I cant tell if it's right or not but in any case it sticks out as the error.(it shouldnt build either)
This is a really inefficient on at least two levels :)
Regex.Split takes a string, not an array of strings.
I would recommend calling Regex.Split on each item of contents individually, then looping over the results of that call. This would mean nested for loops.
string[] contents = File.ReadAllLines(filename);
foreach (string line in contents)
{
string[] splitlines = Regex.Split(line);
foreach (string splitline in splitlines)
{
string prefix = Regex.Match(splitline, #"(\S+)(\d+)").Groups[0].Value;
File.AppendAllText(workingdirform2 + "configuration.txt", prefix+"\r\n");
}
}
This, of course isn't the most efficient way to go about it.
A more efficient way might be to split on a regular expression instead. I think this works:
string splitlines = Regex.Split(File.ReadAllText(filename), "$|\\|");
I have to assume, based on the limited feedback, that this is what you're looking for:
string inputFile = filename;
string outputFile = Path.Combine( workingdirform2, "configuration.txt" );
using ( StreamReader inputFileStream = File.OpenText( inputFile ) )
{
using ( StreamWriter ouputFileStream = File.AppendText( outputFile ) )
{
// Iterate over the file contents to extract the prefix
string currentLine;
while ( ( currentLine = inputFileStream.ReadLine() ) != null )
{
// Notice the updated Regex - your's is a bit broken
string prefix = Regex.Match( currentLine, #"^(\S+?)\d+" ).Groups[1].Value;
ouputFileStream.WriteLine( prefix );
}
}
}
This would take a file full of:
Text1231|abc|abc
Text1232|abc|abc
Text1233|abc|abc
Text1234|abc|abc
and place:
Text
Text
Text
Text
into a new file.
I hope this, at least, gets you on the right path. My crystal ball is getting hazy.. haaazzzy..
Probably one of the best way to process text files in C# is to use fileHelpers. Give it a look. It allows you to strongly type your import data.
I have a string which consists of different fields. So what I want to do is get the different text and assign each of them into a field.
ex: Hello Allan IBM
so what I want to do is:
put these three words in different strings like
string Greeting = "Hello"
string Name = "Allan"
string Company = "IBM"
//all of it happening in a loop.
string data = "Hello Allan IBM"
string s = data[i].ToString();
string[] words = s.Split(',');
foreach (string word in words) {
Console.WriteLine(word);
}
any suggestions?
thanks hope to hear from you soon
If I understand correctly you have a string with place-holders and you want to put different string in those place-holders:
var format="{0}, {1} {2}. How are you?";
//string Greeting = "Hello"
//string Name = "Allan"
//string Company = "IBM"
//all of it happening in a loop.
string data = ...; //I think you have an array of strings separated by ,
foreach( va s in data){
{
//string s = data[i];//.ToString(); - it is already a string array
string[] words = data[i].Split(',');
Console.WriteLine(format, words[0], words[1], words[2]);
}
To me it sound not like a problem that can be solved with a loop. The essential problem is that the loop can only work if you do exactly the same operation on the items within the loop. If your problem doesn't fit, you end up with a dozen of lines of code within the loop to handle special cases, what could have been written in a shorter way without a loop.
If there are only two or three strings you have to set (what should be the case if you have named variables), assign them from the indexes of the split string. An alternative would be using regular expressions to match some patterns to make it more robust, if one of the expected strings is missing.
Another possibility would be to set attributes on members or properties like:
[MyParseAttribute(/*position*/ /*regex*/)]
string Greeting {get;set;}
And use reflexion to populate them. Here you could create a loop on all properties having that attribute, as it sounds to me that you are eager to create a loop :-)