Here is one way of doing this using titlesec. The vertically centered horizontal \hrulefill called \Vhrulefill was taken from Vertically centered horizontal rule filling the rest of a line?

\documentclass{article}
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage[explicit]{titlesec}% http://ctan.org/pkg/titlesec
\def\Vhrulefill{\leavevmode\leaders\hrule height 0.7ex depth \dimexpr0.4pt-0.7ex\hfill\kern0pt}
\titleformat{\section}{\bfseries\large}{}{0pt}{\noindent\Vhrulefill~\thesection\quad#1~\Vhrulefill}
\begin{document}
\section{First section}
\lipsum[1]
\section{Second section}
\lipsum[2]
\end{document}
Specifying the package option explicit allows you to have more control over the placement of the sectioning title via the #1 parameter in \titleformat. Of course, in this sense, you can also add a different space between the section and the horizontal rules (I've used ~ in my example). However, this modification (as well as the sectional formatting) may all be up to personal preference. I've also used \bfseries\large for the sectional headings which is different from what is defined in article.cls. Here are the defaults:
\section: \normalfont\Large\bfseries
\subsection: \normalfont\large\bfseries
\subsubsection: \normalfont\normalsize\bfseries
\paragraph: \normalfont\normalsize\bfseries
\subparagraph: \normalfont\normalsize\bfseries
\quad is the regular skip between the sectional number/counter and title/heading. It is defined as a horizontal skip of 1em, which is relative to the font size used - a good thing. Finally, if you want a regular rule at the baseline level, use \hrulefill instead of \Vhrulefill. Of course, any of the other methods described to provide a vertically centered \hrulefill should also work.
\unskipafter#1to remove spaces at end of\section{...}, e.g. because of a line break before closing brace. – Schweinebacke Nov 07 '11 at 11:55\section{First\\ section}, how to solve it? – lyl Feb 27 '19 at 06:49\section[First section]{First\\section}. – Werner Feb 27 '19 at 07:06