换行符转 \n 转换器

这个转换器通过将实际换行符替换为转义字符序列 \n(或 \r\n),将多行文本转换为单行字符串。可将代码中的多行字符串内联或准备用于 JSON、API 和配置文件的文本。

Newlines to \n Converter

Convert text with line breaks into a single line by replacing all line breaks with \n characters. Perfect for preparing string literals in programming.

Frequently Asked Questions

编程中\n是什么意思?

\n是一个转义序列,在JavaScript、Python、Java、C、C++和PHP等大多数编程语言中表示换行符(换行符/LF)。当程序遇到字符串中的\n时,会在该位置插入实际的换行符。反斜杠(\)告诉解释器或编译器下一个字符具有特殊含义,而不是字面字母"n"。其他常见的转义序列包括\t(制表符)、\r(回车符)和\\(字面反斜杠)。

如何将换行符转换为\n?

要将实际换行符转换为\n转义序列,将每个换行符替换为两字符字面字符串"\n"。在JavaScript中,使用text.replace(/\n/g, "\\n")。在Python中,使用text.replace("\n", "\\n")。本工具自动执行转换:粘贴多行文本,输出包含\n的单行字符串,代替换行位置。当在JSON字符串或单行代码赋值中嵌入多行内容时,这是必不可少的。

\n和\r\n有什么区别?

\n(换行符,LF)和\r\n(回车符+换行符,CRLF)是不同操作系统使用的不同行尾约定。Unix、Linux和macOS使用\n(LF)作为行尾。Windows使用\r\n(CRLF)。旧版Mac OS(OS X之前)单独使用\r(CR)。\r将光标移到行首,\n将光标移到下一行。大多数现代文本编辑器和编程语言能处理两种格式,但不匹配可能导致脚本错误、Git差异和文件解析问题。

为什么JSON字符串需要转义的换行符?

JSON规范(RFC 8259)要求字符串包含在单行中。JSON字符串值内的字面换行符是无效的,会导致解析错误。要在JSON字符串中包含换行符,必须使用转义序列\n。例如,{"text": "第一行\n第二行"}是有效的JSON,但在引号内的"第一行"和"第二行"之间有实际换行符会是语法错误。这就是为什么在为JSON负载准备文本时,将换行符转换为\n是必不可少的。

如何将\n转换回实际换行符?

要将\n转义序列转换回实际换行符,将两字符字面字符串"\n"替换为真实的换行符。在JavaScript中,使用text.replace(/\\n/g, "\n")。在Python中,使用text.replace("\\n", "\n")。本工具也支持反向转换:粘贴包含\n序列的单行字符串,将其转换回有实际换行符的多行文本。当从日志、API响应或配置文件中读取转义字符串时,这非常有用。

什么是行尾?

行尾(也称为换行符、换行或行终止符)是标记一行文本结束的特殊字符或字符序列。三种主要类型为:LF(\n,十六进制0x0A)用于Unix/Linux/macOS,CRLF(\r\n,十六进制0x0D 0x0A)用于Windows,CR(\r,十六进制0x0D)用于经典Mac OS。行尾是控制文件和终端中文本换行的不可见字符。可以使用core.autocrlf设置配置Git以在平台之间自动转换行尾。

LF和CRLF有什么区别?

LF(换行符,\n)是Unix、Linux和macOS上用作行尾的单个字符(字节0x0A)。CRLF(回车符+换行符,\r\n)是Windows上使用的两字符序列(字节0x0D 0x0A)。这两个术语起源于打字机:回车将打印头移到行首,换行将纸张前进一行。在同一文件中混合LF和CRLF会导致显示问题、脚本失败和嘈杂的Git差异。大多数现代编辑器可以在两种格式之间转换。

Newlines to \n Converter - Convert Line Breaks to \n Characters

What is Newlines to \n Converter?

The Newlines to \n Converter is a powerful tool that converts text with line breaks into a single line, replacing all line breaks with the literal \n character sequence. This tool is particularly useful for developers, programmers, and anyone working with string literals in programming languages.

Key Features of Our Newlines to \n Converter

  • Converts all types of line breaks (CR, LF, CRLF) to \n
  • Preserves all other text content exactly as input
  • One-click copy functionality for easy use
  • Clean and intuitive interface
  • Real-time conversion

How to Use the Newlines to \n Converter

  1. Paste or type your text with line breaks in the input field
  2. Click "Convert" button to process your text
  3. The converted text will appear in the output field with all line breaks replaced by \n
  4. Use the copy button to copy the result to your clipboard

Common Use Cases

  • Preparing string literals for programming
  • Converting multi-line text for single-line storage
  • Processing log files or text data
  • Preparing text for JSON string values

Why Use Our Newlines to \n Converter?

  • Accurate conversion of all line break types
  • User-friendly interface with clear input and output areas
  • Instant results with no page reloads
  • Free to use with no registration required
  • Works offline in your browser

Technical Details

Our Newlines to \n Converter handles all common line ending types:

  • Windows-style line endings (CRLF: \r\n)
  • Unix/Linux-style line endings (LF: \n)
  • Legacy Mac-style line endings (CR: \r)