# How to count if dash in string has n spaces?

#### Magnatolia

##### Board Regular
Hi everyone,

Is it possible to count the number of dashes in a string that do not have space on both or just one side? The string length will be inconsistent so cannot use number of words. e.g. "This-document" would be counted, whereas "This - Document" would not.

Thanks!

### Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Is this what you want?

Book1
ABCD
1This-document1
2This - Document0
3This--document2
Sheet1
Cell Formulas
RangeFormula
D1=LEN(A1)-LEN(IF(ISERROR(SEARCH(" - ",A1)),SUBSTITUTE(A1,"-",""),A1))
D2=LEN(A2)-LEN(IF(ISERROR(SEARCH(" - ",A2)),SUBSTITUTE(A2,"-",""),A2))
D3=LEN(A3)-LEN(IF(ISERROR(SEARCH(" - ",A3)),SUBSTITUTE(A3,"-",""),A3))

Is this what you want?
ABCD
1This-document1
2This - Document0
3This--document2

</tbody>
Sheet1

Worksheet Formulas
CellFormula
D1=LEN(A1)-LEN(IF(ISERROR(SEARCH(" - ",A1)),SUBSTITUTE(A1,"-",""),A1))
D2=LEN(A2)-LEN(IF(ISERROR(SEARCH(" - ",A2)),SUBSTITUTE(A2,"-",""),A2))
D3=LEN(A3)-LEN(IF(ISERROR(SEARCH(" - ",A3)),SUBSTITUTE(A3,"-",""),A3))

</tbody>

<tbody>
</tbody>

Scott, your formulas do not take into account the red highlighted part below of the OP's question...

"....in a string that do not have space on both or just one side?"

Last edited:
Is this what you want?
ABCD
1This-document1
2This - Document0
3This--document2

</tbody>
Sheet1

Worksheet Formulas
CellFormula
D1=LEN(A1)-LEN(IF(ISERROR(SEARCH(" - ",A1)),SUBSTITUTE(A1,"-",""),A1))
D2=LEN(A2)-LEN(IF(ISERROR(SEARCH(" - ",A2)),SUBSTITUTE(A2,"-",""),A2))
D3=LEN(A3)-LEN(IF(ISERROR(SEARCH(" - ",A3)),SUBSTITUTE(A3,"-",""),A3))

</tbody>

<tbody>
</tbody>

Thanks! This is awesome, however I think this only works if there is one dash. What would I need to change if I wanted to check any dash in the string? e.g. TEAM - DOCUMENT TYPE -TITLE

Thanks!

Maybe this:

Replies
8
Views
260
Replies
3
Views
133
Replies
2
Views
100
Replies
2
Views
425
Replies
0
Views
187

1,221,522
Messages
6,160,308
Members
451,637
Latest member
hvp2262

### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back