String Manipulation - C#

  • Thread starter Thread starter G
  • Start date Start date
G

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 =
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.
 
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.
 
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") res += "foo") else
break;)
 
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 =
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.
 
Lebesgue said:
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") res += "foo") else
break;)


However, it would be much better to work out how many characters to
take, and then just use Substring...
 
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 said:
Lebesgue said:
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") res += "foo") else
break;)


However, it would be much better to work out how many characters to
take, and then just use Substring...
 
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.
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top