473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

string manupulation in .net, c#

121 100+
hello all,
i have a string as
MyNameIsJOHN

but now i want it to be displayed as 'My Name Is JOHN'. ( space in between)

i have used
Expand|Select|Wrap|Line Numbers
  1.  System.Text.RegularExpressions.Regex.Replace("MyNameIsJOHN", "[A-Z]", " $0").Trim()
and i am getting 'My Name Is J O H N' , my problem is how should i get JOHN in one word rather than with space(s).

any idea
Feb 18 '09 #1
3 2645
IanWright
179 100+
The reason for it is your Regex expression...

"[A-Z]"

In your code this will replace any occurance of an uppercase letter with the space and the letter.

You'll probably want to check for something that starts with an upper case, and is not followed by another uppercase letter... however you may then struggle with something like : IAm...
Feb 18 '09 #2
balabaster
797 Expert 512MB
This should do the job:

[AI](?![A-Z]{2,})[a-z]*|[A-Z][a-z]+|[A-Z]{2,}(?=[A-Z]|$)

Replacement string is "$0[space]", where [space] should be replaced with an actual space character.

It will handle MyNameIsJOHN, IAmJOHN, WhatsUpWithJOHN etc. It also handles things like: WhatsUpWithJOHNToday, WhatsUpWithENGLANDToday, WhatsUpWithINDIAToday, FRANCERules

Note that between [AI] and [A-Z][a-z]+ that's a pipe char, not an uppercase i or lowercase L. Inside the [] is an uppercase a and i as they're the only single letter words I can think that are valid. If you want other valid single letter words, just insert the characters into that token to include them. Each match is a valid word in the string. Any words made entirely of upper case letters are treated as a single word.

This seems to handle most of the edge cases, i.e. uppercase names that contain valid single letter words. It basically defines a "word" token which can then be appended with a space. Maybe there are some odd cases where it doesn't work, but I can't think of any off the top of my head.

The only annoying thing I don't have time to figure out is how to stop it appending a space to the last word of the string...but you can resolve that using a simple trim on the resulting string. It would perhaps be more suitable in the longer term to have it ignore the last word and hence not add that final space though.
Feb 18 '09 #3
jay123
121 100+
thanks balabaster for your response..
Feb 20 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
2
by: s | last post by:
I'm getting compile errors on the following code: <code> #include <iostream> #include <fstream> #include <list> #include <string> using namespace std;
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.