473,664 Members | 2,773 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String Formatting (Like in VB)

Hello,

I have a problem, I'm hoping someone can help.

I have a string, lets say: "111111"; // Not always numeric
I have a format mask, lets say: "(00) 0000"; // Again, not always
numeric.

The mask and the value are passed into a method.

private string GiveMeTheFormat tedString(strin g i_Value, string i_Mask)
{

}

I want the output of this method to be: "(11) 1111"

Remember, I can't assume that my input string or my mask will be
numeric and the mask will be different.

Help. :-)

I've tried this:

string retVal = String.Format(" {0:" + iMask + "}", i_Value);

No luck, it always comes back as the original input.

I even tried the above hard coding the format mask in, i.e. ("{0:(00)
0000}",i_Value) ;

still no luck.

Thanks for the help, in advance.

John

Sep 25 '06 #1
6 2594
What is the function in VB that you use to do this? More than likely,
it exists in the Microsoft.Visua lBasic namespace. You can add a reference
to Microsoft.Visua lBasic.dll and use the method that you know will work
(rather than write your own).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<jm*****@fort e-industries.comw rote in message
news:11******** **************@ e3g2000cwe.goog legroups.com...
Hello,

I have a problem, I'm hoping someone can help.

I have a string, lets say: "111111"; // Not always numeric
I have a format mask, lets say: "(00) 0000"; // Again, not always
numeric.

The mask and the value are passed into a method.

private string GiveMeTheFormat tedString(strin g i_Value, string i_Mask)
{

}

I want the output of this method to be: "(11) 1111"

Remember, I can't assume that my input string or my mask will be
numeric and the mask will be different.

Help. :-)

I've tried this:

string retVal = String.Format(" {0:" + iMask + "}", i_Value);

No luck, it always comes back as the original input.

I even tried the above hard coding the format mask in, i.e. ("{0:(00)
0000}",i_Value) ;

still no luck.

Thanks for the help, in advance.

John

Sep 25 '06 #2

In VB, I'd use the Format function.

As I bring code forward from the VB6 world, I'm supposed to work under
the Mantra:

VB = Bad;
VC# = Good.

:-)

So, if I can do it, I'd like to not use the VB namespace.

John

Sep 25 '06 #3

In VB, I'd use the Format function.

As I bring code forward from the VB6 world, I'm supposed to work under
the Mantra:

VB = Bad;
VC# = Good.

:-)

So, if I can do it, I'd like to not use the VB namespace.

John

Sep 25 '06 #4
Bad mantra, especially if you don't have a C++ background. VB 2005 is just
as expressive as VC#. Yes, there are some differences between the
languages, but they are functionally 99+% identical and both are fully
supported by MS.

Mike Ober.
<jm*****@fort e-industries.comw rote in message
news:11******** *************@i 42g2000cwa.goog legroups.com...
>
In VB, I'd use the Format function.

As I bring code forward from the VB6 world, I'm supposed to work under
the Mantra:

VB = Bad;
VC# = Good.

:-)

So, if I can do it, I'd like to not use the VB namespace.

John

Sep 25 '06 #5
See inline.

Willy.

<jm*****@fort e-industries.comw rote in message
news:11******** **************@ e3g2000cwe.goog legroups.com...
| Hello,
|
| I have a problem, I'm hoping someone can help.
|
| I have a string, lets say: "111111"; // Not always numeric
| I have a format mask, lets say: "(00) 0000"; // Again, not always
| numeric.
|

Not sure what you mean with one/both not always being numeric.
But here is how you can format a string.

Here suppose the Mask is ("(000) 0000") and the string something like
"1234567" or " 123.2356" or " +1.234567 " ...
private string GiveMeTheFormat tedString(strin g i_Value, string i_Mask)
{
double dv;
if(Double.TryPa rse(i_Value, NumberStyles.Nu mber,null, out dv))
return dv.ToString(i_M aks);
else return null;
}

If this doesn't suits your needs you will have to implement your own
IFormatProvider .

