BrainDrain Posted January 18, 2007 Share Posted January 18, 2007 In my application I need to capitalize the first character after each space in a string, is there anything faster than this:varmystring:string;beginmystring:=stringreplace(mystring, ' a', ' A', [rfReplaceAll]);mystring:=stringreplace(mystring, ' b', ' B', [rfReplaceAll]);mystring:=stringreplace(mystring, ' c', ' C', [rfReplaceAll]);mystring:=stringreplace(mystring, ' d', ' D', [rfReplaceAll]);mystring:=stringreplace(mystring, ' e', ' E', [rfReplaceAll]);mystring:=stringreplace(mystring, ' f', ' F', [rfReplaceAll]);mystring:=stringreplace(mystring, ' g', ' G', [rfReplaceAll]);mystring:=stringreplace(mystring, ' h', ' H', [rfReplaceAll]);mystring:=stringreplace(mystring, ' i', ' I', [rfReplaceAll]);mystring:=stringreplace(mystring, ' j', ' J', [rfReplaceAll]);Down to z and Z, it works, but it seems like a dirty, or roundabout method, is there any easier way of doing this? Link to comment Share on other sites More sharing options...
IcemanND Posted January 18, 2007 Share Posted January 18, 2007 you could put it in a loop.depending upon the languae you are using you may be able to dofor x:=a to z mystring:=stringreplace(mystring, ' ' & x, ' ' & upcase(x), [rfReplaceAll]);nextorfor x:=97 to 122 mystring:=stringreplace(mystring, ' ' & chr(x), ' ' & chr(x-32), [rfReplaceAll]);nextOr another optiopn would be to go throught the string on character at a time and when you find a space you replace the next character with uppercase. Link to comment Share on other sites More sharing options...
ColdFusion200 Posted January 18, 2007 Share Posted January 18, 2007 you could split the string using space as a delimiter, capitalise the first letter and then concatenate them back together again Link to comment Share on other sites More sharing options...
IcemanND Posted January 18, 2007 Share Posted January 18, 2007 you could split the string using space as a delimiter, capitalise the first letter and then concatenate them back together againI like that idea. Or does stringreplace allow for wildcards? That be too easy. Link to comment Share on other sites More sharing options...
LLXX Posted January 20, 2007 Share Posted January 20, 2007 (edited) you could split the string using space as a delimiter, capitalise the first letter and then concatenate them back together againThat is even moar inefficient (string splitting and concatenation involves a huge amount of overhead in most HLLs).Remember that stringreplace has to make a complete pass over the input string for *every* character you want to replace. Why not uppercase them all at once in one pass?I shall provide a reasonably efficient method, example is in C.cap_space(char *s) { while(*s) { if((*s==32) && *(s+1)) { /* a space and next will be capitalised if not the terminus */ *(s+1) &= 224; s++; /* don't reexamine already capitalised letters and spaces */ } s++; }}Edit: damn indents won't stay, isn't that the point of [code] tags? Edited January 20, 2007 by LLXX Link to comment Share on other sites More sharing options...
ColdFusion200 Posted January 20, 2007 Share Posted January 20, 2007 (edited) you could split the string using space as a delimiter, capitalise the first letter and then concatenate them back together againThat is even moar inefficient (string splitting and concatenation involves a huge amount of overhead in most HLLs).Remember that stringreplace has to make a complete pass over the input string for *every* character you want to replace. Why not uppercase them all at once in one pass?i aint a professional coder, i program in vb mainly and the size of the programs i make efficiency isnt really important Edited January 20, 2007 by ColdFusion200 Link to comment Share on other sites More sharing options...
BrainDrain Posted January 23, 2007 Author Share Posted January 23, 2007 (edited) Thanks, I got an idea from your code to use the About Delphi "alphabet puzzle", and hacked it to support renaming the lowercase version of ' x' to the uppercase version, it seems to handle large amounts of usage efficiently EDIT: I 'hacked it to support renaming...', not 'I hack it to...' Edited January 23, 2007 by BrainDrain Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now