Skip to content

Commit

Permalink
add modifying input example
Browse files Browse the repository at this point in the history
  • Loading branch information
osiegmar committed Aug 28, 2024
1 parent de8d352 commit 3739987
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
28 changes: 28 additions & 0 deletions docs/src/content/docs/guides/Examples/modifying-input.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: Modifying input
---

import {Code} from '@astrojs/starlight/components';
import sourceCode from '../../../../../../example/src/main/java/example/ExampleCsvReaderWithFieldModifier.java?raw';

The CSV specification ([section 2.4. of RFC 4180](https://www.rfc-editor.org/rfc/rfc4180#section-2)) explicitly states:

> Spaces are considered part of a field and should not be ignored.
However, in some cases, you might want to remove leading and trailing spaces from fields while reading a CSV file
or modify the fields in another way (e.g., converting them to lower- or uppercase). FastCSV allows you to do this
using a field modifier that can be used together with a `CsvRecordHandler`.

:::tip
If you want to read fields not as string but as a specific type (e.g., `Boolean`, `Long`, `LocalDate`, etc.),
you should check out the [Custom Callback handler example](../custom-callback-handler/).
:::

## Example

In the following example, fields are modified while reading a CSV file.

<Code code={sourceCode} title="ExampleCsvReaderWithFieldModifier.java" lang="java"/>

You also find this source code example in the
[FastCSV GitHub repository](https://github.com/osiegmar/FastCSV/blob/main/example/src/main/java/example/ExampleCsvReaderWithFieldModifier.java).
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,36 @@ public class ExampleCsvReaderWithFieldModifier {

public static void main(final String[] args) {
System.out.println("Trim fields:");
CsvReader.builder().build(new CsvRecordHandler(FieldModifiers.TRIM), DATA)
CsvReader.builder()
.build(new CsvRecordHandler(FieldModifiers.TRIM), DATA)
.forEach(System.out::println);

System.out.println("Trim and lowercase fields:");
CsvReader.builder().build(new CsvRecordHandler(combinedModifier()), DATA)
System.out.println("Combine modifiers (trim/lowercase):");
CsvReader.builder()
.build(new CsvRecordHandler(combinedModifier()), DATA)
.forEach(System.out::println);

System.out.println("Trim and lowercase fields of first record (by using a custom modifier):");
CsvReader.builder().build(new CsvRecordHandler(customModifier()), DATA)
System.out.println("Custom modifier (trim/lowercase on first record):");
CsvReader.builder()
.build(new CsvRecordHandler(customModifier()), DATA)
.forEach(System.out::println);
}

private static FieldModifier combinedModifier() {
return FieldModifiers.TRIM.andThen(FieldModifiers.lower(Locale.ENGLISH));
return FieldModifiers.TRIM
.andThen(FieldModifiers.lower(Locale.ENGLISH));
}

private static FieldModifier customModifier() {
return new FieldModifier() {
@Override
public String modify(final long startingLineNumber, final int fieldIdx, final boolean quoted,
public String modify(final long startingLineNumber,
final int fieldIdx,
final boolean quoted,
final String field) {
return startingLineNumber == 1 ? field.trim().toLowerCase(Locale.ENGLISH) : field;
return startingLineNumber == 1
? field.trim().toLowerCase(Locale.ENGLISH)
: field;
}
};
}
Expand Down

0 comments on commit 3739987

Please sign in to comment.