| The mask and the value are passed into a method.
|
| private string GiveMeTheFormat tedString(strin g i_Value, string i_Mask)
| {
|
| }
|
| I want the output of this method to be: "(11) 1111"
|
| Remember, I can't assume that my input string or my mask will be
| numeric and the mask will be different.
|
| Help. :-)
|
| I've tried this:
|
| string retVal = String.Format(" {0:" + iMask + "}", i_Value);
|
| No luck, it always comes back as the original input.
|
| I even tried the above hard coding the format mask in, i.e. ("{0:(00)
| 0000}",i_Value) ;
|
| still no luck.
|
| Thanks for the help, in advance.
|
| John
|
Sep 25 '06 #6
jm*****@forte-industries.com wrote:
Hello,

I have a problem, I'm hoping someone can help.

I have a string, lets say: "111111"; // Not always numeric
I have a format mask, lets say: "(00) 0000"; // Again, not always
numeric.

The mask and the value are passed into a method.

private string GiveMeTheFormat tedString(strin g i_Value, string i_Mask)
{

}

I want the output of this method to be: "(11) 1111"

Remember, I can't assume that my input string or my mask will be
numeric and the mask will be different.
OK, so what do you want the output to be in these cases:

input: abcdef
mask: (00) 0000

input: -3.14592536
mask: (0) 0-0 (0)

input: 1A2B3C
mask: 00abcdef

?

IF your input is an integer and you want to use a custom numeric format
string, cast to an integer and use ToString():

string formatted = 111111.ToString ("(00) 0000");
// now formatted == "(11) 1111"

To be able to say how to produce results in other cases, we would need
to know what you want to see in those other cases.

--
Larry Lard
la*******@googl email.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Sep 26 '06 #7

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

Similar topics

10
4831
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 operation would grow the string beyond its maximum size, an exeception would be thrown. This kind of string obviously has superior performance over a std::string because there's never any additional memory-allocation. But before I'm going to re-invent...
20
11305
by: hagai26 | last post by:
I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular expressions and I sure there is a lot ways, but I need realy efficient one
4
2424
by: Dennis Myrén | last post by:
Hi. Is there a way to utilize the great primitive data type formatting routines available in .NET without working with strings? I want a byte directly rather than a string. I think it is unfortunate that i have to walk via System.String to do that. For instance, from an integer of value 123, i want:
7
398
by: ilona | last post by:
Hi all, I store phone numbers in the database as 123447775665554(input mask is used for input, and some numbers have extensions), and I also know from db if the number is Canadian, US, or some other country. When I retrieve the phone numbers, I need to display them as (###) ###-#### x 99999 if it is a Canadian number or (###) ###-#### Ext. 99999 if it is US phone number. Potentially I'd have other countries added as well which might have...
4
22989
by: Lauren Wilson | last post by:
Hi folks, We have a need to replace sub strings in certain message text. We use the Office Assistant to display help and often use the imbedded formatting commands. Those of you who have used them know they look like this: "{cf 5}" or "{cf 0}" or "{ul 1}" or "{ul 0}", etc. The commonality they have is that they are always 6 charters long and always begin and end with curly brackets. This all works great if the user is running the...
15
8055
by: angellian | last post by:
Sorry to raise a stupid question but I tried many methods which did work. how can I conserve the initial zero when I try to convert STR(06) into string in SQL statment? It always gives me 6 instead of 06. Thanks a lot.
7
3106
by: L. Scott M. | last post by:
Have a quick simple question: dim x as string x = "1234567890" ------------------------------------------------------- VB 6 dim y as string
11
4992
by: Dustan | last post by:
Is there any builtin function or module with a function similar to my made-up, not-written deformat function as follows? I can't imagine it would be too easy to write, but possible... 'I am coding, and he coded last week.' ('coding', 'coded', 'week') expanded (for better visual): ('coding', 'coded', 'week')
1
8177
by: schoedl | last post by:
Hello, we often compose strings via a ostringstream and then create a string from it. What is the rationale of not being able to use string in place of a ostringstream, so I could write string str; str << ... << ...; SomeAPI( str.c_str() );
2
5584
by: SammyBar | last post by:
Hi all, I'm trying to convert the xml obtained from a XmlReader object into a UTF-8 array. My general idea is to read the XmlReader and write into a MemoryStream. Then convert the MemoryStream bytes into utf-8. MemoryStream ms = new MemoryStream(); XmlTextWriter xmlWriter = new XmlTextWriter(ms, new UTF8Encoding(false)); writer.Formatting = Formatting.Indented;
0
8438
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
8348
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8863
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8779
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
8549
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
8636
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
7376
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...
0
5660
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
4186
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.