One Star

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.

25 REPLIES
One Star

Re: Converting strings into proper / title case using Java

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
One Star

Re: Converting strings into proper / title case using Java

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.
One Star

Re: Converting strings into proper / title case using Java

You can use java Strring.split method
ex
String spt= "Java.How.To.Split";
spt.split(".")
One Star

Re: Converting strings into proper / title case using Java

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
One Star

Re: Converting strings into proper / title case using Java

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;
One Star

Re: Converting strings into proper / title case using Java

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?
One Star

Re: Converting strings into proper / title case using Java

I think you should try both and use the one which is faster
One Star

Re: Converting strings into proper / title case using Java

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;
Seventeen Stars

Re: Converting strings into proper / title case using Java

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

Re: Converting strings into proper / title case using Java

I should have been more clear - it is not uppercase I want, I want only the first letter of each word to be capital:
Just Like This Example
One Star

Re: Converting strings into proper / title case using Java

In this case You can use String buffer
it is not uppercase I want, I want only the first letter of each word to be capital

ex
String a="alpha";
StringBuffer abc=new StringBuffer(a);
String temp = Character.toString(abc.charAt(0));
temp=temp.toUpperCase();
abc.setCharAt(0,temp.charAt(0));
System.out.println(abc);
This case will only work if you know the index number of which Char to change
One Star

Re: Converting strings into proper / title case using Java

The following code in a tjava / tjavarow component will do it... Just replace the string literal with your input field name
String sentence = "";
String curWord = "";
String curWordUpper = "";
String str = "my first talend etl job";
String[] temp;
/* delimiter */
String delimiter = " ";
/* given string will be split by the argument delimiter provided. */
temp = str.split(delimiter);
/* print substrings */
for(int i =0; i < temp.length ; i++)
{
curWord = temp;
curWordUpper = "";

for (int j = 0; j < curWord.length(); j++)
{
String next = curWord.substring(j, j + 1);
if (j == 0)
{
curWordUpper += next.toUpperCase();
}
else
{
curWordUpper += next.toLowerCase();
}
}
sentence += curWordUpper + " ";
//System.out.println(curWord2);
}
System.out.println(sentence.trim());
One Star

Re: Converting strings into proper / title case using Java

There's a WordUtils.capitalize() function in Commons Lang if you want to modify each word in a sentence.
WordUtils.captialize(row1.mytext);
import org.apache.commons.lang.WordUtils;
One Star

Re: Converting strings into proper / title case using Java

I have added the commons Lang by adding to the JRE files and also tried using the Library Loader but my TMap keeps throwing up and error that it doesn't recognise either WordUtils or StringUtils. I want to use the function capitalizeFully to set the first letters of each word in an address string to upper case.
Would someone be kind enough to explain how exactly I can get the function for the commons Lang to work.
One Star

Re: Converting strings into proper / title case using Java

Hi,
Double-check the import statement which is needed in addition to telling Talend where the library is (jar file). If that's ok, post the specific error message.
This post has an example that may help you: http://bekwam.blogspot.com/2011/01/java-libraries-in-talend-open-studio.html.
One Star

Re: Converting strings into proper / title case using Java

Hi there. Thanks for the prompt reply. I have attached images of the set up and errors. I have tried both ways; to add the Land file under the JRE setting sand with the Loader as you suggested. First images via the Preferences route and the then by Library Loader.
Trying to achieve Word.Utils.capitalizeFully(row1.Address_1) - pic1
Pic2 - added the commons Land to JRE
Pic3 - run error.
Pic4 - now with Library Load basic settings
Pic5 - Advanced settings JAR location and Import string
pic6 - new run error.
I hope this is clear.
Thanks.
One Star

Re: Converting strings into proper / title case using Java

