Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 2nd, 2007, 2:22 PM   #1
csrocker101
Programmer
 
Join Date: Dec 2006
Posts: 50
Rep Power: 2 csrocker101 is on a distinguished road
Help with breaking apart a string

FIrst off I want to thank everyone for being extremely helpful as I try to complete a pretty big computer science project. I have got a server application and a client application and a database application.The database application is connected with the server application which logs and updates the information sent to it via the client . Its rather simple to update the database via the client you send the server a carriage and line return. Basically updating the database consists of sending a username string and a location string to the streamwriter such as "322095 In the labs" to the server. Putting a space in between two strings tells the server to update. If you do not wish to update the database only one string is passed with no space. So for instance if you do not wish to update the database a string passed to the stream writer looks like this "322095" which will then tell you were the student is. The problem I am having is in my server. Here is my code
public void doRequest()
        {
            //streamReader reads incoming connection from client
            
            StreamWriter sw = new StreamWriter(socketStream);
            StreamReader sr = new StreamReader(socketStream);
            //writes in coming data
           //Assigns data from the streamreader to a string
            string clientInfo = sr.ReadLine();

Basically the string clientInfo is the information the client sends to the server via the streamwriter. My problem is that the string will just appear as "322095 In the labs" but I want to check this invididual string to see if there is a space in between it. So for instance:
public void doRequest()
        {
            //streamReader reads incoming connection from client
            
            StreamWriter sw = new StreamWriter(socketStream);
            StreamReader sr = new StreamReader(socketStream);
            //writes in coming data
           //Assigns data from the streamreader to a string
            string clientInfo = sr.ReadLine();
           if(clientInfo == IF THERE IS A SPACE BETWEEN THE VARIABLES) 
           {
                //updateDatabase Commands
           }
          else
          {
                 //dont update database
          }

Obviously this is pseduocode but I want to know if I can check if there is a space in between the read in string. Is there a way I can do this?? I think the main problem is that the two strings are combined into one when i send it via the streamwriter, but i want to know if theres some way I can check if there is a space in just one string. I would appreciate some help. Thanks
csrocker101 is offline   Reply With Quote
Old Apr 2nd, 2007, 2:32 PM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,473
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Use the IndexOf() function...

int pos = clientInfo.IndexOf(" ");
if (pos > 0)
   Console.WriteLine("Space in the string.");
else 
   Console.WriteLine("NO space in the string.");
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Apr 3rd, 2007, 12:00 AM   #3
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
Makes me wonder how I used to tolerate doing string manipulation in C.
__________________
Clifford Matthew Roche <geek@cliffordroche.com>
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Old Apr 3rd, 2007, 9:35 AM   #4
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,473
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Hah, That was quite a pain... In C, I would just loop through each element and compare it to " " and if it was equal would return true.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Apr 4th, 2007, 8:30 AM   #5
csrocker101
Programmer
 
Join Date: Dec 2006
Posts: 50
Rep Power: 2 csrocker101 is on a distinguished road
Thanks so much Infinite Recursion Helped alot!
csrocker101 is offline   Reply With Quote
Old Apr 4th, 2007, 9:50 AM   #6
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,473
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
np man, glad to help.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Apr 6th, 2007, 8:50 AM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Quote:
Originally Posted by Infinite Recursion View Post
Use the IndexOf() function...

int pos = clientInfo.IndexOf(" ");
if (pos > 0)
   Console.WriteLine("Space in the string.");
else 
   Console.WriteLine("NO space in the string.");
You'll want to check if pos is greater than or equal to 0 - a space can be the first character in a string too. If there is no space, the function will return -1.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Function Parameters grimpirate PHP 10 Mar 14th, 2007 7:55 PM
C# corruption!!! Kilo C++ 32 May 21st, 2006 9:44 PM
Array issues :( Alo Tsum Java 10 Nov 26th, 2005 6:45 PM
A standards question, optional inputs into Methods Arla C# 4 Apr 27th, 2005 11:38 PM
replace space with ' * ' TecBrain C++ 15 Apr 13th, 2005 1:32 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:01 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC