473,796 Members | 2,495 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help on string

Hi,

I want modify a string as "1122334455 66" in
"11:22:33:44:55 :66"

which is the best method?

Using a RegExp? and if yes how?

Thanks in advance
Nov 16 '05 #1
14 1500
Hi,

Does the string has the same length always?
what is the criteria for include the ":"
From the answers to these question depends the solutions.

if you always want to split in two continuos chars you could do something
like this

StringBuilder sb = new StringBuilder()
int start = 0;
string str = "1122334455 66";
while ( str.Length - start > 2 )
{
if ( sb.Length == 0 )
sb.Append( str.substring( start, 2 ) );
else
{
sb.Append( ":" );
sb.Append( str.substring( start, 2 ) );
}
start +=2;
}
This may give you some ideas, just notice that I wrote the text here and it
may have errors

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"SnakeS" <Sn***********@ hotmail.com> wrote in message
news:Cc******** ***********@tor nado.fastwebnet .it...
Hi,

I want modify a string as "1122334455 66" in
"11:22:33:44:55 :66"

which is the best method?

Using a RegExp? and if yes how?

Thanks in advance

Nov 16 '05 #2
Yes it's perfect!

I thinked that could be a code more short.

Thanks for all
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:%2******** *******@TK2MSFT NGP11.phx.gbl.. .
Hi,

Does the string has the same length always?
what is the criteria for include the ":"
From the answers to these question depends the solutions.

if you always want to split in two continuos chars you could do something
like this

StringBuilder sb = new StringBuilder()
int start = 0;
string str = "1122334455 66";
while ( str.Length - start > 2 )
{
if ( sb.Length == 0 )
sb.Append( str.substring( start, 2 ) );
else
{
sb.Append( ":" );
sb.Append( str.substring( start, 2 ) );
}
start +=2;
}
This may give you some ideas, just notice that I wrote the text here and it may have errors

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"SnakeS" <Sn***********@ hotmail.com> wrote in message
news:Cc******** ***********@tor nado.fastwebnet .it...
Hi,

I want modify a string as "1122334455 66" in
"11:22:33:44:55 :66"

which is the best method?

Using a RegExp? and if yes how?

Thanks in advance


Nov 16 '05 #3
C Addison Ritchie <CA************ *@discussions.m icrosoft.com> wrote:
This seems to work well.

using System.Text;
using System.Text.Reg ularExpressions ;

// your string "1122334455 66";
string s = "1122334455 66";

// create the regular expression
Regex re = new Regex("(\\d\\d) ");

// find the matches
MatchCollection matches = re.Matches();

// build the string
string result = String.Format(" {0}:{1}:{2}:{3} :{4}:{5}",
matches[0].Value,
matches[1].Value,
matches[2].Value,
matches[3].Value,
matches[4].Value,
matches[5].Value);

return result;

The input string must be 12 characters long and all numbers. I would
assert that this is true before executing the above code.


That's terrible in terms of performance though - there's really no need
to use a regex here, IMO. Here's code which I believe would be
significantly faster:

if (s.Length != 12)
{
// Whatever your error handling is
}
foreach (char c in s)
{
if (c < '0' || c > '9')
{
// Whatever your error handling is
}
}

return String.Concat(s .Substring(0, 2), ":",
s.Substring(2, 2), ":",
s.Substring(4, 2), ":",
s.Substring(6, 2), ":",
s.Substring(8, 2), ":",
s.Substring(10, 2));

Alternatively, for the last bit:

char[] result = new char[17];
int resultIndex=0;
int origIndex=0;
for (int i=0; i < 6; i++)
{
result[resultIndex++]=s[origIndex++];
result[resultIndex++]=s[origIndex++];
if (i != 5)
{
result[resultIndex++]=':';
}
}
return new string(result);

(That avoids creating too many extra strings.)

I don't know which would be faster - or using a StringBuilder would be
better - but I don't think using a Regex here is a good idea.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
For asserting that the string is valid you can put this at the top of the method:

Regex ex = new Regex(@"^\d{12} $");
if (!re.IsMatch(s) )
throw new ApplicationExce ption("Not a valid string");

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.
"SnakeS" wrote:
Hi,

I want modify a string as "1122334455 66" in
"11:22:33:44:55 :66"

which is the best method?

Using a RegExp? and if yes how?

Thanks in advance

Nov 16 '05 #5
Jon you are very correct in your assertions. I threw all three solutions
out there in a loop of 30,000 iterations just for fun. The regular
expression code came in about 1.5 seconds for 30,000 iterations. Your first
solution came in at about 0.1 seconds for 30,000 iterations but your second
solution blew them all away coming in at 0.02 seconds for the 30,000
iterations.

Like you am sure the slow down is in the Regular Expressions.
_______________ __________
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
C Addison Ritchie <CA************ *@discussions.m icrosoft.com> wrote:
This seems to work well.

using System.Text;
using System.Text.Reg ularExpressions ;

// your string "1122334455 66";
string s = "1122334455 66";

// create the regular expression
Regex re = new Regex("(\\d\\d) ");

// find the matches
MatchCollection matches = re.Matches();

// build the string
string result = String.Format(" {0}:{1}:{2}:{3} :{4}:{5}",
matches[0].Value,
matches[1].Value,
matches[2].Value,
matches[3].Value,
matches[4].Value,
matches[5].Value);

return result;

The input string must be 12 characters long and all numbers. I would
assert that this is true before executing the above code.


That's terrible in terms of performance though - there's really no need
to use a regex here, IMO. Here's code which I believe would be
significantly faster:

if (s.Length != 12)
{
// Whatever your error handling is
}
foreach (char c in s)
{
if (c < '0' || c > '9')
{
// Whatever your error handling is
}
}

