Im making the switch from Visual Basic to c#. In my project i had it split a string a few times and one of them being by a word. Here was my VB code:
Dim Item As String() = New String() {TXTItem1.Text + "-"}
If Foods.Contains(TXTItem1.Text) Then
Dim Substring As String = Foods.Split(Item, StringSplitOptions.None)(1)
Dim SPValue As String = Substring.Split(vbNewLine)(0)
MsgBox(SPValue)
Now here is my c# code:
string[] Item = new string[] {TXTSearchItem.Text + "-"};
if (Foods.Contains(TXTSearchItem.Text))
{
string Substring = Foods.Split(Item, StringSplitOptions.None)[1];
MessageBox.Show(Substring);
For some reason i cannot split it again and the MessageBox isnt even showing. Any help? Thanks!
What is the Foods object? Is that an array or a String?
If I'm not mistaken, I believe that the Split extension method on a string would create an Array of type String instead of a String.
So that MessageBox.Show method may only be showing the String array as an object or not compiling correctly since Substring is declared as a String instead of an Array.
*Edit.
Would this be what you are looking for? This would only show the information that occurs after the Item String Array in the MessageBox.Show method.
string[] Item = new string[] { TXTSearchItem.Text + "-" };
if (Foods.ToLower().Contains(TXTSearchItem.Text.ToLower()))
{
string Substring = Foods.Split(Item, StringSplitOptions.None)[1];
MessageBox.Show(Substring);
}
Rather than using string.Split, how about you just search for what you are looking for; it's probably simpler:
const string foods = "Banana-21\r\nEggs-123\r\n";
const string item = "banana";
var startIndex = foods.IndexOf(item + "-", StringComparison.CurrentCultureIgnoreCase);
var dashIndex = foods.IndexOf("-", startIndex);
var endIndex = foods.IndexOf("\r\n", startIndex);
var foodName = foods.Substring(startIndex, dashIndex - startIndex);
var footCount = foods.Substring(dashIndex + 1, endIndex - dashIndex - 1);
MessageBox.Show($"[{foodName}]: {footCount}");
But, if you really want to use string.Split, here's an example (doing something similar):
const string foods = "Banana-21\r\nEggs-123\r\nCandy-1\r\n";
var foodItems = new Dictionary<string, int>();
var records = foods.Split(new[]{ "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var foodRecord in records)
{
var parts = foodRecord.Split(new[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
if (!int.TryParse(parts[1], out var foodCount))
{
foodCount = 0;
}
foodItems.Add(parts[0], foodCount);
}
After that code runs, you end up with foodItems containing:
[0] {[Banana, 21]}
[1] {[Eggs, 123]}
[2] {[Candy, 1]}
For what it's worth, it makes a lot more sense (to me, at least) to split on the line ends first, then split each line (or just the line you are interested in).
Related
I have a Powershell output to re-format, because formatting gets lost in my StandardOutput.ReadToEnd().There are several blanks to be removed in a line and I want to get the output formatted readable.
Current output in my messageBox looks like
Microsoft.MicrosoftJigsaw All
Microsoft.MicrosoftMahjong All
What I want is
Microsoft.MicrosoftJigsaw All
Microsoft.MicrosoftMahjong All
What am I doing wrong?
My C# knowledge still is basic level only
I found this question here, but maybe I don't understand the answer correctly. The solution doesn't work for me.
Padding a string using PadRight method
This is my current code:
string first = "";
string last = "";
int idx = line.LastIndexOf(" ");
if (idx != -1)
{
first = line.Substring(0, idx).Replace(" ","").PadRight(10, '~');
last = line.Substring(idx + 1);
}
MessageBox.Show(first + last);
String.PadLeft() first parameter defines the length of the padded string, not padding symbol count.
Firstly, you can iterate through all you string, split and save.
Secondly, you should get the longest string length.
Finally, you can format strings to needed format.
var strings = new []
{
"Microsoft.MicrosoftJigsaw All",
"Microsoft.MicrosoftMahjong All"
};
var keyValuePairs = new List<KeyValuePair<string, string>>();
foreach(var item in strings)
{
var parts = item.Split(new [] {" "}, StringSplitOptions.RemoveEmptyEntries);
keyValuePairs.Add(new KeyValuePair<string, string>(parts[0], parts[1]));
}
var longestStringCharCount = keyValuePairs.Select(kv => kv.Key).Max(k => k.Length);
var minSpaceCount = 5; // min space count between parts of the string
var formattedStrings = keyValuePairs.Select(kv => string.Concat(kv.Key.PadRight(longestStringCharCount + minSpaceCount, ' '), kv.Value));
foreach(var item in formattedStrings)
{
Console.WriteLine(item);
}
Result:
Microsoft.MicrosoftJigsaw All
Microsoft.MicrosoftMahjong All
The PadRight(10 is not enough, it is the size of the complete string.
I would probably go for something like:
string[] lines = new[]
{
"Microsoft.MicrosoftJigsaw All",
"Microsoft.MicrosoftMahjong All"
};
// iterate all (example) lines
foreach (var line in lines)
{
// split the string on spaces and remove empty ones
// (so multiple spaces are ignored)
// ofcourse, you must check if the splitted array has atleast 2 elements.
string[] splitted = line.Split(new Char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
// reformat the string, with padding the first string to a total of 40 chars.
var formatted = splitted[0].PadRight(40, ' ') + splitted[1];
// write to anything as output.
Trace.WriteLine(formatted);
}
Will show:
Microsoft.MicrosoftJigsaw All
Microsoft.MicrosoftMahjong All
So you need to determine the maximum length of the first string.
Assuming the length of second part of your string is 10 but you can change it. Try below piece of code:
Function:
private string PrepareStringAfterPadding(string line, int totalLength)
{
int secondPartLength = 10;
int lastIndexOfSpace = line.LastIndexOf(" ");
string firstPart = line.Substring(0, lastIndexOfSpace + 1).Trim().PadRight(totalLength - secondPartLength);
string secondPart = line.Substring(lastIndexOfSpace + 1).Trim().PadLeft(secondPartLength);
return firstPart + secondPart;
}
Calling:
string line1String = PrepareStringAfterPadding("Microsoft.MicrosoftJigsaw All", 40);
string line2String = PrepareStringAfterPadding("Microsoft.MicrosoftMahjong All", 40);
Result:
Microsoft.MicrosoftJigsaw All
Microsoft.MicrosoftMahjong All
Note:
Code is given for demo purpose please customize the totalLength and secondPartLength and calling of the function as per your requirement.
I'm pulling a string from a MySQL database containing all ID's from friends in this format:
5+6+12+33+1+9+
Now, whenever i have to add a friend it's simple, I just take the the string and add whatever ID and a "+". My problem lies with separating all the ID's and putting it in an array. I'm currently using this method
string InputString = "5+6+12+33+1+9+";
string CurrentID = string.Empty;
List<string> AllIDs = new List<string>();
for (int i = 0; i < InputString.Length; i++)
{
if (InputString.Substring(i,1) != "+")
{
CurrentID += InputString.Substring(i, 1);
}
else
{
AllIDs.Add(CurrentID);
CurrentID = string.Empty;
}
}
string[] SeparatedIDs = AllIDs.ToArray();
Even though this does work it just seems overly complicated for what i'm trying to do.
Is there an easier way to split strings or cleaner ?
Try this:-
var result = InputString.Split(new char[] { '+' });
You can use other overloads of Split
as well if you want to remove empty spaces.
You should to use the Split method with StringSplitOptions.RemoveEmptyEntries. RemoveEmptyEntries helps you to avoid empty string as the last array element.
Example:
char[] delimeters = new[] { '+' };
string InputString = "5+6+12+33+1+9+";
string[] SeparatedIDs = InputString.Split(delimeters,
StringSplitOptions.RemoveEmptyEntries);
foreach (string SeparatedID in SeparatedIDs)
Console.WriteLine(SeparatedID);
string[] IDs = InputString.Split('+'); will split your InputString into an array of strings using the + character
var result = InputString.Split('+', StringSplitOptions.RemoveEmptyEntries);
extra options needed since there is a trailing +
I'm having a bit of an issue. Lets say I have 2 text boxes, one on the left with this content:
Win
Lose
Hello
Goodbye
And one on the right, with this information:
One
Two
Three
Four
Now, on button press, I want to combine these two text boxes with colon delimitation, so it would output like this:
Win:One
Lose:Two
Hello:Three
Goodbye:Four
Any idea how I can accomplish this? Nothing I have tried thus far has worked. This is my current code, sorry. I'm not trying to have you do my work for me, I'm just rather confused:
string path = Directory.GetCurrentDirectory() + #"\Randomized_List.txt";
string s = "";
StringBuilder sb = new StringBuilder();
StreamReader sr1 = new StreamReader("Randomized_UserList.txt");
string line = sr1.ReadLine();
while ((s = line) != null)
{
var lineOutput = line+":";
Console.WriteLine(lineOutput);
sb.Append(lineOutput);
}
sr1.Close();
Console.WriteLine();
StreamWriter sw1 = File.AppendText(path);
sw1.Write(sb);
sw1.Close();
Here's a different approach that might work for you.
You can generate a couple string arrays by splitting on the new line character.
var tb1 = textBox1.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
var tb2 = textBox2.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
And then use LINQ's Zip() method to combine them into a new list. The first element in each list is combined, then the second elements in each, and so on...
var combined = tb1.Zip(tb2, (s1, s2) => string.Format("{0}:{1}", s1, s2));
In order for this to work, both TextBoxes must have the same number of lines. If they differ, then Zip won't work.
The code below demonstrates one way of splitting strings and then concatenating them. I misunderstood the question at first. :)
string left = string.Format("Win{0}Lose{0}Hello{0}Goodbye", Environment.NewLine);
string right = string.Format("One{0}Two{0}Three{0}Four", Environment.NewLine);
string[] leftSplit = left.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
string[] rightSplit = right.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
string output = "";
if (leftSplit.Length == rightSplit.Length)
{
for (int i = 0; i < leftSplit.Length; i++)
{
output += leftSplit[i] + ":" + rightSplit[i] + Environment.NewLine;
}
}
Well if this was a winforms app you could take advantage of the Lines property to do the following.
var tb1 = this.textBox1.Lines.Select((line, index) => new { Line = line, Index = index });
var tb2 = this.textBox2.Lines.Select((line, index) => new { Line = line, Index = index });
var q = from t1 in tb1
join t2 in tb2 on t1.Index equals t2.Index
select string.Format("{0}:{1}", t1.Line, t2.Line);
this.textBox3.Lines = q.ToArray();
textbox1.Text.Split("\n").Zip(texbox2.Text.Split("\n"),(s1,s2)=>s1+":"+s2).Aggregate("",(a,s)=>a+s+"\n")
Split method converts the string on behalf which it was call, to array of strings, by splitting it with character in parameter (new line in this case).
At this movement we have to arrays of lines from textbox1 and textbox2.
Now we use Zip method of any IEnumerable (this is an extension method as the Aggregate method is).
The outcome of Zip method is a IEnumerable that contains elements that are merge from both IEnumerables that we mentioned using function passed in the parameters, in this case it is (s1,s2)=>s1+":"+s2.
At this moment we have some IEnumerable having elements as merged lines from both textboxes. What we need to do now is to merge them into one string with Aggregate function. It a function that construct result starting with first parameter and for each element taking the result of last step and returning new value that is some kind of aggregation of the previous result and current element
You can split a string by linebreaks in the following way:
string[] lines = theString.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
I think you should split the content of both TextBoxes like that, and after that (if the resulting arrrays are of the same size), concatenate the corresponding (the first string from the first array with the first string form the second array, the second string from the first array with the second string from the second array, etc.) strings with a semicolon between them.
For example:
var lines1 = textBox1.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
var lines2 = textBox2.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
string result = String.Empty;
if (lines1.Length == lines2.Length)
{
for(int i=0; i< lines1.Length; ++i)
{
result += lines1[i] + ":" + lines2[i] + Environment.NewLine;
}
}
I have the following string that I need to parse out so I can insert them into a DB. The delimiter is "`":
`020 Some Description `060 A Different Description `100 And Yet Another `
I split the string into an array using this
var responseArray = response.Split('`');
So then each item in the responseArrray[] looks like this: 020 Some Description
How would I get the two different parts out of that array? The 1st part will be either 3 or 4 characters long. 2nd part will be no more then 35 characters long.
Due to some ridiculous strangeness beyond my control there is random amounts of space between the 1st and 2nd part.
Or put the other two answers together, and get something that's more complete:
string[] response = input.Split(`);
foreach (String str in response) {
int splitIndex = str.IndexOf(' ');
string num = str.Substring(0, splitIndex);
string desc = str.Substring(splitIndex);
desc.Trim();
}
so, basically you use the first space as a delimiter to create 2 strings. Then you trim the second one, since trim only applies to leading and trailing spaces, not everything in between.
Edit: this a straight implementation of Brad M's comment.
You can try this solution:
var inputString = "`020 Some Description `060 A Different Description `100 And Yet Another `";
int firstWordLength = 3;
int secondWordMaxLength = 35;
var result =inputString.Split('`')
.SelectMany(x => new[]
{
new String(x.Take(firstWordLength).ToArray()).Trim(),
new String(x.Skip(firstWordLength).Take(secondWordMaxLength).ToArray()).Trim()
});
Here is the result in LINQPad:
Update: My first solution has some problems because the use of Trim after Take.Here is another approach with an extension method:
public static class Extensions
{
public static IEnumerable<string> GetWords(this string source,int firstWordLengt,int secondWordLenght)
{
List<string> words = new List<string>();
foreach (var word in source.Split(new[] {'`'}, StringSplitOptions.RemoveEmptyEntries))
{
var parts = word.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
words.Add(new string(parts[0].Take(firstWordLengt).ToArray()));
words.Add(new string(string.Join(" ",parts.Skip(1)).Take(secondWordLenght).ToArray()));
}
return words;
}
}
And here is the test result:
Try this
string response = "020 Some Description060 A Different Description 100 And Yet Another";
var responseArray = response.Split('`');
string[] splitArray = {};
string result = "";
foreach (string it in responseArray)
{
splitArray = it.Split(' ');
foreach (string ot in splitArray)
{
if (!string.IsNullOrWhiteSpace(ot))
result += "-" + ot.Trim();
}
}
splitArray = result.Substring(1).Split('-');
string[] entries = input.Split('`');
foreach (string s in entries)
GetStringParts(s);
IEnumerable<String> GetStringParts(String input)
{
foreach (string s in input.Split(' ')
yield return s.Trim();
}
Trim only removes leading/trailing whitespace per MSDN, so spaces in the description won't hurt you.
If the first part is an integer
And you need to account for some empty
For me the first pass was empty
public void parse()
{
string s = #"`020 Some Description `060 A Different Description `100 And Yet Another `";
Int32 first;
String second;
if (s.Contains('`'))
{
foreach (string firstSecond in s.Split('`'))
{
System.Diagnostics.Debug.WriteLine(firstSecond);
if (!string.IsNullOrEmpty(firstSecond))
{
firstSecond.TrimStart();
Int32 firstSpace = firstSecond.IndexOf(' ');
if (firstSpace > 0)
{
System.Diagnostics.Debug.WriteLine("'" + firstSecond.Substring(0, firstSpace) + "'");
if (Int32.TryParse(firstSecond.Substring(0, firstSpace), out first))
{
System.Diagnostics.Debug.WriteLine("'" + firstSecond.Substring(firstSpace-1) + "'");
second = firstSecond.Substring(firstSpace).Trim();
}
}
}
}
}
}
You can get the first part by finding the first space and make a substring. The second is also a Substring. Try something like this.
foreach(string st in response)
{
int index = response.IndexOf(' ');
string firstPart = response.Substring(0, index);
//string secondPart = response.Substring(response.Lenght-35);
//better use this
string secondPart = response.Substring(index);
secondPart.Trim();
}
I have several delimiters. For example {del1, del2, del3 }.
Suppose I have text : Text1 del1 text2 del2 text3 del3
I want to split string in such way:
Text1 del1
text2 del2
text3 del3
I need to get array of strings, when every element of array is texti deli.
How can I do this in C# ?
String.Split allows multiple split-delimeters. I don't know if that fits your question though.
Example :
String text = "Test;Test1:Test2#Test3";
var split = text.Split(';', ':', '#');
//split contains an array of "Test", "Test1", "Test2", "Test3"
Edit: you can use a regex to keep the delimeters.
String text = "Test;Test1:Test2#Test3";
var split = Regex.Split(text, #"(?<=[;:#])");
// contains "Test;", "Test1:", "Test2#","Test3"
This should do the trick:
const string input = "text1-text2;text3-text4-text5;text6--";
const string matcher= "(-|;)";
string[] substrings = Regex.Split(input, matcher);
StringBuilder builder = new StringBuilder();
foreach (string entry in substrings)
{
builder.Append(entry);
}
Console.Out.WriteLine(builder.ToString());
note that you will receive empty strings in your substring array for the matches for the two '-';s at the end, you can choose to ignore or do what you like with those values.
You could use a regex. For a string like this "text1;text2|text3^" you could use this:
(.*;|.*\||.*\^)
Just add more alternative pattens for each delimiter.
If you want to keep the delimiter when splitting the string you can use the following:
string[] delimiters = { "del1", "del2", "del3" };
string input = "text1del1text2del2text3del3";
string[] parts = input.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
for(int index = 0; index < parts.Length; index++)
{
string part = parts[index];
string temp = input.Substring(input.IndexOf(part) + part.Length);
foreach (string delimter in delimiters)
{
if ( temp.IndexOf(delimter) == 0)
{
parts[index] += delimter;
break;
}
}
}
parts will then be:
[0] "text1del1"
[1] "text2del2"
[2] "text3del3"
As #Matt Burland suggested, use Regex
List<string> values = new List<string>();
string s = "abc123;def456-hijk,";
Regex r = new Regex(#"(.*;|.*-|.*,)");
foreach(Match m in r.Matches(s))
values.Add(m.Value);