var list = string.Join(",", item.Split(new string[] { "2|" }, StringSplitOptions.RemoveEmptyEntries));
I use above code and it works well.
I want to split like below,
2| or 3| or 4| or 5| or 6| or 7|
If value is
2|222 program will continue to working.
if value is
5|232 program will continue to working.
i want program working without exception.
How can i use "or" according to my scerenio ?
I guess you have some particular set of format to care about, not all has the format \d|, if so you could also try using Regex, but for the simplicity enough, we can always use string.Split with an array of string passed in to split the string by any one of the array elements, something like this:
var list = string.Join(",", item.Split(new string[] { "2|", "3|", "4|", "5|", "6|", "7|" },
StringSplitOptions.RemoveEmptyEntries));
For regex version, you should use the Regex.Replace like this:
var list = Regex.Replace(item, "(?<![2-7][|])[2-7][|](?![2-7][|])",",");
You should use Regex.Split with pattern like this [2-7]+|
replace [2-7] with \d if you need all digits.
var regex = new Regex(#"\d\|");
var splitted = regex.Split(yourInput);
Related
I'm currently trying to do string comparisons - I know it's not best to do so, but I need to for this solution.
If I were parsing a line such as: Raiden: HelloWorld, I would want to extract the separate strings Raiden and HelloWorld for further use.
I currently do achieve this by performing the following:
var list = channelMessage.Split(' ', ':', '\n', '\0');
However, when printing the result and length of each item in list the HelloWorld string's length is incorrect.
Output:
Raiden | length: 6
HelloWorld | length: 11
HelloWorld's length should be 10, not 11. I'm assuming there's null characters somewhere in the line, but cannot figure out how to remove them all.
Sidenote: If I remember correctly, c#'s strings are arrays, and the last character of the array is a '\0' but I tried removing it (as seen above)
Is my assumption correct, and how can I correctly get HelloWorld's length to 10?
you're supposed to use Trim() to remove whitespaces around a string
See : https://learn.microsoft.com/en-us/dotnet/api/system.string.trim?view=netframework-4.8
This would result in
var list = channelMessage.Split(':').Select(s => s.Trim());
I'm also using the Select() from linq. This code would be similar to:
var list = channelMessage.Split(':');
var list2 = new List<string>();
foreach(string s in list)
list2.add(s.trim());
The problem is that when you split Raiden: HelloWorld on : you end up with this:
Raiden
_HelloWorld
Where the _ represents an empty whitespace.
Here's one possible solution. When I run this console app:
static void Main(string[] args)
{
var test = "Raiden: HelloWorld";
List<string> split = test.Split(':').Select(t => t.Trim()).ToList();
Console.WriteLine(split[0].Length);
Console.WriteLine(split[1].Length);
Console.ReadLine();
}
I get:
6
10
Split will likely give you trouble in this solution since it doesn't handle a huge range of input.
Something like a regex might be better because of the char ranges, something like \W+ would match any non-world sequence.
example:
Regex.Split("asdadaSD asdsad asdsad \n asdasdsd", "\\W+")
I'm trying to split a string on every parenthese into an array and keep all text in C#, get everything in the parenthese.
Example: "hmmmmmmmm (asdfhqwe)asasd"
Should become: "hmmmmmmmm", "(asdfhqwe)" and "asasd".
My current setup is only able to take everything inside the parentheses and discards the rest.
var output = input.Split('(', ')').Where((item, index) => index % 2 != 0).ToList();
How would i go forward to do such thing (disregarding my current code) ?
Use regrx split with positive look-ahead and look-behind and an optional space; then filter out empty strings.
var tokens = Regex
.Split(str, #"(?<=[)])\s*|\s*(?=[(])")
.Where(s => s != string.Empty)
.ToList();
Demo.
Oky so I do not know what the real string will look like in your application, but based on the provided string this will be my hack of a solution:
string sample = "hmmmmmmmm (asdfhqwe)asasd";
var result = sample.Replace("(", ",(").Replace(")", "),").Split(',');
So i replaced where the split should be with a comma, but you can use any other char that might never occur in your string, Say like the '~' could also work.
But not knowing all the required functionality, this would work for above scenario.
Try this:
string[] subString = myString.Split(new char[] { '(', ')' });
I have a little problem that I don't know how to call it like, so I will do my best to explain you that.
String text = "Random text over here boyz, I dunno what to do";
I want to take by split only over here boyz for example, I want to let split the word text and the word , and it will show me the whole text that in thoose 2 strings. Any ideas?
Thank you,
Sagi.
From your comments I get that from this string:
foo bar id="baz" qux
You want to obtain the value baz, because it is in the id="{text}" pattern.
For that you can use a regular expression:
string result = Regex.Match(text, "id=\"(.*?)\"").Groups[1].Value;
Note that this will match any character. Also note that this will yield false positives, like fooid="bar", and that this won't match unquoted values.
So all in all, for parsing HTML, you should not use regular expressions. Try HtmlAgilityPack and an XPath expression.
There is a Split overload that can receive multiple string seperators:
var rrr = text.Split(new string[] { ",", "text" }, StringSplitOptions.None);
If you would like to extract only the text between these two strings using regex you can do something like this:
var pattern = #"text(.*),";
var a = new Regex(pattern).Match(text);
var result = a.Groups[1];
You can use Regex class:
https://msdn.microsoft.com/pl-pl/library/ze12yx1d%28v=vs.110%29.aspx
But first of all (as it was said) you need to clarify for yourself how you will identify string that you want.
in first case you can use
string stringResult;
if (text.Contains("over here boyz"))
stringResult = string.Empty;
else
stringResult = "over here boyz";
but the second case can solve by this code
String text = "Random text over here boyz, I dunno what to do";
//Second dream without whitespace
var result = Regex.Split(text, " *text *| *, *");
foreach (var x in result)
{
Console.WriteLine(x);
}
//Second dream with whitespace
result = Regex.Split(text, "text|,");
foreach (var x in result)
{
Console.WriteLine(x);
}
You can train to write Regex with this tool http://www.regexbuddy.com/ or http://www.regexr.com/
In C#, I have a string comes from a file in this format:
Type="Data"><Path.Style><Style
or maybe
Type="Program"><Rectangle.Style><Style
,etc. Now I want to only extract the Data or Program part of the Type element. For that, I used the following code:
string output;
var pair = inputKeyValue.Split('=');
if (pair[0] == "Type")
{
output = pair[1].Trim('"');
}
But it gives me this result:
output=Data><Path.Style><Style
What I want is:
output=Data
How to do that?
This code example takes an input string, splits by double quotes, and takes only the first 2 items, then joins them together to create your final string.
string input = "Type=\"Data\"><Path.Style><Style";
var parts = input
.Split('"')
.Take(2);
string output = string.Join("", parts); //note: .net 4 or higher
This will make output have the value:
Type=Data
If you only want output to be "Data", then do
var parts = input
.Split('"')
.Skip(1)
.Take(1);
or
var output = input
.Split('"')[1];
What you can do is use a very simple regular express to parse out the bits that you want, in your case you want something that looks like this and then grab the two groups that interest you:
(Type)="(\w+)"
Which would return in groups 1 and 2 the values Type and the non-space characters contained between the double-quotes.
Instead of doing many split, why don't you just use Regex :
output = Regex.Match(pair[1].Trim('"'), "\"(\w*)\"").Value;
Maybe I missed something, but what about this:
var str = "Type=\"Program\"><Rectangle.Style><Style";
var splitted = str.Split('"');
var type = splitted[1]; // IE Data or Progam
But you will need some error handling as well.
How about a regex?
var regex = new Regex("(?<=^Type=\").*?(?=\")");
var output = regex.Match(input).Value;
Explaination of regex
(?<=^Type=\") This a prefix match. Its not included in the result but will only match
if the string starts with Type="
.*? Non greedy match. Match as many characters as you can until
(?=\") This is a suffix match. It's not included in the result but will only match if the next character is "
Given your specified format:
Type="Program"><Rectangle.Style><Style
It seems logical to me to include the quote mark (") when splitting the strings... then you just have to detect the end quote mark and subtract the contents. You can use LinQ to do this:
string code = "Type=\"Program\"><Rectangle.Style><Style";
string[] parts = code.Split(new string[] { "=\"" }, StringSplitOptions.None);
string[] wantedParts = parts.Where(p => p.Contains("\"")).
Select(p => p.Substring(0, p.IndexOf("\""))).ToArray();
I have a program to compare text files. Takes in 2 files spits out 1. The input files have lines of data similar to this
tv_rocscores_DeDeP005M3TSub.csv FMR: 0.0009 FNMR: 0.023809524 SCORE: -4 Conformity: True
tv_..............P006............................................................
tv_..............P007............................................................
etc etc.
For my initial purposes, I was splitting the lines based on spaces, to get the respective values. However, for the first field, tv_rocscores_DeDeP005M3TSbu.csv i only need P005 and not the rest. I cannot opt for position number as well, because the position of P005 in the phrase is not the same for every file.
Any advise on how i split this so that i can identify my first field with only P005??
Your question is a bit unclear. If you're looking for pattern, say "P + three digits", e.g. "P005" you can use regular expressions:
String str = #"tv_rocscores_DeDeP005M3TSub.csv FMR: 0.0009 FNMR: 0.023809524 SCORE: -4 Conformity: True";
String[] parts = str.Split(' ');
parts[0] = Regex.Match(parts[0], #"P\d\d\d").Value; // <- "P005"
To extract the desired part I would try something like this:
var parts = str.Split(' ');
var number = Regex.Match(parts[0], ".*?(?<num>P\d+).*?").Groups["num"].Value;
Or if you know its only three digits you could change the regular expression to .*?(?<num>P\d{3}).*?
Hope that solves your problem :)
How about just checking if the first field contains P005?
bool hasP005 = field1.Contains("P005");
Your question isn't clear. Can't you just replace the first field with your string?
string[] parts = str.Split(' ');
parts[0] = "P005";
Are you looking to try field the field that contains that string? if so then you can use some linq
var field = s.Split(' ').Where(x => x.Contains("P005")).ToList()[0];