return String.Concat(s .Substring(0, 2), ":",
s.Substring(2, 2), ":",
s.Substring(4, 2), ":",
s.Substring(6, 2), ":",
s.Substring(8, 2), ":",
s.Substring(10, 2));

Alternatively, for the last bit:

char[] result = new char[17];
int resultIndex=0;
int origIndex=0;
for (int i=0; i < 6; i++)
{
result[resultIndex++]=s[origIndex++];
result[resultIndex++]=s[origIndex++];
if (i != 5)
{
result[resultIndex++]=':';
}
}
return new string(result);

(That avoids creating too many extra strings.)

I don't know which would be faster - or using a StringBuilder would be
better - but I don't think using a Regex here is a good idea.

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

Nov 16 '05 #6
hi

what about this one?

s = "1122334455 66";
for (int i = 2; i < 15; i += 3)
s = s.Insert(i, ":");

regards
rb

"SnakeS" <Sn***********@ hotmail.com> wrote in message
news:Cc******** ***********@tor nado.fastwebnet .it...
Hi,

I want modify a string as "1122334455 66" in
"11:22:33:44:55 :66"

which is the best method?

Using a RegExp? and if yes how?

Thanks in advance

Nov 16 '05 #7
Hi,

You are creating lots of several strings, it;s much better to use a
StringBuilder, see my other post for a possible solution.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Ralf B." <ra*********@mo rtgageuk.com> wrote in message
news:uw******** ******@TK2MSFTN GP12.phx.gbl...
hi

what about this one?

s = "1122334455 66";
for (int i = 2; i < 15; i += 3)
s = s.Insert(i, ":");

regards
rb

"SnakeS" <Sn***********@ hotmail.com> wrote in message
news:Cc******** ***********@tor nado.fastwebnet .it...
Hi,

I want modify a string as "1122334455 66" in
"11:22:33:44:55 :66"

which is the best method?

Using a RegExp? and if yes how?

Thanks in advance


Nov 16 '05 #8
hi

i am creating 5 substrings..

your solution works but has a lot of code..

substring() produces temp strings, too. Also, I suppose StringBuilder
internally works with temporary strings as well.

regards
rb

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:ei******** ******@TK2MSFTN GP11.phx.gbl...
Hi,

You are creating lots of several strings, it;s much better to use a
StringBuilder, see my other post for a possible solution.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Ralf B." <ra*********@mo rtgageuk.com> wrote in message
news:uw******** ******@TK2MSFTN GP12.phx.gbl...
hi

what about this one?

s = "1122334455 66";
for (int i = 2; i < 15; i += 3)
s = s.Insert(i, ":");

regards
rb

"SnakeS" <Sn***********@ hotmail.com> wrote in message
news:Cc******** ***********@tor nado.fastwebnet .it...
Hi,

I want modify a string as "1122334455 66" in
"11:22:33:44:55 :66"

which is the best method?

Using a RegExp? and if yes how?

Thanks in advance



Nov 16 '05 #9
Ralf B. <ra*********@mo rtgageuk.com> wrote:
i am creating 5 substrings..

your solution works but has a lot of code..

substring() produces temp strings, too. Also, I suppose StringBuilder
internally works with temporary strings as well.


StringBuilder uses a temporary string if it needs to be expanded, but
if you give it the right size to start with, it shouldn't need any
temporary measures.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10

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

Similar topics

6
4356
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing result in any way. Who can help me, I thank you very very much. list.cpp(main program) //-------------------------------------------------------------------------- - #pragma hdrstop #pragma argsused
7
2393
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help me. This was inspired by Exercise 7 and Programming Problem 8 in Chapter 3 of our text. I have done Exercise 7 for you: Below you will find the ADT specification for a string of characters. It represents slightly more that a minimal string...
1
574
by: wukexin | last post by:
I write my own class Cfile, I want to know what about implement ctime().Who help me? My use function ctime, I sign it with $$$. my class Cfile: #------------------------ file.h #--------------------------- #include <io.h> #include <ctime> #include <string>
3
3368
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With numarray, help gives unhelpful responses:
6
5000
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
18
3356
by: James Radke | last post by:
Hello, We are currently using a user DLL that when working in VB 6.0 has a user defined type as a parameter. Now we are trying to use the same DLL from a vb.net application and are having some problems getting it to work and we don't know why. Basically the function is accepting the parameters, and then returning an error and never performing the update.
16
2014
by: Allen | last post by:
I have a class that returns an arraylist. How do I fill a list box from what is returned? It returns customers which is a arraylist but I cant seem to get the stuff to fill a list box. I just learning and really need some help bad. Public Shared Function GetAll() As ArrayList Dim dsCustomer As New DataSet() Dim sqlQuery As String = "SELECT Name, Address, PhoneNo " & _ "FROM CustomerTable" Try
1
3725
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
22
2203
by: KitKat | last post by:
I need to get this to go to each folders: Cam 1, Cam 2, Cam 4, Cam 6, Cam 7, and Cam 8. Well it does that but it also needs to change the file name to the same folder where the file is being grabbed, BUT it doesn't. I have tried and tried.....please help example: C:\Projects\Darryl\Queue Review Files\2-24\Cam 7\Cam7-20060224170000-01.jpg Cam7 but all I keep getting is Cam1, as the beginning of the jpg name,...:( HELP!
6
4033
by: JonathanOrlev | last post by:
Hello everyone, I have a newbe question: In Access (2003) VBA, what is the difference between a Module and a Class Module in the VBA development environment? If I remember correctly, new types of objects (classes) can only be defined in Class modules.
0
9683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10231
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10176
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10013
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9054
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7550
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2927
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.