Con2Str:
Con2Strはコンテナから値のリストを取得し、デフォルトではcomma (,)
値を区切るために使用します。
client server public static str Con2Str(container c, [str sep])
パラメータの値が指定されていない場合sep
、返される文字列の要素の間にコンマ文字が挿入されます。
可能なオプション:
スペースをデフォルトの区切り文字にしたい場合は、2番目のパラメーターとしてスペースをメソッドに渡すことができますCon2Str
。
fileRecord
もう1つのオプションは、コンテナをループして個々の要素をフェッチすることもできます。
コードスニペット1:
以下のコードスニペットは、ファイルの内容をテキストバッファにロードし、キャリッジリターン(\r
)を改行(\n
)文字に置き換えます。この条件if (strlen(line) > 1)
は、改行文字が連続して発生する可能性があるため、空の文字列をスキップするのに役立ちます。
TextBuffer textBuffer;
str textString;
str clearText;
int newLinePos;
str line;
str field1;
str field2;
str field3;
counter row;
;
textBuffer = new TextBuffer();
textBuffer.fromFile(@"C:\temp\Input.txt");
textString = textBuffer.getText();
clearText = strreplace(textString, '\r', '\n');
row = 0;
while (strlen(clearText) > 0 )
{
row++;
newLinePos = strfind(clearText, '\n', 1, strlen(clearText));
line = (newLinePos == 0 ? clearText : substr(clearText, 1, newLinePos));
if (strlen(line) > 1)
{
field1 = substr(line, 1, 14);
field2 = substr(line, 15, 12);
field3 = substr(line, 27, 10);
info('Row ' + int2str(row) + ', Column 1: ' + field1);
info('Row ' + int2str(row) + ', Column 2: ' + field2);
info('Row ' + int2str(row) + ', Column 3: ' + field3);
}
clearText = (newLinePos == 0 ? '' : substr(clearText, newLinePos + 1, strlen(clearText) - newLinePos));
}
コードスニペット2:
\r\n
値をハードコーディングする代わりにファイルマクロを使用できR
ます。これは読み取りモードを示します。
TextIo inputFile;
container fileRecord;
str line;
str field1;
str field2;
str field3;
counter row;
;
inputFile = new TextIo(@"c:\temp\Input.txt", 'R');
inputFile.inFieldDelimiter("\r\n");
row = 0;
while (inputFile.status() == IO_Status::Ok)
{
row++;
fileRecord = inputFile.read();
line = con2str(fileRecord);
if (line != '')
{
field1 = substr(line, 1, 14);
field2 = substr(line, 15, 12);
field3 = substr(line, 27, 10);
info('Row ' + int2str(row) + ', Column 1: ' + field1);
info('Row ' + int2str(row) + ', Column 2: ' + field2);
info('Row ' + int2str(row) + ', Column 3: ' + field3);
}
}
