CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2008
    Posts
    12

    Searching a String Array

    Hi,
    At the moment I have a string array setup with:
    Code:
    ID:2 Avaliablitiy: Available Name:Daveship

    What I would like to do is search this array for "Available" and replace it with Unavailable. Does anyone know if this can be done and if so how to do it?



    Thanks.

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Searching a String Array

    Er, you can loop through the array item by item, and for each item String you can use the String.replaceAll(..) method to replace all instances of 'Available' with 'Unavailable' in the String...

    Is that what you wanted?

    The biggest difference between time and space is that you can't reuse time...
    M. Furst
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Apr 2008
    Posts
    12

    Re: Searching a String Array

    Not really. What ive got to do is have a boat hire company with rowingboats and pedal boats. Ive manged to make constuctors that will create the boats such as:
    public RowingBoat(String a, int s, String Name)

    Now ignoring pedal boats, my rowingboat array will look like this:
    Code:
    ID:1 Avaliablitiy: Available Name:Tedship
    ID:2 Avaliablitiy: Available Name:Daveship
    ID:3 Avaliablitiy: Available Name:Peteship
    and what I want to do is be able to change a rowingboat from Available to Unavailable when it is hired out (this will be changed with user input from a menu)

    So bassically what its got to do is search the string array for "Available" and replace it with Unavailable. Can this be done?

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Searching a String Array

    Quote Originally Posted by fortune2k
    So bassically what its got to do is search the string array for "Available" and replace it with Unavailable. Can this be done?
    ? That's just what I described in my previous email... can you explain a bit more how what you want to do is different from what I suggested?

    But I don't understand why you are storing that information in the array concatenated as a single String. If you have a RowingBoat class, why not use that in the array? When you want to chage a boat from available to unavailable, you only need to call boat.setAvailable(false) (assuming you have such a method), which sets the value of the boolean 'available' flag in Boat. Then if you want to know whether a boat is available, you can call boat.isAvailable(), which returns the value of the boolean 'available' flag in Boat.

    It is better to have an approximate answer to the right question than an exact answer to the wrong one...
    J. Tukey
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Apr 2008
    Posts
    12

    Re: Searching a String Array

    I think ive done it, thankyou for your help
    Last edited by fortune2k; April 19th, 2008 at 10:41 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured