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

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
Anonymous
Not applicable
Author

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
willm1
Creator
Creator

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());
Anonymous
Not applicable
Author

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;
Anonymous
Not applicable
Author

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.
Anonymous
Not applicable
Author

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.
Anonymous
Not applicable
Author

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.
Anonymous
Not applicable
Author

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.
Anonymous
Not applicable
Author

Carl, you are a scholar and a gentleman?.. All peachy.
Much appreciated.
Anonymous
Not applicable
Author

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