Suppose I have a string:
var myFilename = "342134_My_Awesome_File.zip
I would like to extract just: My_Awesome_File.zip.
I am attempting to do this with the following:
var removedPrependedTieBreaker = myFilename.Split('_').Skip(1);
However, that will yield an IEnumerable<string>.
How do we do a split on underscore, and get the rest of the contents?
You just need to use a different overload of split that takes the count:
var parts = myFilename.Split(new[] { '_' }, 2, StringSplitOptions.None);
var interestingData = parts[1];
You could recombine the remaining parts with String.Join. But maybe it would be better to avoid splitting and just take a substring:
myFilename.Substring(myFilename.IndexOf('_') + 1)
While I like Joe Sewell's answer, I just thought I'd give you another way (besides string.Join, as mentioned in the question comments) to make your own approach work:
var removedPrependedTieBreaker = myFilename.Split('_').Skip(1).Aggregate((current, next) => $"{current}_{next}");
This will take the IEnumerable<string> that you made and recompose the pieces as a string with _ as a delimiter.
Related
Okay, lets say I have a string:
string text = "one|two|three";
If I do string[] texts = text.Split('|'); I will end up with a string array of three objects. However, this isn't what I want. What I actually want is to split the string only once... so the two arrays I could would be this:
one
two|three
Additionally, is there a way to do a single split with the last occurrence in a string? So I get:
one|two
three
As well, is there a way to split by a string, instead of a character? So I could do Split("||")
Split method takes a count as parameter, you can pass 2 in that position, which basically says that you're interested in only 2 elements maximum. You'll get the expected result.
For second question: There is no built in way AFAIK. You may need to implement it yourself by splitting all and joining first and second back.
C#'s String.Split() can take a second argument that can define the number of elements to return:
string[] texts = text.Split(new char[] { '|' }, 2);
For your first scenario, you can pass a parameter of how many strings to split into.
var text = "one|two|three";
var result = text.Split(new char[] { '|' }, 2);
Your second scenario requires a little more magic.
var text = "one|two|three";
var list = text.Split('|');
var result = new string[] { string.Join("|", list, 0, list.Length - 1), list[list.Length - 1] };
Code has not been verified to check results before using.
Well, I took it as a challenge to do your second one in one line. The result is... not pretty, mostly because it's surprisingly difficult to reverse a string and keep it as a string.
string text = "one|two|three";
var result = new String(text.Reverse().ToArray()).Split(new char[] {'|'}, 2).Reverse().Select(c => new String(c.Reverse().ToArray()));
Basically, you reverse it, then follow the same procedure as the first one, then reverse each individual one, as well as the resulting array.
You can simply do like this as well...
//To split at first occurence of '|'
if(text.Containts('|')){
beginning = text.subString(0,text.IndexOf('|'));
ending = text.subString(text.IndexOf('|');
}
//To split at last occurence of '|'
if(text.Contains('|')){
beginning = text.subString(0,text.LastIndexOf('|'));
ending = text.subString(text.LastIndexOf('|');
}
Second question was fun. I solved it this way:
string text = "one|two|three";
var result =
new []
{
string.Concat(text.ToCharArray().TakeWhile((c, i) => i <= text.LastIndexOf("|"))),
string.Concat(text.ToCharArray().SkipWhile((c, i) => i <= text.LastIndexOf("|")))
};
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];
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);
I'm having the following string as an example:
<tr class="row_odd"><td>08:00</td><td>08:10</td><td>TEST1</td></tr><tr class="row_even"><td>08:10</td><td>08:15</td><td>TEST2</td></tr><tr class="row_odd"><td>08:15</td><td>08:20</td><td>TEST3</td></tr><tr class="row_even"><td>08:20</td><td>08:25</td><td>TEST4</td></tr><tr class="row_odd"><td>08:25</td><td>08:30</td><td>TEST5</td></tr>
I need to have to have the output as a onedimensional Array.
Like 11111=myArray(0) , 22222=myArray(1) , 33333=myArray(2) ,......
I have already tried the myString.replace, but it seems I can only replace a single Char that way. So I need to use expressions and a for loop for filling the array, but since this is my first c# project, that is a bridge too far for me.
Thanks,
It seems like you want to use a Regex search pattern. Then return the matches (using a named group) into an array.
var regex = new Regex("act=\?(<?Id>\d+)");
regex.Matches(input).Cast<Match>()
.Select(m => m.Groups["Id"])
.Where(g => g.Success)
.Select(g => Int32.Parse(g.Value))
.ToArray();
(PS. I'm not positive about the regex pattern - you should check into it yourself).
Several ways you could do this. A couple are:
a) Use a regular expression to look for what you want in the string. Used a named group so you can access the matches directly
http://www.regular-expressions.info/dotnet.html
b) Split the expression at the location where your substrings are (e.g. split on "act="). You'll have to do a bit more parsing to get what you want, but that won't be to difficult since it will be at the beginning of the split string (and your have other srings that dont have your substring in them)
Use a combination of IndexOf and Substring... something like this would work (not sure how much your string varies). This will probably be quicker than any Regex you come up with. Although, looking at the length of your string, it might not really be an issue.
public static List<string> GetList(string data)
{
data = data.Replace("\"", ""); // get rid of annoying "'s
string[] S = data.Split(new string[] { "act=" }, StringSplitOptions.None);
var results = new List<string>();
foreach (string s in S)
{
if (!s.Contains("<tr"))
{
string output = s.Substring(0, s.IndexOf(">"));
results.Add(output);
}
}
return results;
}
Split your string using HTML tags like "<tr>","</tr>","<td>","</td>", "<a>","</a>" with strinng-variable.split() function. This gives you list of array.
Split html row into string array