Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello there.

I have to read a .txt file that has fixed line lenght. Think this line lenght is 12. Below is an example of this .txt :

-first line
-second line
-third line
continues
-fourh l.....

If lenght of a line is bigger than it's fixed lenght, it's written to a new line.

But my problem is, i want to join this new line strings that continues. I want :

-first line
-second line
-third line continues
-fourh l....

But i couldn't accomplish it.
Edit : The contidion is, if line starts with a '-' char, this is a new line. If not, join them.

Any help?

Thanks from now..
Posted
Updated 26-Feb-14 21:09pm
v2
Comments
dan!sh 27-Feb-14 3:16am    
What have you tried?
ahmetkocadogan 27-Feb-14 3:21am    
I read the lines of .txt file into a List<string>. And tried to join strings with foreach loop, with

if(line.substring(0,1)=="-")
do sth..
else
do sth..

But the logic with do sth.. is wrong. And i couldn't make it. And trying to find a way..

That is a pretty simple task: at each step you have to read the input file (line by line), collecting and concatenating the read lines until a stop condition is met. The stop condition is either a new start of line (the hiphen) or the end of file. Upon stop condition the collected lines are written to the output file (and the new line, if any, is collected).
 
Share this answer
 
It's not exactly complex...
C#
string[] input = new string[] {"-first line",
                               "-second line",
                               "-third line ",
                               "continues",
                               "-fourh l.....",};
List<string> list = new List<string>();
string old = "";
foreach (string s in input)
    {
    string line = s;
    if (!line.StartsWith("-"))
        {
        line = string.Format("{0}{1}", old, line);
        }
    list.Add(line);
    old = line;
    }
input = list.ToArray();
 
Share this answer
 
Comments
ahmetkocadogan 27-Feb-14 5:59am    
I wrote similar codes. I know it looks, and definetely is really easy.
Here is the output of your code.

-first line
-second line
-third line
-third line continues
-fourh l.....

I don't want the '-third line' line. I want only the '-third line continues' line.
This is what i can't do. My brain is away or sth...
OriginalGriff 27-Feb-14 6:09am    
So add one line:
if (!line.StartsWith("-"))
{
if (list.Count > 0) list.RemoveAt(list.Count - 1);
line = string.Format("{0}{1}", old, line);
}
ahmetkocadogan 27-Feb-14 6:17am    
Thanks dude..
OriginalGriff 27-Feb-14 6:26am    
You're welcome!
Matt T Heffron 27-Feb-14 13:57pm    
Griff, Really!?
Using
string.Format("{0}{1}", old, line);
instead of
string.Concat(old, line);
or a StringBuilder...
You're getting sloppy in your old age. ;-)
You can make this easier by just reading the entire file into a string using File.ReadAllText [^].

Then, you can perform some simple search-and-replace operations to transform the data you just read:
C#
private string SampleData = @"-first line
-second line
-third line
continues
-fourh l";

private string replaceString = "-";

private string MassageData(string theData)
{
    theData = theData.Replace(Environment.NewLine, " ");
    
    // use this to remove the 'replaceString
    // theData = theData.Replace(replaceString, Environment.NewLine);

    // use this to keep the 'replaceString
    theData = theData.Replace(replaceString, Environment.NewLine + replaceString);
     
    // optional removal of extra spaces ...
    theData = theData.Replace("  ", " ");
    theData = theData.Trim();
}

// test
private void TestTransformData()
{
    string transformedData = MassageData(SampleData);
    Console.WriteLine(transformedData);
}
 
Share this answer
 
I've found stuff like this to be useful as an extension method as well, so I'm just posting an alternative for future visitors.

C#
public static string[] JoinWithCondition(this string[] pInput, char pCondition)
{
	string input = string.Join(string.Empty, pInput);
	List<string> output = new List<string>();
	string value;
	
	for (int i = 0; i < input.Length; i++)
	{
		if (input[i] == pCondition)
		{
			if (!string.IsNullOrEmpty(value)) output.Add(value);
			value = string.Empty;
			
			continue; // remove to keep condition character
		}
		
		value += input[i];
	}
	
	return output.ToArray();
}</string></string>
 
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