Multi Commander > Script
Title Case
Ulfhednar:
I saw this the other day
--- Code: ---using System;
using System.Collections.Generic;
public static class ClipboardFusionHelper
{
public static string ProcessText(string text)
{
//Words that will not be capitalized; add words to this list as required
string[] exceptionsArray = { "a", "an", "and", "any", "at", "from", "into", "of", "on", "or", "the", "to", };
List<string> exceptions = new List<string>(exceptionsArray);
//Break the input text into a list of capitalized (not upper case) words
text = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(text.ToLower());
List<string> words = new List<string>(text.Split(new char[] { ' ' }));
//Always leave the first word capitalized, regardless of what it is
text = words[0];
words.RemoveAt(0);
//Check each remaining word against the list, and append it to the new text
foreach (string s in words)
{
if (exceptions.Contains(s.ToLower()))
text += " " + s.ToLower();
else
text += " " + s;
}
return text;
}
}
--- End code ---
I thought I'd like to translate it to MS & add it to a button.
I can grab & split a filename, I can see how I could use the str selection to alter the words I want in lower case to lower case ... but I'm wondering if I need something like a regex version of
--- Code: ---<str> StrReplace(<str> input, <str> find, <str> with );
--- End code ---
Maybe I just haven't figured out the ordering of commands to modify the arrays yet but I'd really like something that would give me a [a-z] -> [A-Z] function :P
Any advice Mathias?
AlanJB:
Hi Ulf.
Can you not use combinations of StrToUpper() and StrSub()?
Mathias (Author):
I think it should be possible
Would be very similar I think..
Except I do not know what ToTitleCase(..) does ? You have to recreate that in script I think.
But should not be to hard. String manipulation is supported..
And as Alan say.. StrToUpper and StrSub would be something to use..
If you try to write something, then leave a post if you get stuck I might be able to help out
Ulfhednar:
Thanks for the replies. :)
I am using StrToUpper and StrSub . The trouble probably is my finding the right approach.
This was a way I'd looked at word capitalization, but a regex gives me all the instances quicker.
--- Code: --- ...
@var $arrN = StrSplit( $NameA, " ");
@var $Words = $arrN[1];
@var $Char1 = StrSub($Words, 0, 1);
@var $arrN1 = StrToUpper($Char1);
...
--- End code ---
Via regex I can have the number of instances output, hence my search for a regex replace function.
Using StrRegExpFind & a simple \s[a-z] gives me all the lower case words, I should in theory be able to take each instance ($1 $2 etc) & handle it with the StrToUpper - but that doesn't seem to be possible (tho it may be just me not getting the <str> &/or <arr> code right!) so I wondered if there might be a StrRegExpReplace function...
Can I pass the instances found by StrRegExpFind to the StrToUpper function? :-\
The exception list should be straight forward enough - I can just run them one by one, would be nice if I could pass the exception array as in the C# above tho! The C# seems to require a lot less steps to achieve the result.
My plan was
* acquire the selected file name(s)
* select the words ignoring 1st word
* all words to lower case
* capitalize all words
* find words in the exception list & make them lower case
* write the new title case string to the filename.
I don't know if I'm misunderstanding you Mathias ref - ToTitleCase - could just be how the author has labelled the process the code invokes & displays on the GUI as opposed to a specific C# function....?
The idea was to take a title eg
ANT-MAN AND THE WASP | ant-man and the wasp | Ant-Man And The Wasp
& convert it to Title Case = Ant-Man and the Wasp
(Altho I think in this case it may need to be Ant-Man and The Wasp... ::) :P)
This process obviously applies to many titles where simply using Camel-Case doesn't look pretty/over-emphasizes secondary prepositions etc.
I liked the idea so thought it might be fun (!) to try to recall how to script with MS ::) ;D
Mathias (Author):
Here is a function in MultiScript that does TitleCase. kind of..
--- Code: ---@var $exceptionsArray[] = { "a", "an", "and", "any", "at", "from", "into", "of", "on", "or", "the", "to", };
function TitleCase($name)
{
return _TitleCase($name, " ");
}
function _TitleCase($name , $splitchar)
{
@var $result = "";
@var $parts = StrSplit($name , $splitchar); // space and - will split string
@var $cnt = arrayCount( $parts );
for( $n = 0; $n < $cnt ; $n++ )
{
@var $textPart = $parts[ $n ];
// To Lower
$textPart = StrToLower( $textPart );
if( StrFind( $textPart , "-", 0 ) > 0)
{
// Becuse of recursive bug, $n is is overwriten
$oldN = $n;
$textPart = _TitleCase($textPart , "-");
$n = $oldN;
}
if( arrayFind( $exceptionsArray, $textPart ) == -1)
{
// Uppser case on first
$textPart[0] = StrToUpper( $textPart[0] );
}
if( StrLen( $result ) > 0 )
$result += $splitchar;
$result += $textPart;
}
return $result;
}
@var $newText = TitleCase("ANT-MAN AND THE WASP");
--- End code ---
Hmm Found a bug with calling recusrive ( same function calling it self.) variables are overwwitten.. so there is a workaround in the code for that..
Navigation
[0] Message Index
[#] Next page
Go to full version