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!');
}
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.
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.
A line is terminated by either: a CR, carriage return: U+000D ('\r')
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.
\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);
In current version it works.
void main() {
print('word\nsecondword');
}
Console:
word
secondword
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With