Convert String to Title Case

If you have questions or if you want to share your opinion about Aware IM post your message on this forum
Post Reply
Jhstephenson
Posts: 303
Joined: Wed Apr 22, 2015 11:44 pm

Convert String to Title Case

Post by Jhstephenson »

I have some data that is all uppercase that I want to convert to Title Case (Where the 1st letter of each word is capitalized). I would also like to automatically convert string input in name fields to title case when a record is saved in a BO.

I have run across some Javascript, Java, and Regex code that does what I am wanting (At least I think it does) but have no clue how to either include that in a Process or in a Rule.

Here is an example of what someone came up with using Regex:

Pattern spaces=Pattern.compile("\\s+[a-z]");
Matcher m=spaces.matcher(word);
StringBuilder capitalWordBuilder=new StringBuilder(word.substring(0,1).toUpperCase());
int prevStart=1;
while(m.find()) {
capitalWordBuilder.append(word.substring(prevStart,m.end()-1));
capitalWordBuilder.append(word.substring(m.end()-1,m.end()).toUpperCase());
prevStart=m.end();
}
capitalWordBuilder.append(word.substring(prevStart,word.length()));

Here is an example of some Java Code (This one preserves defined acronyms as uppercase and minor words as lowercase, which would be really nice) :

import java.util.Scanner;
public class CapitolizeOneString {

public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print(" Please enter Your word = ");
String str=scan.nextLine();

printCapitalized( str );
} // end main()

static void printCapitalized( String str ) {
// Print a copy of str to standard output, with the
// first letter of each word in upper case.
char ch; // One of the characters in str.
char prevCh; // The character that comes before ch in the string.
int i; // A position in str, from 0 to str.length()-1.
prevCh = '.'; // Prime the loop with any non-letter character.
for ( i = 0; i < str.length(); i++ ) {
ch = str.charAt(i);
if ( Character.isLetter(ch) && ! Character.isLetter(prevCh) )
System.out.print( Character.toUpperCase(ch) );
else
System.out.print( ch );
prevCh = ch; // prevCh for next iteration is ch.
}
System.out.println();
}
} // end class

And here is some Javascript:

String.prototype.toTitleCase = function() {
var i, j, str, lowers, uppers;
str = this.replace(/([^\W_]+[^\s-]*) */g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});

// Certain minor words should be left lowercase unless
// they are the first or last words in the string
lowers = ['A', 'An', 'The', 'And', 'But', 'Or', 'For', 'Nor', 'As', 'At',
'By', 'For', 'From', 'In', 'Into', 'Near', 'Of', 'On', 'Onto', 'To', 'With'];
for (i = 0, j = lowers.length; i < j; i++)
str = str.replace(new RegExp('\\s' + lowers + '\\s', 'g'),
function(txt) {
return txt.toLowerCase();
});

// Certain words such as initialisms or acronyms should be left uppercase
uppers = ['Id', 'Tv'];
for (i = 0, j = uppers.length; i < j; i++)
str = str.replace(new RegExp('\\b' + uppers + '\\b', 'g'),
uppers.toUpperCase());

return str;
}

Can someone point me in the right direction for using these?

Thanks,
Jim
aware_support
Posts: 7536
Joined: Sun Apr 24, 2005 12:36 am
Contact:

Re: Convert String to Title Case

Post by aware_support »

The best thing would be to write a custom Java function as a plugin - it should be very easy to do.
Aware IM Support Team
Jhstephenson
Posts: 303
Joined: Wed Apr 22, 2015 11:44 pm

Re: Convert String to Title Case

Post by Jhstephenson »

I kind of assumed the Java plugin is the way to go. But, I don't have enough experience with it yet to even know where to get started.

I will have to go back over the Vegas Conference recordings, when Vlad did the demo of Java plugins, to get an idea of what needs done.

Thanks,
Jim
BenHayat
Posts: 2749
Joined: Thu Dec 23, 2010 5:48 am
Location: Fla, USA
Contact:

Re: Convert String to Title Case

Post by BenHayat »

Jhstephenson wrote:I kind of assumed the Java plugin is the way to go. But, I don't have enough experience with it yet to even know where to get started.

I will have to go back over the Vegas Conference recordings, when Vlad did the demo of Java plugins, to get an idea of what needs done.

Thanks,
Jim
Jim, don't forget Awaresoft also develops custom plugins for developers based on their specs and they do a fantastic job. I know, because I use them. So, you may want to consider them developing it for you. Or perhaps see if a few people are also interested, you can all share the expense.
Just an idea!
BobK
Posts: 548
Joined: Thu Jan 31, 2008 2:14 pm
Location: Cincinnati, Ohio, USA

Re: Convert String to Title Case

Post by BobK »

Jim,

I have not looked at the java code you posted in detail, but if that works, you already have 80% of the code needed for a plugin.
Making some small changes to your code and adding the AwareIM required methods would not be too difficult.
I have created several plugins and would be willing to assist you in this one.
Bob
Jhstephenson
Posts: 303
Joined: Wed Apr 22, 2015 11:44 pm

Re: Convert String to Title Case

Post by Jhstephenson »

Thanks Ben, Mark Bailey and I were talking about that a little yesterday.

Jim
Jhstephenson
Posts: 303
Joined: Wed Apr 22, 2015 11:44 pm

Re: Convert String to Title Case

Post by Jhstephenson »

Thanks Bob,

I am going to look closely at what I need to do for the plugin this weekend.

Jim
hpl123
Posts: 2615
Joined: Fri Feb 01, 2013 1:13 pm
Location: Scandinavia

Re: Convert String to Title Case

Post by hpl123 »

Jim,
Before you do anything else, take a look at the existing Aware IM functions. I am fairly certain you can achieve what you´re after using existing functions i.e. you don´t need a plugin or custom java/regex etc. etc.. (look under Text Functions in the Aware IM userguide).
Henrik (V8 Developer Ed. - Windows)
Post Reply