等价于\ n

问题描述 投票:0回答:3

是否有Dart等于C ++的“ \ n”?

在以下示例中:

for(int j=0;j<readLines.length;j++)
{
    outputFile.writeAsStringSync(readLines[j], mode: FileMode.APPEND);
}

我希望将“ readLines [j]”中的文本放在单独的行中。如何做到这一点?

例如:

readLines是一个字符串列表,它包含:“嘿,我叫Cheshie”,和“谢谢大家的帮助”。

在上面的代码中,我试图使用“ outputFile”将列表的内容写入文件,并且我希望将其编写如下:

嘿,我叫Cheshie

感谢大家的帮助

即,每个readLines [j]应该写在单独的行中。

谢谢。

代码:

import 'dart:io';

void func (String foldername)
{
  Directory thisFolder = new Directory (foldername);

  List<File> files = thisFolder.listSync(recursive:false);
  int number=1;
  String oldContent='';
  String newContent='';
  String concatenate='';
  String name='';
  int nameStart;
  int nameLength;
  for (int i=0; i<files.length; i++)
  {
    if (files[i].name.endsWith('.in'))
    {
      oldContent=files[i].readAsStringSync(Encoding.UTF_8);
      newContent=number.toString();

      var Strings=[newContent, oldContent];
      concatenate=Strings.join();

      files[i].writeAsStringSync(concatenate);
      number++;
    }   

// ==================== << [这里开始相关部分 ============ ======

nameLength=files[i].name.length; nameStart=files[i].name.lastIndexOf('\\', nameLength); name=files[i].name.slice(nameStart+1, nameLength); if (name.compareTo('hello.in')==0) { File outputFile=new File('hello.out'); if (outputFile.existsSync()) { outputFile.deleteSync(); } outputFile.createSync(); List<String> readLines=files[i].readAsLinesSync(Encoding.UTF_8); for(int j=0;j<readLines.length;j++) { outputFile.writeAsStringSync('$readLines[j]\n', mode: FileMode.APPEND); //outputFile.writeAsStringSync('\n', mode: FileMode.APPEND); // TODO: figure out how to get to the next line. if (readLines[j].contains('you')) print(readLines[j]); } } } } void main () { func('In files'); print('the end!'); }
newline dart
3个回答
2
投票
\n不是C ++特有的,您可以将其添加到字符串的末尾,然后再将其写入文件。因此,请使用类似的内容

outputFile.writeAsStringSync('${readLines[j]}\n', mode: FileMode.APPEND);


2
投票
readLines[j]的内容是什么?一堆词?

假设类似这样:

var readLines = [ 'Dart is fun', 'It is easy to learn' ];

并且假设您要输出以下内容:

Dart is fun It is easy to learn

尝试一下:

for (String line in readLines) { String split = line.replaceAll(new RegExp(r'\s+'), '\n'); outputFile.writeAsStringSync(split, mode: FileMode.APPEND); }


0
投票
在当前版本中有效。

void main() { print('word\nsecondword'); }

控制台:

word secondword

© www.soinside.com 2019 - 2024. All rights reserved.