I have written two lines of code below in vb6. The code is :
d = InStr(s, data1, Chr(13), 1) ' Fine 13 keycode(Enter) form a text data.
sSplit2 = Split(g, Chr(32)) ' Split with 13 Keycode(Enter)
But I can't write above code in C#. Please help me out. How can I write the above code in C#.
I believe you are looking for string.Split:
string str = "Test string" + (char)13 + "some other string";
string[] splitted = str.Split((char)13);
Or you can use:
string[] splitted = str.Split('\r');
For the above you will get two strings in your splitted array.
the equivalnt code for sSplit2 = Split(g, Chr(32)) is
string[] sSplit2 = g.Split('\n');
int index = sourceStr.IndexOf((char)13);
String[] splittArr = sourceStr.Split((char)13);
const char CarriageReturn = (char)13;
string testString = "This is a test " + CarriageReturn + " string.";
//find first occurence of CarriageReturn
int index = testString.IndexOf(CarriageReturn);
//split according to CarriageReturn
string[] split = testString.Split(CarriageReturn);
If you want to encapsulate the carriage return depending on whether you are running in a unix or non unix environment you can use Environment.NewLine . See http://msdn.microsoft.com/en-us/library/system.environment.newline(v=vs.100).aspx .
string testString2 = "This is a test " + Environment.NewLine + " string.";
//find first occurence of Environment.NewLine
int index2 = testString2.IndexOf(Environment.NewLine);
//split according to Environment.NewLine
string[] split2 = testString2.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
Related
I need to insert in a specific position of the string line, another string, so I compute the specific position for start to insert:
string info1 = "info1";
string info2 = "info2";
string info3 = "info3";
string info4 = "info4";
string keyWord = "BELEGIT";
start = line.IndexOf(keyWord, 0) + keyWord.Length + 13;
var aStringBuilder = new StringBuilder(line);
aStringBuilder.Remove(start, 19);
line = aStringBuilder.ToString();
string newLine = line.Insert(start, "\r\n" + info1 + "\r\n" + "\r\n" + info2 + "\r\n" + info3 + "\r\n" + info4 + "\r\n");
(newLine will be the content of a file in my application).
newline contains the correct content except the string "00000" that inserts after "info4". So in my new file with the content that is newline there is newline and immediately after "00000". I do not really understand why.
Thanks in advance.
INPUT:
line contains:
#~11\r\nT-02040121R\r\n\r\n\r\n\r\n\r\n\r\n\n2.000000000\r\n
OUTPUT
newLine contains:
#~11\r\nT-02040121R\r\ninfo1\r\n\r\ninfo2\r\ninfo3\r\ninfo4\r\n00000\r\n
Assuming that you want just the first 19 chars of lineyou could use Substringto get them and string.Formatto build the new string.
Something like this
var start = line.Substring(0, 19);
string newLine = $"{start}\r\n{info1}\r\n\r\n{info2}\r\n{info3}\r\n{info4}\r\n";
The second line is the short form for
string newLine = string.Format("{0}\r\n{1}\r\n\r\n{2}\r\n{3}\r\n{4}\r\n", start, info1, info2, info3, info4);
if you need more information about string.Formathave a look at the MSDN.
I have string string text = "1.2788923 is a decimal number. 1243818 is an integer. "; Is there a way to split it on the commas only ? This means to split on ". " but not on '.'. When I try string[] sentences = text.Split(". "); I get method has invalid arguments error..
Use String.Split Method (String[], StringSplitOptions) to split it like:
string[] sentences = text.Split(new string[] { ". " },StringSplitOptions.None);
You will end up with two items in your string:
1.2788923 is a decimal number
1243818 is an integer
You can use Regex.Split:
string[] parts = Regex.Split(text, #"\. ");
The string(s) that you splitting on are expected to be in a separate array.
String[] s = new String[] { ". " };
String[] r = "1. 23425".Split(s, StringSplitOptions.None);
Use Regular Expressions
public static void textSplitter(String text)
{
string pattern = "\. ";
String[] sentences = Regex.Split(text, pattern);
}
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!!
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.
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');
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
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');
}
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.
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;
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
I have a question...kind of basic but I thought I can take some help from you guys
I am encrypting a file and the information I encrypt is
LoginTxtBox.Text + "/" + PwdTxtBox.Text + "/" + InstNameTextBox.Text + "/" + DBNameTxtBox.Text;
When I decrypt it ... I am doing:
StringBuilder sClearText = new StringBuilder();
encryptor.Decrypt(sPrivateKeyFile, sDataFile, sClearText);
//username/password
string s = sClearText.ToString();
string[] split = s.Split(new Char[] { '/' });
if (split.Length == 4)
{
split0 = split[0];
split1 = split[1];
split2 = split[1];
split3 = split[1];
Now the requirement I got is I need to count the delimiters in the decrypted format of string and if there are more than 2 delimiter then its not a new application. If there is only one delimiter then its a never used application. I don't know how to to count the delimiters from the decrypt string...Help me plzz
try with this code
Regex.Matches( s, "/" ).Count
Some more ways:
int delimiters = input.Count(x => x == '/');
-or-
int delimiters = input.split('/').Length - 1;
Couldn't you split the string on the character delimeter and the resulting array should contain one more than the number of delimeters?
I want to split a string into an array. The string is as follows:
:hello:mr.zoghal:
I would like to split it as follows:
hello mr.zoghal
I tried ...
string[] split = string.Split(new Char[] {':'});
and now I want to have:
string something = hello ;
string something1 = mr.zoghal;
How can I accomplish this?
String myString = ":hello:mr.zoghal:";
string[] split = myString.Split(':');
string newString = string.Empty;
foreach(String s in split) {
newString += "something = " + s + "; ";
}
Your output would be:
something = hello; something = mr.zoghal;
For your original request:
string myString = ":hello:mr.zoghal:";
string[] split = myString.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
var somethings = split.Select(s => String.Format("something = {0};", s));
Console.WriteLine(String.Join("\n", somethings.ToArray()));
This will produce
something = hello;
something = mr.zoghal;
in accordance to your request.
Also, the line
string[] split = string.Split(new Char[] {':'});
is not legal C#. String.Split is an instance-level method whereas your current code is either trying to invoke Split on an instance named string (not legal as "string" is a reserved keyword) or is trying to invoke a static method named Split on the class String (there is no such method).
Edit: It isn't exactly clear what you are asking. But I think that this will give you what you want:
string myString = ":hello:mr.zoghal:";
string[] split = myString.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
string something = split[0];
string something1 = split[1];
Now you will have
something == "hello"
and
something1 == "mr.zoghal"
both evaluate as true. Is this what you are looking for?
It is much easier than that. There is already an option.
string mystring = ":hello:mr.zoghal:";
string[] split = mystring.Split(new char[] {':'}, StringSplitOptions.RemoveEmptyEntries);