Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   Help with breaking apart a string (http://www.programmingforums.org/showthread.php?t=12925)

csrocker101 Apr 2nd, 2007 2:22 PM

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

Infinite Recursion Apr 2nd, 2007 2:32 PM

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.");


kurifu Apr 3rd, 2007 12:00 AM

Makes me wonder how I used to tolerate doing string manipulation in C.

Infinite Recursion Apr 3rd, 2007 9:35 AM

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.

csrocker101 Apr 4th, 2007 8:30 AM

Thanks so much Infinite Recursion Helped alot! :D

Infinite Recursion Apr 4th, 2007 9:50 AM

np man, glad to help.

Ooble Apr 6th, 2007 8:50 AM

Quote:

Originally Posted by Infinite Recursion (Post 126123)
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.


All times are GMT -5. The time now is 2:24 AM.

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