473,396 Members | 1,784 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,396 software developers and data experts.

How do I keep one line in a String and delete the rest?

Hi

I would like to remove everything else but one line of text in a String.

I have tried using .Split("\n"), however I did not succeed.

The string contains three lines, but I only need the string to contain the
first line.

Can anybody help me?

Thanks

Daniel
Aug 14 '06 #1
5 1395
Actually it's not "\n" it's "\r\n" if I'm not mistaken. In any case
Environment.NewLine.

string[] ts = t.Split(new string[] { Environment.NewLine },
StringSplitOptions.None); // written with VS2005

now you have an array of lines. you can do what you need to...

Mike

http://www.seeknsnatch.com

"Daniel" <fj*******************@hotmail.comwrote in message
news:44*********************@dreader1.cybercity.dk ...
Hi

I would like to remove everything else but one line of text in a String.

I have tried using .Split("\n"), however I did not succeed.

The string contains three lines, but I only need the string to contain the
first line.

Can anybody help me?

Thanks

Daniel


Aug 14 '06 #2
"Daniel" <fj*******************@hotmail.comwrote in message
news:44*********************@dreader1.cybercity.dk ...
I would like to remove everything else but one line of text in a String.

I have tried using .Split("\n"), however I did not succeed.

The string contains three lines, but I only need the string to contain the
first line.
How do you know that Split didn't work...? Did you get any error messages?
What was the length of the array created by Split?

If your string is delimited by \n, then <stringVariable>.Split("\n")[0]
should give you the first line only...
Aug 14 '06 #3
some of your questions are answered here:
http://msdn2.microsoft.com/en-us/library/b873y76a.aspx

Normally, no exceptions, see the doc for return values in various
situations.
If it's delimited by '\n' characters you'll need to use \n with single
quotes around the \n: '\n'.
Yes, with the [0] at the end you'll get the first line, but you'll probably
get exceptions if you're not careful about the text that you're reading in.

Mike
http://www.seeknsnatch.com

"Mark Rae" <ma**@markNOSPAMrae.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
"Daniel" <fj*******************@hotmail.comwrote in message
news:44*********************@dreader1.cybercity.dk ...
>I would like to remove everything else but one line of text in a String.

I have tried using .Split("\n"), however I did not succeed.

The string contains three lines, but I only need the string to contain
the first line.

How do you know that Split didn't work...? Did you get any error messages?
What was the length of the array created by Split?

If your string is delimited by \n, then <stringVariable>.Split("\n")[0]
should give you the first line only...

Aug 14 '06 #4

Thank you for all the answers :-)

By using:

<stringVariable>.Split('\n')[0]

everything worked out perfectly!

Thanks

Daniel
Aug 15 '06 #5
Hi,

stringvar.Substring( 0, stringvar.IndexOf( Environment.NewLine) )
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Daniel" <fj*******************@hotmail.comwrote in message
news:44*********************@dreader1.cybercity.dk ...
Hi

I would like to remove everything else but one line of text in a String.

I have tried using .Split("\n"), however I did not succeed.

The string contains three lines, but I only need the string to contain the
first line.

Can anybody help me?

Thanks

Daniel


Aug 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dakkar | last post by:
I have a txt file like this ClientFirstLaunch=no NextLoginKey=255 LastServerID=1 PlayerName=Dakkar DefaultChar=0 LastTip=3
6
by: tomtown.net | last post by:
Hello I'm trying to get a single line removed from a text file using a search pattern (pretty simple: if line contains "NODE1") -> remove line). To achieve this I's like to operate with only the...
1
by: ricky | last post by:
I am a student doing internship. Iam doing a project. I need to remove or update a line in the file. So far 1.I can read from file 2. I can write to the file 3. I can find the string in the file...
7
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
13
by: programming | last post by:
how do i delete from a text file 1 of the following lines: jon|scott adam|smith <--delete paul|clark say i would like to delete the middle line of this txt, in member.txt what php code or...
10
blazedaces
by: blazedaces | last post by:
Alright guys, so the title explains exactly my goal. The truth is I'm going to be reading in a lot of data from an xml file. The file is too large and there's too much data to store in arraylists...
2
dbanning
by: dbanning | last post by:
I am trying to remove part of a string and place it into another variable the string is a filename eg "C:\help\demo\sample.txt" I want to remove and resave into a new string just the file...
4
by: MartinRinehart | last post by:
Thinking about unclosed multi-line quotes. When you open a multi-line quote (type '"""') what does your editor do? Does it color the remainder of your text as a quote, or does it color the line...
8
by: Rob | last post by:
I have a vector of a class type and when I create an object inside a function and return that and add it to the vector, it doesn't properly keep the data inside it. So I have a few questions: 1....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.