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);
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", "");
I do want to get all the text before the first \r\n\ symbol. For example,
lalal lalal laldlasdaa daslda\r\n\Prefersaasda reanreainrea
I would need: lalal lalal laldlasdaa daslda. The above string can be empty, but it also may not contain any '\r\n\' symbol." How can I achieve this?
You can use IndexOf to get the position, then use Substring to get the first part:
string s = "lalal lalal laldlasdaa daslda\r\n Prefersaasda reanreainrea";
int positionOfNewLine = s.IndexOf("\r\n");
if (positionOfNewLine >= 0)
{
string partBefore = s.Substring(0, positionOfNewLine);
}
Hope that this is what you are looking for:
string inputStr = "lalal lalal laldlasdaa daslda\r\nPrefersaasda reanreainrea";
int newLineIndex = inputStr.IndexOf("\r\n");
if(newLineIndex != -1)
{
string outPutStr = inputStr.Substring(0, newLineIndex );
// Continue
}
else
{
// Display message no new line character
}
Checkout an example here
You can use Split like this
string mystring = #"lalal lalal laldlasdaa daslda\r\n\Prefersaasda reanreainrea";
string mylines = mystring.Split(new string[] { #"\r\n" }, StringSplitOptions.None)[0];
Whereas if the string is like
string mystring = "lalal lalal laldlasdaa daslda\r\n Prefersaasda reanreainrea";
string mylines = mystring.Split(new string[] { Environment.NewLine }, StringSplitOptions.None)[0];
Environment.NewLine will also work with it. The with \r\n\P is making that string an invalid string thus \r\n P makes it a new line
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);
}
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 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