Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Write Table now available in Qlik Cloud Analytics: Read Blog
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Converting strings into proper / title case using Java

Hi, how would you go about converting a string which is all upper case into capital first letter only for the whole sentence (proper/title case)? There does not seem to be a Talend string function for this and neither built-in to the basic Java libraries.

Labels (3)
25 Replies
Anonymous
Not applicable
Author

Hi,
One of the libraries that comes with Talend has a capitalize() function. It's called Commons Lang and capitalize() is in a class called StringUtils.
To use this, use a tLoadLibrary component. Configure it to use the commons-lang-2.5.jar and import the class 'org.apache.commons.lang.StringUtils'. In your tMap, use something like
row2.first_name = StringUtils.capitalize(row1.first_name)
If you want a quick test, cut-and-paste this in a tJava.
System.out.println( StringUtils.capitalize(null) );
System.out.println( StringUtils.capitalize("") );
System.out.println( StringUtils.capitalize("cat") );
System.out.println( StringUtils.capitalize("cAt") );
-Carl
Anonymous
Not applicable
Author

Brilliant, thank you. That will solve the problem I am sure. I was not aware that there was such a vast amount of libraries shipped with Talend.
I don't suppose you know where to find a typical string splitting method now that I am thinking of it? We want to grab only the last part of a string, the text after the last comma.
Anonymous
Not applicable
Author

You can use java Strring.split method
ex
String spt= "Java.How.To.Split";
spt.split(".")
Anonymous
Not applicable
Author

If the standard split() does't work for you, try (from the same Commons Lang / StringUtils class) substringAfterLast().
Here's a link to the Javadoc. These string functions are useful. http://commons.apache.org/lang/api-release/index.html.
If you want to extend these functions or make some documentation available with them, take a look at this blog post on the topic.
http://bekwam.blogspot.com/2011/02/user-defined-functions-with-talend-open.html
Anonymous
Not applicable
Author

I have now tried the tJava example above and it doesn't actually do anything with the strings - this is the output:
null
Cat
CAt

Where exactly am I supposed to write the import line? I have written it under Advanced Settings in the tJava component now like this:
import org.apache.commons.lang.StringUtils;
Anonymous
Not applicable
Author

Also, thank you both for the info on the Split function, it turns out to be rather easy doesn't it. Would you recommend using Java code or the tExtractDelimtedFields component?
Anonymous
Not applicable
Author

I think you should try both and use the one which is faster
Anonymous
Not applicable
Author

Yeah, fair enough, thanks.

I would like to repeat my previous question as well:

I have now tried the tJava example above and it doesn't actually do anything with the strings. Where exactly am I supposed to write the import line? I have written it under Advanced Settings in the tJava component now like this:
import org.apache.commons.lang.StringUtils;
Anonymous
Not applicable
Author

hi all
use toUppercase() method to capitalise characters.
the opposite => toLowerCase()
Talend Method : DOWNCASE and UPCASE
regards
laurent