---
title: "String.Format with null values C#"  
description: "String.Format with null values C#"  
author: "Anonymous User"  
published: 2013-12-12  
updated: 2013-12-12  
canonical: https://www.mindstick.com/forum/1763/string-format-with-null-values-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# String.Format with null values C#

I want to [format](https://www.mindstick.com/forum/162083/how-to-convert-ost-files-into-pst-format) an address. Here is my [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii):

```
address = String.Format("{0}, {1}, {2}, {3}, {4}, {5},
{6}",                       
postalAddress.Line1,postalAddress.Line2,                       
postalAddress.Line3,                       
postalAddress.Line4,                       
postalAddress.Suburb,                       
postalAddress.StateCode,                       
postalAddress.Pcode);
```

Unfortunately this produces 116 Knox St, , , , Watson, ACT, 2602 when Line2, Line3, Line4 are null. How can I [handle](https://www.mindstick.com/articles/311004/suede-skillet-handle-cover) the nulls to get a [results](https://yourviews.mindstick.com/story/4497/usage-of-baking-soda-magical-results) like 116 Knox St, Watson, ACT, 2602?

## Replies

### Reply by Pravesh Singh

Hi Chintoo,\
\
You can try this :\
\

StringBuilder sb = new StringBuilder();

List<string> addressParts = new List<string> { postalAddress.Line1, postalAddress.Line2, postalAddress.Line3, postalAddress.Line4, postalAddress.Suburb, postalAddress.StateCode, postalAddress.Pcode };

addressParts.ForEach(x => sb.Append(String.IsNullOrEmpty(x) ? "" : ", " + x));

string address = sb.ToString().Trim(',');\
\
Hope this will solve your problem.


---

Original Source: https://www.mindstick.com/forum/1763/string-format-with-null-values-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
