Reputation: 549
I'm joining a load of strings to make a superstring but i need to ignore a param if one is null. Currently i cannot think how to do this other than emcompassing all the params in seperate if statements. Help pls:
Here the code
public void LinkBuilder(string baselink, string sharedkey, string service, string period, string bulletintype,
string includeresults, string includemap, string username, string password)
{
sharedkey = "&" + sharedkey;
service = "&" + service;
period = "&" + period;
bulletintype = "&" + bulletintype;
includeresults = "&" + includeresults;
includemap = "&" + includemap;
username= "&" + username;
password = "&" + password;
string completeLink = sharedkey + service + period + bulletintype + includeresults + includemap + username +
password;
Not sure how to tackle this.
Upvotes: 6
Views: 4350
Reputation: 120450
Make an enumerable collection of your strings, use a bit of linq to filter out the nulls, then join it all back together again with String.Join
:
var elements =
new[]{baselink, sharedkey, service, period,
bulletintype, includeresults, includemap,
username, password};
var nonNullElements = elements.Where(e => e != null);
var outputString = String.Join("&", nonNullElements);
On the off-chance that you're actually trying to assemble a querystring, there are better ways.
For instance, you could leverage HttpUtility
and reflection using the following method for parsing an anonymous object to a query string:
public static class ObjEx
{
public static string ToQueryString(this object data)
{
var collection = data.GetType()
.GetProperties()
.Aggregate(
HttpUtility.ParseQueryString(string.Empty),
(prev,curr) => {
var val = curr.GetValue(data);
var propName = curr.Name;
prev.Add(propName,val.ToString());
return prev;
});
return collection.ToString();
}
}
then
var data = new{foo = "bar", num = 1, cat = "bad", dog = "good", needsEscaping = "é\"&"};
Console.WriteLine(data.ToQueryString());
will give you:
foo=bar&num=1&cat=bad&dog=good&needsEscaping=%u00e9%22%26
Upvotes: 1
Reputation: 11480
Below would accept a Collection
which I feel may be better maintainable. I re factored a bit.
public string LinkBuilder(Dictionary<string, string> parameters)
{
var url = String.Empty;
foreach(var parameter in parameters)
if(!string.IsNullOrEmpty(parameter.Value))
url += String.Format("&{0}={1}", parameter.Key, parameter.Value);
return url;
}
This way you would pass a collection to build your URL, then it would return
the giant URL for you. You have a massive selection, I personally like Maksim's answer.
Upvotes: 1
Reputation: 96
You can do a check of strings by operator ?: in method.
public void LinkBuilder(string baselink, string sharedkey, string service, string period, string bulletintype,
string includeresults, string includemap, string username, string password)
{
sharedkey = checkValue(sharedkey);
service = checkValue(service );
period = checkValue(period );
bulletintype = checkValue(bulletintype );
includeresults = checkValue(includeresults );
includemap = checkValue(includemap );
username= checkValue(username );
password = checkValue(password );
string completeLink = sharedkey + service + period + bulletintype + includeresults + includemap + username +
password;
}
private String checkValue(String str)
{
return str != null ? "&" + str : "";
}
Upvotes: 1
Reputation: 9679
I would really refactor it this way:
public void LinkBuilder(params string[] links)
{
string completeLink = String.Join("&", links.Where(x=>!String.IsNullOrEmpty(x)));
}
Upvotes: 21
Reputation: 1140
If the objective is to avoid wrapping each parameter in an if statement, you could add them to a list, then use String.Join
, and Linq.Select
public void LinkBuilder(string baselink, string sharedkey, string service, string period, string bulletintype,
string includeresults, string includemap, string username, string password)
{
var allParams = new List<string>
{
baselink,
sharedkey,
service,
period,
bulletintype,
includeresults,
includemap,
username,
password
};
var completeLink = "?" + String.Join("&", allParams.Select(p => p != null));
}
Upvotes: 1