473,396 Members | 1,805 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 to break on string into two seperate variable or strings in C#

Just wondering how you would break a string such as first and last name
together, into two separate strings or variables, each containing the
firstName and lastName. I am a newbie at C# so any help would be great!

Thanks

Brad
Nov 15 '05 #1
6 15450
rb
FA Q
-----Original Message-----
Just wondering how you would break a string such as first and last nametogether, into two separate strings or variables, each containing thefirstName and lastName. I am a newbie at C# so any help would be great!
Thanks

Brad
.

Nov 15 '05 #2
[Removed microsoft.public.dotnet.csharp.general which isn't a valid
newsgroup, and comp.programming as I'm not sure whether it's on-topic
there.]

Brad Britton <brad.britton@_NOTASPAMMER_shaw.ca> wrote:
Just wondering how you would break a string such as first and last name
together, into two separate strings or variables, each containing the
firstName and lastName. I am a newbie at C# so any help would be great!


Have a look at String.Substring, String.IndexOf and String.Split.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #3
You can use the Split method of the String class.
Sample:
String sName = "Name1 Name2";
String[] sResult = sName.Split(' ');
Sacalul
-----Original Message-----
Just wondering how you would break a string such as first and last nametogether, into two separate strings or variables, each containing thefirstName and lastName. I am a newbie at C# so any help would be great!
Thanks

Brad
.

Nov 15 '05 #4
In addition remember that you can split from more separators...
String[] sResult = sName.Split( ' ', ';', ',' '\' ); // and so on
"Brad Britton" <brad.britton@_NOTASPAMMER_shaw.ca> ha scritto nel messaggio
news:N5***********************@news3.calgary.shaw. ca...
Just wondering how you would break a string such as first and last name
together, into two separate strings or variables, each containing the
firstName and lastName. I am a newbie at C# so any help would be great!

Thanks

Brad

Nov 15 '05 #5
You could check out Regular Expressions as well.

"Brad Britton" <brad.britton@_NOTASPAMMER_shaw.ca> wrote in message news:<N5***********************@news3.calgary.shaw .ca>...
Just wondering how you would break a string such as first and last name
together, into two separate strings or variables, each containing the
firstName and lastName. I am a newbie at C# so any help would be great!

Thanks

Brad

Nov 15 '05 #6
Here's one way:

string fullName = "your name";
string[] firstAndLast = fullName.Split(new char[] { ' ' });

// results:
// firstAndLast[0] == "your";
// firstAndLast[1] == "name";
--------------------
Content-Class: urn:content-classes:message
From: "rb" <r@ro.com>
Sender: "rb" <r@ro.com>
References: <N5***********************@news3.calgary.shaw.ca >
Subject: How to break on string into two seperate variable or strings in C#
Date: Wed, 30 Jul 2003 19:46:04 -0700
Lines: 16
Message-ID: <07****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Thread-Index: AcNXDeJq/ooy42iUSVawb2gRjNMrZA==
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Newsgroups: microsoft.public.dotnet.languages.csharp
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:173141
NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

FA Q
-----Original Message-----
Just wondering how you would break a string such as first

and last name
together, into two separate strings or variables, each

containing the
firstName and lastName. I am a newbie at C# so any help

would be great!

Thanks

Brad
.

--

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.

Nov 15 '05 #7

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

Similar topics

17
by: Olivier Bellemare | last post by:
I've tried to make a function that returns the middle of a string. For example: strmid("this is a text",6,4); would return "is a". Here is my code: char *strmid(char *texte, int depart,...
6
by: Chris Simmons | last post by:
I know that a String is immutable, but I don't understand why this piece of code fails in nUnit: // BEGIN CODE using System; class Test { public static void Main( String args )
3
by: chris | last post by:
Hi there, I have a string containing of a letter and numbers, i.e. "B1234". How can I saperate the numbers and the letter in two seperate strings? string1 = B string2 = 1234 I also have...
1
by: Justin | last post by:
I have a string of dates seperated by commas and I need to seperate them and put them into a dropdown control. With the code below I am able to only remove the comma which results in one big long...
8
by: KRoy | last post by:
I have a password stored in the Registry encrypted using System.Security.Cryptography DES Algorithm. I supplied it a password and a Initialization Vector. I am trying to decrypt it using the...
1
by: RSummersJr | last post by:
Hello, I have been placed in charge of migrating an old access based database over to sql server 7.0. So far, I have imported all the tables into sql server, but now I have come across the...
17
by: Christoph Scholtes | last post by:
Hi, I have two questions about the following code snippet. I am trying to read in a series of strings and save them to character arrays. Since I dont know how long my string is going to be (and...
4
by: deerchao | last post by:
Hi, I'd like to render colorful text with word wrap on screen (kind of inline <fonttag to change text color in html), but run into the line break problem. Graphics provides several DrawString...
7
by: jeddiki | last post by:
Hi, I am using a function called htmlwrap() which states that it does NOT add a "<br>" to the 70 character line so that it forces a line wrap. ( the script safely wraps long words without...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.