Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
How can I append all the string arr into one arr within a for loop?
string[] finalArray = new string[5];
for (int i = 0; i < finalArray.Length; i++)
{
string[] array1 = GenRandArray1();
string[] array2 = GenRandArray2();
string[] array3 = GenRandArray3();
string[] array4 = GenRandArray4();
// I want to combine all the string array to my finalArray.
finalArray[i] = array1 + array2 + array3 + array4
}
Since I make loop 5 times and store in finalArray[i], so the sample output will be something like this.
"12345","17896","685","984","063","991","6","9","3","6","68","20","69","52"
"43256","24356","765","345","347","983","2","1","0","5","90","34","12","76"
"76324","98754","542","625","267","865","3","2","1","8","23","43","76","86"
"00982","16864","537","847","249","136","1","0","9","3","65","80","23","17"
"12569","98754","658","345","646","999","5","9","6","3","94","63","15","47"
Do you mean concat all the arrays or a different algorithm? If you just want to concat all the arrays it just like below.
string[] finalArray = new string[5];
string[] array1 = new[] { "1", "2", "4" };
string[] array2 = new[] { "9", "3", "6" };
string[] array3 = new[] { "4", "0", "0" };
string[] array4 = new[] { "7", "8", "2" };
finalArray = finalArray.Concat(array1).Concat(array2).Concat(array3).Concat(array4).ToArray();
From your question it is not clear if you really want to concat your arrays five times, but let's assume that that is the case in your simplified code. In addition, as you remarked, you will be returning a list of arrays
Then:
List<string[]> finalArrayList = new List<string[]>();
for(int i=0; i<5; i++)
{
string[] array1 = new[] { "1", "2", "4" };
string[] array2 = new[] { "9", "3", "6" };
string[] array3 = new[] { "4", "0", "0" };
string[] array4 = new[] { "7", "8", "2" };
List<string> ConcatList = new List<string>(array1);
ConcatList.AddRange(array2);
ConcatList.AddRange(array3);
ConcatList.AddRange(array4);
finalArrayList.Add(ConcatList.ToArray());
}
If I have input a string is it possible to check that the first letter does start with an input from a list of strings:
var dir = "FOLDERNAME";
var list = new List<string>() { "a", "b", "c", "d", "e", "f", "g", "s",
"t", "u", "v", "w", "z", "y", "z",
"1", "2", "3", "4", "5", "6", "7", "8", "9"};
if (!dir.ToLower().!StartsWith :MagicLinq: list) { Do Stuff; }
Or do I have to go the regex route?
One approach to consider:
var lower = new string(dir?.FirstOrDefault() ?? new char(), 1);
if (list.Contains(lower) {
The first line gets the first character from the string, and handles empty and null strings (by getting the null char), and then loads that char into a string.
You could simplify that if the List<string> was a List<char or HashSet<char> instead. Then you could remove the new string part.
You can check if the first letter is in the list:
if (list.Contains(""+dir[0])){}
If this check will be done many times using a HashSet<> would be more performant than a linear search on a List<>. Even if performance is not a concern a HashSet<> might better represent the data and how its used since each element is unique and ordering doesn't matter.
Here I'm using a HashSet<> of chars...
var dir = "FOLDERNAME";
var restrictedChars = new HashSet<char>(
new[] {
'A', 'B', 'C', 'D', 'E', 'F', 'G',
'S', 'T', 'U', 'V', 'W', 'Z', 'Y', 'Z',
'1', '2', '3', '4', '5', '6', '7', '8', '9'
}
);
if (!restrictedChars.Contains(dir[0]))
{
// Do stuff...
}
Note that I changed the restricted characters from lowercase to uppercase since char.ToUpper()/char.ToUpperInvariant() are the recommended way to normalize case for case-insensitive comparisons. You could then perform the check in a case-insensitive manner like this...
if (!restrictedChars.Contains(char.ToUpperInvariant(dir[0])))
{
// Do stuff...
}
Alternatively, you could use a HashSet<> of strings and pass in a case-insensitive StringComparer instance...
var dir = "FOLDERNAME";
var restrictedChars = new HashSet<string>(
new[] {
"a", "b", "c", "d", "e", "f", "g",
"s", "t", "u", "v", "w", "z", "y", "z",
"1", "2", "3", "4", "5", "6", "7", "8", "9"
},
StringComparer.OrdinalIgnoreCase
);
// Get a string from Substring() instead of dir[0].ToString()
if (!restrictedChars.Contains(dir.Substring(0, 1)))
{
// Do stuff...
}
With this approach the case of the restricted character strings fed into the HashSet<> doesn't matter.
This is how i'd do it
String dir = "FOLDERNAME";
var MyList = new List<string>() { "a", "b", "c", "d", "e", "f", "g", "s",
"t", "u", "v", "w", "z", "y", "z",
"1", "2", "3", "4", "5", "6", "7", "8", "9"};
Console.WriteLine((MyList.IndexOf(dir.Substring(0, 1).ToLower()) != -1) ? "True" : "False");
Console.ReadLine();
I need to put the data from this table into an array and then make the array print as a formatted table in the console.
Here's the table that I got my data from http://puu.sh/oqV8f/7d982f2665.jpg ; I just need to make the array output rows and columns instead of a list.
I have this so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Zumba1
{
class Zumba
{
static void Main(string[] args)
{ //Recreated the data in the table for the zumba section, added each row, and each column.
string[,] schedule = new string [8, 6] { { "1:00", "3:00", "5:00", "7:00", "TOTAL", "", },
{"Monday", "12", "10", "17", "22", "244", },
{"Tuesday", "11", "13", "17", "22", "252",},
{"Wednesday", "12", "10", "22", "22", "264",},
{"Thursday", "9", "14", "17", "22", "248",},
{"Friday", "12", "10", "21", "12", "220",},
{"Saturday", "12", "10", "5", "10", "148"},
{" ", " ", " ", " ", " ","1376",}};
foreach (string i in schedule)
{
Console.WriteLine(i.ToString());
}
Console.ReadKey();
}
}
}
Any Ideas?
Foreach on a [,] array gives you all elements as a list, as you noticed. In this case you need to output as follow:
for (int x0 = 0; x0 < schedule.GetLength(0); x0++)
{
for (int x1 = 0; x1 < schedule.GetLength(1); x1++)
{
Console.Write("{0}\t", schedule[x0, x1]);
}
Console.WriteLine();
}
Console.ReadKey();
If you want to use foreach for any reason, you can also declare your table as [][] array. But in both ways you have to create 2 loops:
string[][] schedule = new string[][] {
new string[] { "1:00", "3:00", "5:00", "7:00", "TOTAL", "", },
new string[] {"Monday", "12", "10", "17", "22", "244", },
new string[] {"Tuesday", "11", "13", "17", "22", "252",},
new string[] {"Wednesday", "12", "10", "22", "22", "264",},
new string[] {"Thursday", "9", "14", "17", "22", "248",},
new string[] {"Friday", "12", "10", "21", "12", "220",},
new string[] {"Saturday", "12", "10", "5", "10", "148"},
new string[] {" ", " ", " ", " ", " ","1376",}
};
foreach (string[] line in schedule)
{
foreach (string i in line)
Console.Write("{0}\t", i);
Console.WriteLine();
}
If you use a monospaced font in the console you can adjust where each thing displays by putting more spaces if necessary
For example for the members corresponding to the the 1st and 2nd row and 1st of second column this would be the thing to calculate:
Largest word is Wednesday that is 9 letters, on first row first column I should put 9 espaces as there would be a blank. Then you might put four spaces as a separator between columns, then for the second column you calculate that 1:00 is the largest string so for 12 you would add 2 extra spaces, and so on.
Using a tab instead of some spaces is also likely to work but if the table ends up having some string that is much larger than one in another column it won't work.
Hope it helps.
Got it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Zumba1
{
class Zumba
{
static void Main(string[] args)
{ //Recreated the data in the table for the zumba section, added each row, and each column. Worked on formatting.
string[,] schedule = new string[8, 6] { { "\t\t1:00", "3:00", "5:00", "7:00", "TOTAL", "", },
{"Monday", "\t12", "10", "17", "22", "$244", },
{"Tuesday", "\t11", "13", "17", "22", "$252",},
{"Wednesday", "12", "10", "22", "22", "$264",},
{"Thursday", "9", "14", "17", "22", "$248",},
{"Friday", "\t12", "10", "21", "12", "$220",},
{"Saturday", "12", "10", "5", "10", "$148"},
{" ", " ", " ", " ", " ","\t$1376",}};
//Nested for loops to print in a table-style format.
for (int i = 0; i < schedule.GetLength(0); i++)
{
for (int j = 0; j < schedule.GetLength(1); j++)
{
Console.Write(schedule[i, j] + "\t");
}
{
Console.WriteLine(i.ToString());
}
}
Console.ReadLine();
}
}
}
}
Ok, before anyone attempts to label this as a duplicate, I am not asking for a string to a byte array. I want a string array, containing something similar like this: {"5","168","188","28","29","155"} to be converted to a byte array. I have searched, and was only able to find string to byte array, which is quite different. Thanks.
Edit: the array will be preset so that each member is parsable through byte.Parse, so this is not an issue.
This will fail for anything that can't be parsed by byte.Parse
var str = new[] {"5", "168", "188", "28", "29", "155"};
var byt = str.Select(byte.Parse).ToArray();
You have to parse each string into a byte and put in a new array. You can just loop though the items and convert each one:
string[] strings = { "5", "168", "188", "28", "29", "155" };
byte[] bytes = new byte[strings.length];
for (int i = 0; i < strings.Length; i++) {
bytes[i] = Byte.Parse(strings[i]);
}
You can also use the Array.ConvertAll method for this:
string[] strings = { "5", "168", "188", "28", "29", "155" };
byte[] bytes = Array.ConvertAll(strings, Byte.Parse);
You can also use LINQ extensions to do it:
string[] strings = { "5", "168", "188", "28", "29", "155" };
bytes = strings.Select(Byte.Parse).ToArray();
Assuming you mean that you have a string array like string[] myArray = {"5","168",...};:
myArray.Select(x => byte.Parse(x)).ToArray();
Or:
myArray.Select(byte.Parse).ToArray();
Try this
With linq
string[] strings = new[] { "1", "2", "3", "4" };
byte[] byteArray = strings.Select(x => byte.Parse(x)).ToArray();
Without linq
string[] strings = { "1", "2", "3", "4" };
byte[] bytArr = new byte[strings.Length];
int i = 0;
foreach(String text in strings)
{
bytArr[i++] = byte.Parse(text);
}
I am using C# regex library to do some text find and replace.
I would like to change the following:
1 -> one
11 -> one one
123 -> one two three
for example, here's my code to replace ampersand:
string pattern = "[&]";
string replacement = " and ";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(text, replacement);
Edit
I found some great examples of .NET RegEx on MSDN:
http://msdn.microsoft.com/en-us/library/kweb790z.aspx
Since you're specifically asking for a regex, you could do something like this
var digits = new Dictionary<string, string> {
{ "0", "zero" },
{ "1", "one" },
{ "2", "two" },
{ "3", "three" },
{ "4", "four" },
{ "5", "five" },
{ "6", "six" },
{ "7", "seven" },
{ "8", "eight" },
{ "9", "nine" }
};
var text = "this is a text with some numbers like 123 and 456";
text = Regex.Replace(text, #"\d", x => digits[x.Value]);
which will give you
this is a text with some numbers like onetwothree and fourfivesix