Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I set "-" in string how can i do it?

For example, 02700060024112 is string
then i store in database string as "0270006-0024-112"

how can i do???
Posted
Comments
Ramug10 24-Feb-14 2:27am    
Not clear at all,need more information on your requirement.
Sergey Alexandrovich Kryukov 24-Feb-14 2:49am    
It is clear enough, and the detail of the requirements are not very relevant.
You better look at some of your own questions...
—SA

I'd prefer a Masked Textbox[^] on the Windows Form where the data are entered, then you'll get the string formatted correctly already here.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Feb-14 2:47am    
Good idea. This post does not answer the question, and perhaps it makes it only more valuable. My 5.

However, I find it very important to explain that strings are immutable (the question says "set character in string", which is impossible), and other things; please see Solution 1.
—SA
It is absolutely impossible to modify any string. Strings are immutable. All methods which "modify" a string actually create a brand new string using the data from source string(s).

You can use the mutable class System.Text.StringBuilder: http://msdn.microsoft.com/en-us/library/system.text.stringbuilder%28v=vs.110%29.aspx[^].

At the end, you still can back to a string, by calling the instance method StringBuilder.ToString() (overridden System.Object.ToString()). But you should understand that it will generate a brand new string.
However, to understand how strings really work, read about string interning:
http://msdn.microsoft.com/en-us/library/system.string.intern(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.string.isinterned(v=vs.110).aspx[^],
http://en.wikipedia.org/wiki/String_interning[^],
http://csharpexperiments.blogspot.com/2013/04/string-interning.html[^].

—SA
 
Share this answer
 
v2
If solution 2 is not an option, you can still go for a regular expression that will extract the three parts of your initial string, allowing you to place your minus sign between them.

This should look like:
C#
using System.Text.RegularExpressions;

public static string GetFormattedValue(string initial) {
   string formatRegex = @"^(?<First>[\d]{7})(?<Second>[\d]{4})(?<Third>[\d]{3})$";
   Regex r = new Regex(formatRegex, RegexOptions.Compiled | RegexOptions.CultureInvariant);
   Match m = r.Match(initial);
   if (m.Success) {
      return string.Format("{0}-{1}-{2}", m.Groups["First"], m.Groups["Second"], m.Groups["Third"]);
   }
   else {
      return string.Empty;
      // OR
      // throw new ArgumentException("initial is not of required format.");
   }
}

But this solution requires that all input strings share the same format.

Hope this helps.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900