How can I write the half-space character in c# in a Textbox (not Console.Writleline statement) ? Is there a specific char code for it?
Unicode THINSPACE U+2009 is a perfectly ordinary character, try:
TextBox.Text += '\u2009';
I don't know what you mean by half-space, but Char.Parse() will probably do what you want. Please get a list of Unicode spaces
Then do this (example is for 1/2 en space; remove all plus signs):
string UnicodeString = "\u2000"; // U+2000
TextBox.Text = Char.Parse(UnicodeString);
However, you probably don't need Char.Parse() for this. You could simply do this:
string halfSpace = "\u2000"; // So you can reuse it
TextBox.Text = halfSpace;
UPDATE: If you want to add it to the TextBox (+=), rather than replacing the whole line (=), use this:
TextBox.Text += halfSpace
Related
I'm trying to parse a string containing a Hex. I need to split it into two parts and convert each into a character. The first part works correctly, but the second returns a number instead of a character.
string chr = "E583";
string result = "";
for (int u = 0; u < chr.Length; u += 2)
result += (char)int.Parse(
chr.Substring(u, 2),
System.Globalization.NumberStyles.HexNumber);
This function returns "å\u0083". The first character translates correctly, the second remains a Hex. Why? Is there a better way of coding this? Thanks! :)
I'm trying to parse a CSV file in C#. Split on commas (,). I got it to work with this:
[\t,](?=(?:[^\"]|\"[^\"]*\")*$)
Splitting this string:
2012-01-06,"Some text with, comma",,"300,00","143,52"
Gives me:
2012-01-06
"Some text with, comma"
"300,00"
"143,52"
But I can't figure out how to lose the "" from the output so I get this instead:
2012-01-06
Some text with, comma
300,00
143,52
Any suggestions?
If you are trying to parse a CSV and using .NET, don't use regular expressions. Use a component that was created for this purpose. See the question CSV File Imports in .Net.
I know the CSV specification looks simple enough, but trust me, you are in for heartache and destruction if you continue down this path.
Why are you using regular expressions for this? Ensuring the file is well-formed?
You can use String.Replace()
String s = "Some text with, comma";
s = s.Replace("\"", "");
// After matched
String line = 2012-01-06,"Some text with, comma",,"300,00","143,52";
String []fields = line.Split(',');
for (int i = 0; i < fields.Length; i++)
{
// Call a function to remove quotes
fields[i] = removeQuotes(fields[i]);
}
String removeQuotes(String s)
{
return s.Replace("\"", "");
}
So, something like this. Again, I wouldn't use RegEx for this purpose, but YMMV.
var sp = Regex.Split(a, "[\t,](?=(?:[^\"]|\"[^\"]*\")*$)")
.Select(s => Regex.Replace(s.Replace("\"\"","\""),"^\"|\"$","")).ToArray();
So, the idea here is that first of all, you want to replace double double quotes with a single double quote. And then that string is fed to the second regex which simply removes double quotes at the beginning and end of the string.
The reason for the first replace is because of strings like this:
var a = "1999,Chevy,\"Venture \"\"Extended Edition, Very Large\"\" Dude\",\"\",\"5000.00\"";
So, this would give you a string like this: ""Extended Edition"", and the double quotes need to be changed to single quotes.
How to update the text from the TextBox control?
Consider a TextBox that already contains the string "Wel"
To insert text in the TextBox, I use:
TextBox1.Text = TextBox1.Text.Insert(3, "come")
And to remove characters from the TextBox:
TextBox1.Text = TextBox1.Text.Remove(3, 4)
But I need to be able to do this:
TextBox1.Text.Insert(3, "come");
TextBox1.Text.Remove(3, 4);
However, this code doesn't update the TextBox.
It this possible?
Can this be accomplished via the append method?
Text property of TextBox is of type string which is immutable it's not possible to change the existing string. Insert() or Remove() returns a new instance of string with the modification and you will have to assign this new instance back to TextBox's Text property.
There is TextBox.AppendText() that you might be interested in. It appends text to the end of the string but you cannot do anything like Insert() or Remove() with it though.
EDIT:
for your keypress, you could do something like this
char charToReplace = (char) (e.KeyChar + 1); // substitute replacement char
textBox1.SelectedText = new string(charToReplace,1);
e.Handled = true;
'string' is a immutable type, so each time string value changes new memory is allocated. So if you want to insert or remove text from TextBox, you have to assign it back to TextBox.Text property. However, if you just want to append text to the TextBox.Text, you can do
textBox.AppendText("Hello");
On Asp.Net, in my textbox, i do not want to enter after zero three numbers.
For example if you enter textbox 0.222 it should be 0.22. After 0.22 you should not enter anything. How can i make this?
Thanks
Use a Masked TextBox.
I think it is better to use an Expression instead of Masked Textbox.
With a simple RegularExpressionValidator you are able to do this, just drag one in your worksheet and then set the TargetControl of it to your Textbox, then set its value to ^\ddd.
Using javascript add an attribute onkeyup='formatNumber(this)' on the tag.
function formatNumber(textBox) {
var current = textBox.value;
var output = "";
//find '000' in current
//delete what you want
textBox.value = output;
}
Substring these string:-
1. ZZ111122
2. ZZZZ222111
3. ZZZZZZZ333
4. ZZZ111333
I have these kind of strings. This value is always starting with Z. And after Z its always either 1 or 2 or 3. But i dont know the number of Zs in the string. So how can i extract all Z from the string
I don't know if I understood right. If you have "ZZZZ222111" and want only "222111", do it:
string test = "ZZZZ222111";
test = test.Substring(test.LastIndexOf("Z") + 1);
If you want only "ZZZZ", do it:
string test = "ZZZZ222111";
test = test.Substring(0, test.LastIndexOf("Z"));
Both ways are very simple. No need of loops or regular expressions.
Sounds like you're going to want to use regular expressions for this.
Use String.Trim function:
ZeroZValue = stringValue.Trim('Z');
String test = "ZZ111122";
String zOnly = test.Substring(0, test.IndexOfAny("123".ToCharArray()));
Take advantage of IndexOfAny(). I am assuming you want only Z's left over ("extract all Z from the string").
This is not difficult. I recommend processing the text line by line.
You can loop the string character by character. You can use regular expressions. Or you could use my sscanf() replacement class for C#.
int start = someString.IndexOf("Z");
int end = someString.LastIndexOf("Z");
someString.Substring(start , end - start);