Hi,
To fix your problem,
1. Switch to commons-lang-2.6.jar in the tLoadLibrary / Basic settings / Library select box
2. Remove everything from Advanced settings / Dynamic libs
3. Add the following path to Advanced settings / import: import org.apache.commons.lang.WordUtils;
4. Use the function in a component like tJavaRow like
output_row.field1 = WordUtils.capitalizeFully(input_row.field1);
I'm having a problem with Commons Lang 3 which may have been installed in your workspace when you installed my tScriptRules component. Apparently, it's in the folder structure and available in the modules list, but TOS isn't recognizing the commons-lang3-3.0.jar as something available on the classpath. It's fine to use Commons Lang 2.6 for what you want. Follow along here: http://www.talendforge.org/forum/viewtopic.php?pid=93369#p93369.
One Star

Re: Converting strings into proper / title case using Java

Carl, you are a scholar and a gentleman?.. All peachy.
Much appreciated.
One Star

Re: Converting strings into proper / title case using Java

Hi,
Probably you can use following expression in your tMap
Expression:
----------------
row1.TitleString.toLowerCase().replaceFirst("", row1.TitleString.substring(0,1).toUpperCase())
Note:- "TitleString" is the column name.
--
Regards,
Vinod
One Star

Re: Converting strings into proper / title case using Java

Hi,
Probably you can use following expression in your tMap
Expression:
----------------
row1.TitleString.toLowerCase().replaceFirst("", row1.TitleString.substring(0,1).toUpperCase())
Note:- "TitleString" is the column name.
--
Regards,
Vinod

Why would you do that.
One Star

Re: 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.

Hi,
Probably you can use following expression in your tMap
Expression:
----------------
row1.TitleString.toLowerCase().replaceFirst("", row1.TitleString.substring(0,1).toUpperCase())
Note:- "TitleString" is the column name.
--
Regards,
Vinod
One Star

Re: Converting strings into proper / title case using Java

Yes, it's a stupid and messy solution. It only works for the first word, why are you using "replaceFirst" with a regex but use the first character via substring as a replacement anyway?
One Star

Re: Converting strings into proper / title case using Java

Hello All
I am trying to ensure that certain fields in my pre-load database contain proper (or title) case. Having read several posts on the subject I think the route I should take is as follows;
tlibraryload > tmssqlInput > tJavaRow > tmap > tlogrow
In tlibraryLoad my settings are:
Basic Settings > library > commons-lang-2.6.jar
Advanced Settings > Lib > empty
Advanced Setting > Import > //import org.apache.commons.lang.WordUtils;
In TJavaRow my settings are:
Basic Settings > Code >
OutputRow.File_Detail_Header_Id = InputRow.File_Detail_Header_Id;
OutputRow.File_Detail_Id = InputRow.File_Detail_Id;
OutputRow.Forenames = WordUtils.capitalizeFully(InputRow.Forenames);
Advanced Settings > Import > //import org.apache.commons.lang.WordUtils;
The error messages I am getting:
Exception in thread "main" java.lang.Error: java.lang.Error: Unresolved compilation problems:
WordUtils cannot be resolved
FileDetailRead cannot be resolved to a variable
Syntax error on token "OutputRow", delete this token
OutputRow is what I called the connector between TJavaRow and TMap and is spelt and the casing are both corrrect.
Where have I gone wrong?
One Star

Re: Converting strings into proper / title case using Java

did something like this a little while back, so i thought i'll post it - even though it seems like the tread is kinda dead
under Code -> Routines -> right click and add your own custom routine, then you have two options
1. right click the new routine and click on Edit routine libraries and add your required lib in here (per my experience routine libs work better than trying to load something into a job), then just create wrapper functions for the lib methods in this routine, and call them via your newly defined wrapper methods.
2. Doing proper case is not that complex of a job once your writing java code (you'll find many examples of this on google), so you can create your own version of a properCase function in this new class, which can implement other complex rules you might need as well - and then just use that in your tMap, Flow etc. no need to integrate with any external API.
hope this helps.
Four Stars

Re: Converting strings into proper / title case using Java

In the talendforge there is an implementation of StringUtils but it doesn't appear supported for DI 6.3. I was wondering since the original posting in 2011 is there a updated library that should be used with tLoadLibrary component?

The original poster suggested the values below but when I browse the list of libraries available I see others and from the apache documents capitalize looks to be available in most

 ApacheCommonLangsLibraries.png

commons-lang-2.5.jar 

and

import the class 

org.apache.commons.lang.StringUtils