Remove non ascii characters from String in Java example – Java Code Examples

This example shows how to remove non ascii characters from String in Java. Example also shows how to remove non ascii characters from String using regular expression.

How to replace non ascii characters with ASCII equivalent character?

All above approaches remove non ascii characters from the String. What if you want to replace “ä” with “a”? You can do that by normalize the string first and then replace the non ascii characters as given below.

Output

Alternatively, you can also use “[^\\p{ASCII}]” to remove all non-ascii characters after normalizing the string value as given below.

Output

If the text is in Unicode format, “[\\p{M}]” pattern should be used instead of “[^\\p{ASCII}]” pattern as given below.

Output

In regular expression \\p{M} matches the accent while \\P{M} matches the glyph of a Unicode character.

Finally, if you are using Apache Commons library, you can use stripAccents method of StringUtils class to remove accents from the Unicode characters as given below.

Output

How to remove only non printable characters from the String?

If you want to keep only printable characters and remove all non printable characters from the String you can use below given code.

Please note that above code also removes \t (tab), \n (new line) and \r (carriage return) characters as well.

Please let us know your views in the comments section below.

Source: Remove non ascii characters from String in Java example – Java Code Examples

Advertisements
Posted in Compus | Tagged , , ,

MS Access: Left Function

Syntax

The syntax for the Left function in MS Access is:

Left ( text, number_of_characters )

Parameters or Arguments

text

The string that you wish to extract from.

number_of_characters

Indicates the number of characters that you wish to extract starting from the left-most character.

Note If number_of_characters exceeds the number of characters in text, the Left function will return text.

Example

Let’s look at how to use the Left function in MS Access:

Left (“Tech on the Net”, 4)

Result: “Tech”

Left (“Alphabet Soup”, 5)

Result: “Alpha”Left (“Alphabet”, 20)

Result: “Alphabet”

Source: MS Access: Left Function

Posted in Compus | Tagged , , ,

How to convert date to number or text in Excel?

Select a cell next to your date data, and type this formula =TEXT(A1,”dd/mm/yyyy”) into it, then press Enter key. If you need, you can drag the fill handle to apply this formula in a range.

Source: How to convert date to number or text in Excel?

Posted in Compus | Tagged , ,

How to find “?” (question mark) ? | PC Review

like “*[?]*”

Source: How to find “?” (question mark) ? | PC Review

Posted in Compus | Tagged

tMap Filtrar Java nulos (null)

row1.postal_code!=null&&row1.gender.equals(“F”)

Source: tMap expression syntax – Talend Knowledge Base – Talend Online Documentation & Knowledge Base

tMap expression syntax – Talend Knowledge Base – Talend Online Documentation & Knowledge Base

Posted in Tech | Tagged , , ,

Create zip file from single file with ZipOutputStream | Examples Java Code Geeks – 2016

Source: Create zip file from single file with ZipOutputStream | Examples Java Code Geeks – 2016

package com.javacodegeeks.snippets.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CreateZipFileFromSingleFileWithZipOutputStream {

public static void main(String[] args) {

String zipFile = “C:/archive.zip”;
String srcFilename = “C:/srcfile.txt”;

try {

// create byte buffer
byte[] buffer = new byte[1024];

FileOutputStream fos = new FileOutputStream(zipFile);

ZipOutputStream zos = new ZipOutputStream(fos);

File srcFile = new File(srcFilename);

FileInputStream fis = new FileInputStream(srcFile);

// begin writing a new ZIP entry, positions the stream to the start of the entry data
zos.putNextEntry(new ZipEntry(srcFile.getName()));

int length;

while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}

zos.closeEntry();

// close the InputStream
fis.close();

// close the ZipOutputStream
zos.close();

}
catch (IOException ioe) {
System.out.println(“Error creating zip file” + ioe);
}

}

}

Posted in General

Talend – compare 2 files and keep entries that are not common to both

Source: Talend – compare 2 files and keep entries that are not common to both

Posted in General