Reputation: 1109
I am using C#.NET and Windows CE Compact Framework. I have a code where in it should separate one string into two textboxes.
textbox1 = ID
textbox2 = quantity
string BarcodeValue= "+0000010901321 JN061704Z00";
textbox1.text = BarcodeValue.Remove(0, BarcodeValue.Length - BarcodeValue.IndexOf(' ') + 2);
//Output: JN061704Z00
textbox2.text = BarcodeValue.Remove(10, 0).TrimStart('+').TrimStart('0');
//Output: should be 1090 but I am getting a wrong output: 10901321 JN061704Z00
//Please take note that 1090 can be any number, can be 999990 or 90 or 1
Can somebody help me with this? :((
THANKS!!
Upvotes: 1
Views: 16036
Reputation: 39329
The Remove(10,0)
removes zero characters. You want Remove(10)
to remove everything after position 10.
See MSDN for the two versions.
Alternatively, use Substring(0,10)
to get the first 10 characters.
Upvotes: 1
Reputation: 142873
use Split()
string BarcodeValue= "+0000010901321 JN061704Z00";
string[] tmp = BarcodeValue.Split(' ');
textbox1.text = tmp[1];
textbox2.text= tmp[0].SubString(0,tmp[0].Length-4).TrimStart('+').TrimStart('0');
Upvotes: 4
Reputation: 10030
string BarcodeValue = "+0000010901321 JN061704Z00";
var splittedString = BarcodeValue.Split(' ');
TextBox1.Text = splittedString [0].Remove(10).TrimStart('+').TrimStart('0');
TextBox2.Text = splittedString [1];
Output-
TextBox1.Text = 1090
TextBox2.Text = JN061704Z00
Upvotes: 0
Reputation: 1698
This works only if the barcodeValue length is always const.
string[] s1 = BarcodeValue.Split(' ');
textBox1.Text = s1[0];
textBox2.Text = s1[1];
string _s = s1[0].Remove(0, 6).Remove(3, 4);
textBox3.Text = _s;
Upvotes: 0
Reputation: 17600
Use Split
method:
string BarcodeValue = "+0000010901321 JN061704Z00";
var splitted = BarcodeValue.Split(' '); //splits string by space
textbox1.text = splitted[1];
textbox2.text = splitted[0].Remove(10).TrimStart('+').TrimStart('0');
you probably should check if splitted length is 2 before accessing it to avoid IndexOutOfBound
exception.
Upvotes: 5
Reputation: 32701
a slightly better version of the code posted above.
string BarcodeValue= "+0000010901321 JN061704Z00";
if(BarcodeValue.Contains(' '))
{
var splitted = BarcodeValue.Split(' ');
textbox1.text = splitted[1];
textbox2.text = splitted[0].TrimStart('+').TrimStart('0');
}
Upvotes: 1
Reputation: 10140
static void Main(string[] args)
{
string BarcodeValue = "+0000010901321 JN061704Z00";
var text1 = BarcodeValue.Split(' ')[1];
var text2 = BarcodeValue.Split(' ')[0].Remove(10).Trim('+');
Console.WriteLine(text1);
Console.WriteLine(Int32.Parse(text2));
}
Result:
JN061704Z00
1090
Upvotes: 2