473,499 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q: Get characters from string and formatting.

Hi!

I have a string, it could look like this

455 56 Gothenburg or 45556 Gothenburg

What i want to do is to separate the numbers from the text into two
different strings
and the numbers i would like to formate like ### ##.

My brain is just not giving me any direction on how to solve this :D

//Martin, Sweden
Nov 17 '05 #1
2 1314
Visual Systems AB (Martin Arvidsson) wrote:
I have a string, it could look like this

455 56 Gothenburg or 45556 Gothenburg

What i want to do is to separate the numbers from the text into two
different strings
and the numbers i would like to formate like ### ##.

My brain is just not giving me any direction on how to solve this :D


Using a regular expression could be a good idea. Consider this code:

Match match = Regex.Match("455 56 Gothenburg",
@"(?<numpart1>\d{3})\s*(?<numpart2>\d{2})\s+(?<tex t>.*)");
if (match.Success) {
string newString = match.Groups["numpart1"].Value + " " +
match.Groups["numpart2"].Value + " " + match.Groups["text"].Value;
// Now newString contains the needed format.
}

This will work just the same whether you pass in the syntax "455 56
Gothenburg" or "45556 Gothenburg".
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
Nov 17 '05 #2
Your question is not specific enough. You didn't describe the rules for the
format of the strings; you gave 2 possible examples. For example, could the
string in question look like this:

4 5 5 5 6 Gothenburg

or 45 556 Whatever

or 45556 Something with spaces

?

The best answer depends on the formatting rules for the source string. For
example, you could cover all possible bases by:

string source = "4 55 56 Gothenburg";
int i, len, pos = 0;
StringBuilder sb = new StringBuilder();
len = source.Length;
sb.Capacity = len;
for (i = 0; i < len; i++)
{
if (Char.IsDigit(source[i])
{
sb.Append(source[i]);
if (++pos == 3) sb.Append(' ');
else if (pos == 5)
{
sb.Append(' ');
do (i++) while (i < len && Char.IsSeparator(source[i]);
while (i < len) sb.Append(source[i]);
}
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Big things are made up of
lots of little things.
"Visual Systems AB (Martin Arvidsson)" <ma**************@vsab.net> wrote in
message news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi!

I have a string, it could look like this

455 56 Gothenburg or 45556 Gothenburg

What i want to do is to separate the numbers from the text into two
different strings
and the numbers i would like to formate like ### ##.

My brain is just not giving me any direction on how to solve this :D

//Martin, Sweden

Nov 17 '05 #3

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

Similar topics

10
4810
by: Oliver S. | last post by:
I've developed a string-class that holds the string in an array which is a member-variable of the class and that has maximum-size which is con- figurable through a template-parameter. If any...
5
9134
by: Jain, Pranay Kumar | last post by:
Hello Everyone, I have written a simple app. that converts the dataset into excelspreadsheet. The App. uses the following architecture. First it generates the dataset with corresponding...
5
13270
by: Brian Reed | last post by:
I have a class that I want to serialize to an XML string. I want the XML to serialize to utf-8 encoding. When I serialize to an XML file, the data looks great. When I try to serialize to a String...
1
6907
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working...
9
2194
by: Andreas Prilop | last post by:
Unicode Technical Report #20 http://www.unicode.org/reports/tr20/ considers bidirectional control (formatting) characters as "not suitable for use with markup". This has sometimes been...
29
2314
by: dbhbarton | last post by:
Had a thought that's grown on me. No idea if it's original or not- too inexperienced in programming- but I guess there's no harm floating it out there. Python wins big on readability, and...
5
6854
by: Rob | last post by:
Hi, I have a VB.Net application that parses an HTML file. This file was an MS Word document that was saved as web page. My application removes all unnecessary code generated by MS Word and does...
4
13395
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I have a multiline RichTextBox that I use for data logging. I'm concerned about how the AppendText method reacts when over time the maximum number of characters have been added. Do the...
1
1811
by: Brock | last post by:
Thanks in advance... I have a string that I'm building that needs to write to a file the last name, first name and middle initial in this format with a maximum of 20 characters. Two examples: ...
0
7134
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
7012
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...
1
6901
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
5479
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,...
1
4920
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...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.