What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » WLM Plus! Help » Auto capitalize

Auto capitalize
Author: Message:
mplusfan
New Member
*


Posts: 5
– / – / Flag
Joined: Sep 2011
O.P. Auto capitalize
Hi. How can I capitalize a word in a sentence automatically in a conversation? And how to do that after a "."?
In other words, uppercase when starting a sentence.

Is there any way?
09-28-2011 01:09 AM
Profile E-Mail PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
36 / Male / Flag
Joined: Aug 2006
RE: Auto capitalize
js code:
function OnEvent_ChatWndSendMessage(ChatWnd, Message){
    Message = Message.split(".");
    for(var i in Message){
        Message[i] = Message[i].replace(/(^\s+)/, '');
        Message[i] = " " + Message[i].substring(0, 1).toUpperCase() + Message[i].substring(1);
    }
    return Message.join(".");
}

Probably not the fastest or best way to do it, but I couldn't find a way to do it with pure regex
<Eljay> "Problems encountered: shit blew up" :zippy:
09-28-2011 08:17 AM
Profile PM Find Quote Report
mplusfan
New Member
*


Posts: 5
– / – / Flag
Joined: Sep 2011
O.P. RE: Auto capitalize
:) Thanks.
09-28-2011 12:33 PM
Profile E-Mail PM Find Quote Report
matty
Scripting Guru
*****


Posts: 8336
Reputation: 109
39 / Male / Flag
Joined: Dec 2002
Status: Away
RE: Auto capitalize
quote:
Originally posted by Spunky
js code:
function OnEvent_ChatWndSendMessage(ChatWnd, Message){
    Message = Message.split(". ");
    for(var i in Message){
        Message[i] = Message[i].replace(/(^\s+)/, '');
        Message[i] = " " + Message[i].substring(0, 1).toUpperCase() + Message[i].substring(1);
    }
    return Message.join(". ");
}

You will want to add spaces after the ".". The current code will turn "..." into ". . . ".

This post was edited on 09-28-2011 at 12:49 PM by matty.
09-28-2011 12:48 PM
Profile E-Mail PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
36 / Male / Flag
Joined: Aug 2006
RE: Auto capitalize
quote:
Originally posted by matty
You will want to add spaces after the ".". The current code will turn "..." into ". . . ".

Good catch :P
<Eljay> "Problems encountered: shit blew up" :zippy:
09-28-2011 03:22 PM
Profile PM Find Quote Report
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Auto capitalize
had a few minutes to spare and it was a long long time since I used regular expressions (and even Plus! scripting for that matter), so...


A few things with Spunky's code:
1) It does not capitalize the first word in a string
"hello world." stays "hello world."

2) Multiple spaces after a period are not taken in account.
"test1.     test2" stays "test1.     test2"

3) Character is not capitalized after an exclamation mark.
"test1! test2" stays "test1! test2"

4) substring(0, 1)  =>  charAt(0)
though, this is just a very tiny detail, or personal preference if you will.

--

Either way, much shorter code using pure regular expressions:
js code:
function OnEvent_ChatWndSendMessage(ChatWnd, Message){
    return Message.replace(/^\s*.|(\.|!)\s+./gm, function(a) {return a.toUpperCase()})
}
This also capitalizes the first word of a string.
Multiple spaces are taken in account.
It also capitalizes after an exclamation mark.
It even capitalizes when a new line character is encountered.

Example string:
"   hello world. this is another sentence! this too.       and this. HTML stays capitalized...this not\nthis will be capitalized too."

Output result:
"   Hello world. This is another sentence! This too.       And this. HTML stays capitalized...this not
This will be capitalized too."


Important note: I would never use something like that with the OnEvent_ChatWndSendMessage() event as-is though. Code like that can 'corrupt' URLs and other stuff (like literal raw commands) which should not be threated as a pure text. So be very carefull with something like this! It is a nice programming exercise to make something like this, but in practice I highly recommend to never ever use it.

So, mplusfan, you could use code like above. But in practice it will always corrupt certain stuff which you do not want! It is much better to simply remember to capitalize sentences manually instead of being 'lazy' (for the lack of a better word).

;)

This post was edited on 09-28-2011 at 06:12 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
09-28-2011 06:00 PM
Profile PM Find Quote Report
Spunky
Former Super Mod
*****

Avatar

Posts: 3658
Reputation: 61
36 / Male / Flag
Joined: Aug 2006
RE: Auto capitalize
Ah, didn't realise it could use anonymous functions for the replace param. Was looking for a way to do it (and my original regex did include multiple space), but all I could find was that it can only be done in POSIX style regex, not perl
<Eljay> "Problems encountered: shit blew up" :zippy:
09-28-2011 06:12 PM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On