I have below string as input
string input = "{A.0a,100,0002_VL}{=}{A.0a,0400,0001_VL}{+}{A.0a,0410,0002_VL}{+}{A.0a,0420,0003_VL}"
I want below output as string array
string[] output
output[0] = "A.0a,100,0002_VL"
output[1] = "="
output[2] = "A.0a,0400,0001_VL"
output[3] = "+"
output[4] = "A.0a,0410,0002_VL"
output[5] = "+"
output[6] = "A.0a,0420,0003_VL"
I want to use RegEx.Split function but unable to identity a pattern. Is it possible to use? Kindly help me.
char[] charArray = { '{', '}' };
var inputArray = input.Split( charArray, StringSplitOptions.RemoveEmptyEntries);
Related
My Question consists of how might i split a string like this:
""List of devices attached\r\n9887bc314\tdevice\r\n12n1n2nj1jn2
\tdevice\r\n\r\n"
Into:
[n9887bc314,n12n1n2nj1jn2]
I have tried this but it throws the error "Argument 1: cannot convert from 'string' to 'char'"
string[] delimiterChars = new string[] {"\\","r","n","tdevice"};
string y = output.Substring(z+1);
string[] words;
words = y.Split(delimiterChars, StringSplitOptions.None);
I'm wondering if i'm doing something wrong because i'm quite new at c#.
Thanks A Lot
First of all String.Split accept strings[] as delimiters
Here is my code, hope it will helps:
string input = "List of devices attached\r\n9887bc314\tdevice\r\n12n1n2nj1jn2\tdevice\r\n\r\n";
string[] delimiterChars = {
"\r\n",
"\tdevice",
"List of devices attached"
};
var words = input.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);
foreach (var word in words)
{
Console.WriteLine(word);
}
Split the whole string by the word device and then remove the tabs and new lines from them. Here is how:
var wholeString = "List of devices attached\r\n9887bc314\tdevice\r\n12n1n2nj1jn2\tdevice\r\n\r\n";
var splits = wholeString.Split(new[] { "device" }, StringSplitOptions.RemoveEmptyEntries);
var device1 = splits[1].Substring(splits[1].IndexOf("\n") + 1).Replace("\t", "");
var device2 = splits[2].Substring(splits[2].IndexOf("\n") + 1).Replace("\t", "");
I've been doing a first aproach and it works, it might help:
I splited the input looking for "/tdevice" and then cleaned everyting before /r/n including the /r/n itself. It did the job and should work with your adb output.
EDIT:
I've refactored my answer to consider #LANimal answer (split using all delimiters) and I tried this code and works. (note the # usage)
static void Main(string[] args)
{
var inputString = #"List of devices attached\r\n9887bc314\tdevice\r\n12n1n2nj1jn2\tdevice\r\n\r\n";
string[] delimiters = {
#"\r\n",
#"\tdevice",
#"List of devices attached"
};
var chunks = inputString.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
string result = "[";
for (int i = 0; i < chunks.Length; i++)
{
result += chunks[i] + ",";
}
result = result.Remove(result.Length - 1);
result += "]";
Console.WriteLine(result);
Console.ReadLine();
}
I hope it helps,
Juan
You could do:
string str = #"List of devices attached\r\n9887bc314\tdevice\r\n12n1n2nj1jn2\tdevice\r\n\r\n";
string[] lines = str.Split(new[] { #"\r\n" }, StringSplitOptions.None);
string firstDevice = lines[1].Replace(#"\tdevice", "");
string secondDevice = lines[2].Replace(#"\tdevice", "");
if i have a string like
string hello="HelloworldHellofriendsHelloPeople";
i would like to store this in a string like this
Helloworld
Hellofriends
HelloPeople
It has to change the line when it finds the string "hello"
thanks
string hello = "HelloworldHellofriendsHelloPeople";
var a = hello.Split(new string[] { "Hello"}, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in a)
Console.WriteLine("Hello" + s);
var result = hello.Split(new[] { "Hello" },
StringSplitOptions.RemoveEmptyEntries)
.Select(s => "Hello" + s);
You can use this regex
(?=Hello)
and then split the string using regex's split method!
Your code would be:
String matchpattern = #"(?=Hello)";
Regex re = new Regex(matchpattern);
String[] splitarray = re.Split(sourcestring);
You can use this code - based on string.Replace
var replace = hello.Replace( "Hello", "-Hello" );
var result = replace.Split("-");
You could use string.split to split on the word "Hello", and then append "Hello" back onto the string.
string[] helloArray = string.split("Hello");
foreach(string hello in helloArray)
{
hello = "Hello" + hello;
}
That will give the output you want of
Helloworld
Hellofriends
HelloPeople
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 have a string like '/Test1/Test2', and i need to take Test2 separated from the same. How can i do that in c# ?
Try this:
string toSplit= "/Test1/Test2";
toSplit.Split('/');
or
toSplit.Split(new [] {'/'}, System.StringSplitOptions.RemoveEmptyEntries);
to split, the latter will remove the empty string.
Adding .Last() will get you the last item.
e.g.
toSplit.Split('/').Last();
Use .Split().
string foo = "/Test1/Test2";
string extractedString = foo.Split('/').Last(); // Result Test2
This site have quite a few examples of splitting strings in C#. It's worth a read.
Using .Split and a little bit of LINQ, you could do the following
string str = "/Test1/Test2";
string desiredValue = str.Split('/').Last();
Otherwise you could do
string str = "/Test1/Test2";
string desiredValue = str;
if(str.Contains("/"))
desiredValue = str.Substring(str.LastIndexOf("/") + 1);
Thanks Binary Worrier, forgot that you'd want to drop the '/', darn fenceposts
string[] arr = string1.split('/');
string result = arr[arr.length - 1];
string [] split = words.Split('/');
This will give you an array split that will contain "", "Test1" and "Test2".
If you just want the Test2 portion, try this:
string fullTest = "/Test1/Test2";
string test2 = test.Split('/').ElementAt(1); //This will grab the second element.
string inputString = "/Test1/Test2";
string[] stringSeparators = new string[] { "/Test1/"};
string[] result;
result = inputString.Split(stringSeparators,
StringSplitOptions.RemoveEmptyEntries);
foreach (string s in result)
{
Console.Write("{0}",s);
}
OUTPUT : Test2
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);