Skip to content
View in the app

A better way to browse. Learn more.

MSFN

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Capital after every space, anything faster than replacing ' a'

Featured Replies

In my application I need to capitalize the first character after each space in a string, is there anything faster than this:

var
mystring:string;

begin
mystring:=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?

you could put it in a loop.

depending upon the languae you are using you may be able to do

for x:=a to z
mystring:=stringreplace(mystring, ' ' & x, ' ' & upcase(x), [rfReplaceAll]);
next

or

for x:=97 to 122
mystring:=stringreplace(mystring, ' ' & chr(x), ' ' & chr(x-32), [rfReplaceAll]);
next

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

you could split the string using space as a delimiter, capitalise the first letter and then concatenate them back together again

you could split the string using space as a delimiter, capitalise the first letter and then concatenate them back together again

I like that idea. Or does stringreplace allow for wildcards? That be too easy.

you could split the string using space as a delimiter, capitalise the first letter and then concatenate them back together again
That 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: da­mn indents won't stay, isn't that the point of [­code] tags? :angry:

Edited by LLXX

you could split the string using space as a delimiter, capitalise the first letter and then concatenate them back together again
That 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 :P

Edited by ColdFusion200

  • Author

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 by BrainDrain

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.