0

I am attempting to build a string using a for() loop to iterate over the elements of an ArrayList. I want to append a coma after each element of the ArrayList is appended on to the String that I'm building, but I don't want to append the coma after the last element of the ArrayList has been appended.

This is what I've attempted so far, but a coma is still added in after the last element.

public String buildWOEIDQuery(ArrayList searchCriteria)
{
    String query = "http://where.yahooapis.com/v1/places.q('";
    for (int i = 0; i < searchCriteria.Count; i++ )
    {
        if(searchCriteria.)

        query += (String)searchCriteria[0];
        if(i != searchCriteria.Count)
        {
            query += ",";
        }
    }
    query += "')?appid=" + devKey;

    return query;
}

The query should end up looking like this if, for example there are 3 search criteria:

http://where.yahooapis.com/v1/places.q('criteria1,criteria2,criteria3')?appid=dj0yJmk9Tm

If i is equal to the Count of the ArrayList, doesn't that make it the last element of the list, thus should no append a coma?

crmepham
  • 4,676
  • 19
  • 80
  • 155

3 Answers3

2

Problem 1 : your condition if(i != searchCriteria.Count) willbe never true because your for loop will not come inside if i equals the ArrayList.Count.

so you need to have the condiftion if(i != searchCriteria.Count) to skip the comma for last item.

You can also use TrimEnd() function to remove the last comma.

Try This:

query = query.TrimEnd(',');

Problem 2: in your loop you are hardcoding value zero to get first element from ArrayList always

Replace This:

query += (String)searchCriteria[0];

With This:

query += (String)searchCriteria[i];

Suggestion 1: don't use ArrayList as it is legacy you can use Generic List<T>.

Complete Code:

public String buildWOEIDQuery(List<string> searchCriteria)
{
    String query = "http://where.yahooapis.com/v1/places.q('";
    for (int i = 0; i < searchCriteria.Count; i++ )
    {
       query += searchCriteria[i] + ",";        
    }
    query = query.TrimEnd(',');
    query += "')?appid=" + devKey;

    return query;
}
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
1

The reason your check does not work is that i is equal to searchCriteria.Count-1 in the last iteration of the loop. So you could modify your if check to be i != searchCriteria.Count-1

As an alternative, you could bypass the check and use the TrimEnd function to trim off the excess characters after the loop.

var query = "a,b,c,";
query = query.TrimEnd(',');
MessageBox.Show(query);    // a,b,c
John Koerner
  • 37,428
  • 8
  • 84
  • 134
1

Do not use ArrayList; use List<string> or even better IEnumerable<string> instead. Your code can be more elegant if you use string.Join(). It is tailor-made for requirements like yours. The complete solution is as under:

public string buildWOEIDQuery(IEnumerable<string> searchCriteria)
{
    string query = "http://where.yahooapis.com/v1/places.q('";
    query += string.Join(",", searchCriteria);
    query += "')?appid=" + devKey;

    return query;
}

You can look at a similar question on SO.

Community
  • 1
  • 1
bittusarkar
  • 6,247
  • 3
  • 30
  • 50