Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
lets say i got a string which has 2 lines in it

[] = the new line like enviermentail line and it not part of the string but is an example of collection of strings

C#
StringCollection text = "That [] Hat";


so as you can see there are 2 lines, "That" is on line 1 and "Hat" is on line 2

I want to be able to read line 2, for example

C#
this.text = text.line2.tostring();

and text.line2.tostring(); will be "Hat"

so i was wondering if some one can help me achieve this goal?
Posted
Comments
Kenneth Haugland 15-Aug-12 5:53am    
\n is new line in a string...
Andreas Gieriet 15-Aug-12 5:54am    
Homework? If yes, please mark as such in the tags...
Cheers
Andi
[no name] 15-Aug-12 6:51am    
not not even finished school, im a couple of years of making this homework
Andreas Gieriet 15-Aug-12 6:59am    
Your way of expressing things is beyond my capabilities. I give up.
Good luck!
Andi

try something like this.

C#
static void Main(string[] args)
       {
           StringCollection TEXT = new StringCollection();
           TEXT.Add("That");
           TEXT.Add(Environment.NewLine);
           TEXT.Add("Hat");
           TEXT.Add(Environment.NewLine);
           TEXT.Add("Hat2");
           int i = 0;
           foreach (string str in TEXT)
           {
               if (str==Environment.NewLine)
                   i=i+1;
               else if(i==2)
                   Console.WriteLine(str);
           }
           Console.Read();
       }



here i==2 means i want 2nd line..it will return me Hat2
if i give i==1 it will give me 1st line..it will return me Hat
 
Share this answer
 
v2
Comments
[no name] 15-Aug-12 6:44am    
I took some of the code to make it look simlar to what i want it


foreach (string str in Properties.Settings.Default.Dock1)
{
if (addpicnumber == 2)
{
this.Text = str;
}
}

right now it just reads the last line, but haven't fully understoon the code, still trying to find the part where i pick the line the text is on
Santhosh Kumar Jayaraman 15-Aug-12 6:45am    
you looking for some explanaton for my code?
[no name] 15-Aug-12 6:48am    
yeh, just the part where it will read the certain line plz
Santhosh Kumar Jayaraman 15-Aug-12 6:54am    
Ok i am setting an integer variable i=0..this will be the counter to check how many new lines are there.
int i = 0;
foreach (string str in TEXT)
{

Now whenever the string is Environment.newline , am incrementing the i variable. So when it comes first, then i=1. Then it willgo to the next text in Stringcollection. here am setting which line i want to get..If i need hat in my example then it comes after first environment.newline.. So when it is reading next line i.2 Hat i am checking i==1, and it will be true , so it will return Hat.

Incase if i want 3rd line.i.e.Hat2 which comes after 2nd enviroment.newline,, i am checking i==2..Like that
if (str==Environment.NewLine)
i=i+1;
else if(i==2)
Console.WriteLine(str);
}
C#
string GetLine(string text, int lineNo) 
{ 
  string[] lines = text.Replace("\r","").Split('\n'); 
  return lines.Length >= lineNo ? lines[lineNo-1] : null; 
} 
 
Share this answer
 
v2
Comments
[no name] 15-Aug-12 6:27am    
please explain all
Prasad_Kulkarni 17-Aug-12 0:20am    
5'ed
Manas Bhardwaj 17-Aug-12 3:09am    
thx!
Your question is a bit unclear:
- your [] seems not to be real, but rather for \n, etc, correct (see below)?
- do the lines need to have stripped off the leading and trailing white spaces?
- does it need to handle \r\n, \n\r, \r, \n?

BTW: See Escaping in C#: characters, strings, string formats, keywords, identifiers[^] for escapes in a string.

A solution may be:
C#
// split at new lines, remove empty lines, remove leading and trailing white spaces
public static string[] GetLines(string fullText)
{
    return fullText.Split(new[] {'\n'}, StringSplitOptions.RemoveEmptyEntries)
                   .Select(line => line.Trim())
                   .ToArray();
}


Cheers
Andi
 
Share this answer
 
Comments
[no name] 15-Aug-12 6:31am    
im tring to read a line number from string like i want to read the text in line 4
Andreas Gieriet 15-Aug-12 6:39am    
You get an array, so, access it as follows:

string fullText = ... // e.g. File.ReadAllText(someFileName);
var lines = GetLines(fullText);
foreach(var line in lines)
{
Console.WriteLine(line);
}
// or random access lines:
Console.WriteLine("Line 1: {0}", lines [0]);
// note: 1st line at index pos 0
// 2nd line at index pos 1
// etc.
<code>

Cheers
Andi
[no name] 16-Aug-12 9:23am    
can you plz convert this code into a stringcollcion? not file text lines?
Andreas Gieriet 16-Aug-12 14:40pm    
I leave that for your exercise.
If you don't manage to do that, please acquire the very basics of (C#) programming first. There are plenty of valuable tutorials around.
Cheers
Andi
[no name] 16-Aug-12 21:34pm    
not for this part, thats why i asked

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