473,397 Members | 2,099 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,397 software developers and data experts.

String Manipulation - C#

G
Hello,

I have a POSTCODE field which allows values for example:

SE12 8JA
B3 1AB
G1 1ET
LA1 2AG

I would like to in code behind, take the first ONE or TWO alpha non
numerical characters, and store them in a second TextField called "Area" in
the format of [SE].

So for example:
SE12 8JA = [SE]
B3 1AB = [b]
G1 1ET = [G]
LA1 2AG = [LA]

The brackets are important, as is the ability to IGNORE everything from the
first numeric charcter onwards.

Any help appreciated.

Feb 6 '07 #1
7 5148
G
A better example of what i'm trying to achieve:

I would like look at a user-entered string, and copy all characters starting
from the left until a number is found. As soon as a number is detected, the
copy should stop and the chracters stored in a second textfield. Sounds
simple but haven't the foggiest how to do it.

For example:

Texfield1.Text = "Bryan2000"
TextField2.Text = "Bryan"

or

TextField1.Text = "H500"
TextField2.text = "H"

or

TextField1.Text = "900TEST"
TextField2.text = ""

Any ideas?

Regards,

G.

Feb 6 '07 #2
string has an indexer that takes an integer and returns the char at the
specified position. ("foo"[0] equalst 'f')
As you may also know, strings can be concatenated by using the + operator
("foo " + "boo" equals "foo boo")
There is also such a thing as for loop. (string res = ""; for (int i = 0; i
< "foo".Length; i++) { if (char.IsLetter("foo"[i]) res += "foo"[i]) else
break;)
"G" <g@nospam.comwrote in message
news:58**********************************@microsof t.com...
>A better example of what i'm trying to achieve:

I would like look at a user-entered string, and copy all characters
starting
from the left until a number is found. As soon as a number is detected,
the
copy should stop and the chracters stored in a second textfield. Sounds
simple but haven't the foggiest how to do it.

For example:

Texfield1.Text = "Bryan2000"
TextField2.Text = "Bryan"

or

TextField1.Text = "H500"
TextField2.text = "H"

or

TextField1.Text = "900TEST"
TextField2.text = ""

Any ideas?

Regards,

G.

Feb 6 '07 #3
On Feb 6, 7:46 am, "G" <g...@nospam.comwrote:
Hello,

I have a POSTCODE field which allows values for example:

SE12 8JA
B3 1AB
G1 1ET
LA1 2AG

I would like to in code behind, take the first ONE or TWO alpha non
numerical characters, and store them in a second TextField called "Area" in
the format of [SE].

So for example:
SE12 8JA = [SE]
B3 1AB = [b]
G1 1ET = [G]
LA1 2AG = [LA]

The brackets are important, as is the ability to IGNORE everything from the
first numeric charcter onwards.

Any help appreciated.
You want "regular expressions" for this, which are in the Regex
namespace in the .NET Framework. Read up on regular expressions a bit
and then try this one:

^([A-Z]*)[0-9]*.*$

which will match zero or more alphabetics, followed by zero or more
numerics, followed by any other characters. You can, of course, write
a Regex to validate the postcode string precisely.

Feb 6 '07 #4
Lebesgue <le******@gmail.comwrote:
string has an indexer that takes an integer and returns the char at the
specified position. ("foo"[0] equalst 'f')
As you may also know, strings can be concatenated by using the + operator
("foo " + "boo" equals "foo boo")
There is also such a thing as for loop. (string res = ""; for (int i = 0; i
< "foo".Length; i++) { if (char.IsLetter("foo"[i]) res += "foo"[i]) else
break;)
However, it would be much better to work out how many characters to
take, and then just use Substring...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 6 '07 #5
Of course, Jon. As you may have noted, my response was being a little
sarcastic. If someone has _absolutely_ no idea how to accomplish this kind
of task and I start thinking about someone who might be paying him for
programming...
Sorry for the not-so-good response though, I should have not responded at
all.

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Lebesgue <le******@gmail.comwrote:
>string has an indexer that takes an integer and returns the char at the
specified position. ("foo"[0] equalst 'f')
As you may also know, strings can be concatenated by using the + operator
("foo " + "boo" equals "foo boo")
There is also such a thing as for loop. (string res = ""; for (int i = 0;
i
< "foo".Length; i++) { if (char.IsLetter("foo"[i]) res += "foo"[i]) else
break;)

However, it would be much better to work out how many characters to
take, and then just use Substring...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Feb 6 '07 #6
G
Of course, Jon. As you may have noted, my response was being a little
sarcastic. If someone has _absolutely_ no idea how to accomplish this kind
of task and I start thinking about someone who might be paying him for
programming...

I pay my own wages, and have done so for over 10 years. I would never sell
my "web development services" as I am insufficiently skilled to do so.

Saying that, I have coded web sites in classic ASP for years and continue to
do so, all of them profitiable enough to keep me working for myself. I
thought it about to time to learn .NET, an option I choose - not a forced
requirement.

Thanks for your advice anyway.

G.

Feb 7 '07 #7
G
You want "regular expressions" for this, which are in the Regex
namespace in the .NET Framework. Read up on regular expressions a bit
and then try this one:

^([A-Z]*)[0-9]*.*$

which will match zero or more alphabetics, followed by zero or more
numerics, followed by any other characters. You can, of course, write
a Regex to validate the postcode string precisely.

Thanks for taking the time to help out.

Regards,

G.
Feb 7 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dim | last post by:
I found that C# has some buggy ways to process string across methods. I have a class with on global string var and a method where i add / remove from this string Consider it a buffer... with some...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
10
by: micklee74 | last post by:
hi if i have a some lines like this a ) "here is first string" b ) "here is string2" c ) "here is string3" When i specify i only want to print the lines that contains "string" ie the first...
5
by: Niyazi | last post by:
Hi, Does anyone knows any good code for string manipulation similar to RegularExpresion? I might get a value as string in a different format. Example: 20/02/2006 or 20,02,2006 or ...
3
by: crprajan | last post by:
String Manipulation: Given a string like “This is a string”, I want to remove all single characters( alphabets and numerals) like (a, b, 1, 2, .. ) . So the output of the string will be “This is...
7
Frinavale
by: Frinavale | last post by:
I currently have a .NET application that has an object which passes a string (a connection string) as a parameter to another object that does database manipulation. This string isn't stored...
3
by: frankeljw | last post by:
I have 2 Java strings 1st String is a series of names, colons, and numbers ie) Name1:13:Name2:4526:Name3:789:Name4:3729:Name5:6:Name6:44 2nd String is a name ie) Name2 I need to get the...
22
by: mann_mathann | last post by:
can anyone tell me a solution: i cannot use the features in standard c++ string classgh i included the string.h file but still its not working.
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.