Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart equivalent of \n

Tags:

newline

dart

Is there a Dart equivalent to C++'s "\n"?

In the following example:

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

I would like to have the text that's in "readLines[j]" in separate lines. How could this be done?

For example:

readLines is a List of Strings, and it contains: "Hey my name is Cheshie", and "Thanks guys for trying to help".

In the code above, I am trying to write the content of the list into a file, using "outputFile", and I would like it to be written as follows:

Hey my name is Cheshie

Thanks guys for trying to help

That is, every readLines[j] should be written in a separate line.

Thanks.

The code:

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++;
    }   

// =====================Here begins the relevant part==================

    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!');
}
like image 560
Cheshie Avatar asked Mar 01 '13 14:03

Cheshie


People also ask

How do you make a new line in darts?

Fortunately Dart has a built-in feature that allows us to split a String by newline. LineSplitter , which is part of Dart's convert API, can be used to split a String by CR , LF , or CR + LF . So, we don't need to create a new function. It returns List <String> , which means we can iterate the result.

Is it n or \n for new line?

Operating systems have special characters denoting the start of a new line. For example, in Linux a new line is denoted by “\n”, also called a Line Feed. In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF.

How do you end a Dart line?

A line is terminated by either: a CR, carriage return: U+000D ('\r')

What is string interpolation in Dart?

String interpolation is the process of inserting variable values into placeholders in a string literal. To concatenate strings in Dart, we can utilize string interpolation. We use the ${} symbol to implement string interpolation in your code.


2 Answers

\n is not C++ specific, you can just add it to the end of your string before writing it to the file. So, use something like

outputFile.writeAsStringSync('${readLines[j]}\n', mode: FileMode.APPEND);
like image 103
JustDanyul Avatar answered Sep 20 '22 17:09

JustDanyul


In current version it works.

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

Console:

word
secondword
like image 40
ridvan.yazici Avatar answered Sep 20 '22 17:09

ridvan.yazici