Shoutbox

Auto capitalize - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: WLM Plus! Help (/forumdisplay.php?fid=12)
+----- Thread: Auto capitalize (/showthread.php?tid=98386)

Auto capitalize by mplusfan on 09-28-2011 at 01:09 AM

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?


RE: Auto capitalize by Spunky on 09-28-2011 at 08:17 AM

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
RE: Auto capitalize by mplusfan on 09-28-2011 at 12:33 PM

:) Thanks.


RE: Auto capitalize by matty on 09-28-2011 at 12:48 PM

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 ". . . ".
RE: Auto capitalize by Spunky on 09-28-2011 at 03:22 PM

quote:
Originally posted by matty
You will want to add spaces after the ".". The current code will turn "..." into ". . . ".

Good catch :P
RE: Auto capitalize by CookieRevised on 09-28-2011 at 06:00 PM

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).

;)
RE: Auto capitalize by Spunky on 09-28-2011 at 06:12 PM

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