Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 30th, 2003, 06:52 PM
Registered User
 
Join Date: Jul 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to megabytes
Default How to read a string with a space?

I was just wondering how you would read a string with a space? For example First and Last name "Brad Britton" when this is typed in by the user? I have seen the code for with a comma, but what about just a space? Thanks in advance!

Cheers

Brad

 
Old July 30th, 2003, 08:05 PM
Registered User
 
Join Date: Jul 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to megabytes
Default

also i need to split the two parts into seperate variables.

Thanks,

Brad

 
Old July 31st, 2003, 01:11 AM
Registered User
 
Join Date: Jul 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,
try this:

string FullName = "Brad Britton";
int SpacePosition = FullName.IndexOf(" ",0);//the position of Space
string FirstName = FullName.Substring(0, SpacePosition);//First Name Variable
string LastName = FullName.Substring(SpacePosition, FullName.Length - SpacePosition);//last Name Variable

   //Print results
Console.WriteLine("Full name is " + FullName);
Console.WriteLine("First name is " + FirstName);
Console.WriteLine("Second name is " + LastName);



 
Old July 31st, 2003, 02:24 AM
Registered User
 
Join Date: Jun 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Brad,

try this:

using System;
class Test
{
  public static void Main()
  {
    string s = "Oh, I hadn't thought of that.";
    char[] separators = new char[]{ ' ', ',' };
    foreach( string sub in s.Split( separators ) )
    {
      Console.WriteLine( "Word: {0}", sub );
    }
  }
}

Hope this helps,
Boris

GFT Solutions GmbH
www.hyparchiv.com
 
Old August 1st, 2003, 03:26 PM
Registered User
 
Join Date: Jul 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to megabytes
Default

I got it going! Thanks for your help! How would I go about getting my name to print B. Britton? Thank you so much for your help!

Thanks,

Brad:D






Similar Threads
Thread Thread Starter Forum Replies Last Post
remove commas and space in a string kumiko Classic ASP Basics 1 January 10th, 2008 11:42 PM
Remove the unwanted space at end of the string gmbalaa General .NET 2 August 21st, 2007 11:35 PM
I Need to Read an Xml from String anand_asv XML 3 March 18th, 2005 12:04 AM
read a string from a comport john.wijnands Access VBA 1 September 8th, 2003 10:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.