Input:
string param = "1100,1110,0110,0001";
Output:
int[] matrix = new[]
{
1,1,0,0,
1,1,1,0,
0,1,1,0,
0,0,0,1
};
What I did?
First of all I splited string to string[].
string[] resultantArray = param.Split(',');
Created one method, where I passed my string[].
var intArray = toIntArray(resultantArray);
static private int[] toIntArray(string[] strArray)
{
int[] intArray = new int[strArray.Length];
for (int i = 0; i < strArray.Length; i++)
{
intArray[i] = int.Parse(strArray[i]);
}
return intArray;
}
Issue?
I tried many solutions of SO, but none of them helped me.
Ended up with array without leading zeroes.
determine all digits: .Where(char.IsDigit)
convert the selected char-digits into integer: .Select(x => x-'0') (this is not as pretty as int.Parse or Convert.ToInt32 but it's super fast)
Code:
string param = "1100,1110,0110,0001";
int[] result = param.Where(char.IsDigit).Select(x => x-'0').ToArray();
As CodesInChaos commented, this could lead to an error if there are other type of Digits within your input like e.g. Thai digit characters: '๐' '๑' '๒' '๓' '๔' '๕' '๖' '๗' '๘' '๙' where char.IsDigit == true - if you need to handle such special cases you can allow only 0 and 1 in your result .Where("01".Contains)
You could also remove the commas and convert the result character-wise as follows using Linq.
string param = "1100,1110,0110,0001";
int[] result = param.Replace(",", "").Select(c => (int)Char.GetNumericValue(c)).ToArray();
yet another way to do this
static private IEnumerable<int> toIntArray(string[] strArray)
{
foreach (string str in strArray)
{
foreach (char c in str)
{
yield return (int)char.GetNumericValue(c);
}
}
}
What about this?
string input = "1100,1110,0110,0001";
var result = input
.Split(',')
.Select(e => e.ToCharArray()
.Select(f => int.Parse(f.ToString())).ToArray())
.ToArray();
string[] s=param.split(',');
Char[] c;
foreach(string i in s){
c+=i.ToCharArray()
}
int[] myintarray;
int j=0;
foreach(char i in c){
myintarray[j]=(int)i;
j++
}
Related
The goal is to remove a certain prefix word from a string in string array example: ["Market1", "Market2", "Market3"]. The prefix word Market is dominant in string array, so we have to remove Market from string array so the result should be ["1", "2", "3"]. Please take note that the Market prefix word in string could be anything.
Look for the first character that is not identical among all strings and select a substring starting at that position to remove the prefix.
string[] words = new string[] { "Market1", "Market2", "Market3" };
int i = 0;
while (words.All(word => word.Length > i && word[i] == words[0][i])) ++i;
var wordsWithoutPrefixes = words.Select(word => word.Substring(i)).ToArray();
Make a delimited string and then replace all the Market with an empty string and then split the string to an array.
string[] arr = new string[] { "Market1", "Market2", "Market3" };
string[] result = string.Join(".", arr).Replace("Market", "").Split('.');
Loop through each item in the array and for each item chop off the beginning the start matches.
var commonPrefix = "Market";
for (int i = 0; i < arr.length, i++) {
if(arr[i].IndexOf(commonPrefix) == 0) {
arr[i] = arr[i].Substring(commonPrefix.Length);
}
}
You can use LINQ:
string[] myArray = ["Market1", "Market2", "Market3"];
string prefix = myArray[0];
foreach (var s in myArray)
{
while (!s.StartsWith(prefix))
prefix = prefix.Substring(0, prefix.Length - 1);
}
string[] result = myArray
.Select(s => s.Substring(prefix.Length))
.ToArray();
Loop through the array of string and replace the substring containing prefix with an empty substring.
string[] s=new string[]{"Market1","Market2","Market3"};
string prefix="Market";
foreach(var x in s)
{
if(x.Contains(prefix))
{
x=x.Replace(prefix,"");
}
}
My front-end application sends strings that look like this:
"12-15"
to a back-end C# application.
Can someone give me some pointers as to how I could extract the two numbers into two variables. Note the format is always the same with two numbers and a hyphen between them.
string stringToSplit = "12-15";
string[] splitStringArray;
splitStringArray = stringToSplit.Split('-');
splitStringArray[0] will be 12
splitStringArray[1] will be 15
Split the string into parts:
string s = "12-15";
string[] num = s.Split('-');
int part1 = Convert.ToInt32(num[0]);
int part2 = Convert.ToInt32(num[1]);
int[] numbers = "12-15".Split('-')
.Select(x => {
int n;
int.TryParse(x, out n);
return n;
})
.ToArray();
We call Split on a string instance. This program splits on a single character
string s ="12-15";
string[] words = s.Split('-');
foreach (string word in words)
{
int convertedvalue = Convert.ToInt32(word );
Console.WriteLine(word);
}
string[] ss= s.Split('-');
int x = Convert.ToInt32(ss[0]);
int y = Convert.ToInt32(ss[1]);
more info
You can use the below code to split and it will return string for each value, then you can typecast it to any type you wish to ...
string myString = "12-15-18-20-25-60";
string[] splittedStrings = myString.Split('-');
foreach (var splittedString in splittedStrings)
{
Console.WriteLine(splittedString + "\n");
}
Console.ReadLine();
Here is the correct version without the wrong code
string textReceived = "12-15";
string[] numbers = textReceived.Split('-');
List<int> numberCollection = new List<int>();
foreach (var item in numbers)
{
numberCollection.Add(Convert.ToInt32(item));
}
String numberString = "12-15" ;
string[] arr = numberString.Split("-");
Now you will get a string array , you can use parsing to get the numbers alone
int firstNumber = Convert.ToInt32(arr[0]);
Helpful answer related to parsing :
https://stackoverflow.com/a/199484/5395773
You could convert that string explicitly to an integer array using Array.ConvertAll method and access the numbers using their index, you can run the below example here.
using System;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
var number = "12-15";
var numbers = Array.ConvertAll(number.Split('-'), int.Parse);
Console.WriteLine(numbers[0]);
Console.WriteLine(numbers[1]);
}
}
}
Or you can explicitly convert the numeric string using int.Parse method, the int keyword is an alias name for System.Int32 and it is preffered over the complete system type name System.Int32, you can run the below example here.
using System;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
var number = "12-15";
var numbers = number.Split('-');
var one = int.Parse(numbers[0]);
var two = int.Parse(numbers[1]);
Console.WriteLine(one);
Console.WriteLine(two);
}
}
}
Additional read: Please check int.Parse vs. Convert.Int32 vs. int.TryParse for more insight on parsing the input to integer
string str = null;
string[] strArr = null;
int count = 0;
str = "12-15";
char[] splitchar = { '-' };
strArr = str.Split(splitchar);
for (count = 0; count <= strArr.Length - 1; count++)
{
MessageBox.Show(strArr[count]);
}
I want to make a program which takes a string you entered and turn it to different string, so if for example I input "Hello World" every char will turn into a string and the console will output something like "Alpha Beta Gamma Gamma Zeta Foxtrot Dona Rama Lana Zema" - making every char a word.
I tried doing it like this :
static string WordMap(string value)
{
char[] buffer = value.ToCharArray();
for (int i = 0; i < buffer.Length; i++)
{
if (letter = "a")
{
letter = ("Alpha");
}
//and so on
buffer[i] = letter;
}
return new string(buffer);
}
but I just can't get it to work.
can anyone give me a tip or point me at the right direction?
What you need is a Dictionary<char,string>
var words = new Dictionary<char, string>();
words.Add('a', "Alpha");
words.Add('b',"Beta");
...
string input = Console.ReadLine();
string[] contents = new string[input.Length];
for (int i = 0; i < input.Length; i++)
{
if (words.ContainsKey(input[i]))
{
contents[i] = words[input[i]];
}
}
string result = string.Join(" ", contents);
Or LINQ way:
var result = string.Join(" ", input.Where(words.ContainsKey).Select(c => words[c]));
First off, the buffer is a char array. Arrays have a fixed size and to expand them you need to create a new one. To overcome this cumbersome work, there is a StringBuilder class that does this automatically.
Secondly, if you keep these 'Alpha', 'Beta', ... strings in if statements you will have a very long piece of code. You can replace this by using a dictionary, or create it from a single string or text file.
To put this into practice:
class MyClass
{
static Dictionary<char, string> _map = new Dictionary<char, string>();
static MyClass()
{
_map.Add('a', "Alpha");
_map.Add('b', "Beta");
// etc
}
static string WordMap(string data)
{
var output = new StringBuilder();
foreach (char c in data)
{
if (_map.ContainsKey(c))
{
output.Append(_map[c]);
output.Append(' ');
}
}
return output.ToString();
}
}
Solution without a dictionary:
static string WordMap(string data)
{
const string words = "Alpha Beta Gamma Delta ...";
string[] wordMap = words.Split(' ');
var output = new StringBuilder();
foreach (char c in data)
{
int index = c - 'a';
if (index >= 0 && index < wordMap.Length)
{
output.Append(wordMap[index]);
output.Append(' ');
}
}
return output.ToString();
}
With LINQ and String.Join it's short and readable. Since you want to have a new word for special chards you need a word-map. I would use a Dictionary<char, string>:
static Dictionary<char, string> wordMap = new Dictionary<char, string>()
{
{'a', "Alpha"}, {'b', "Beta"},{'c', "Gamma"}, {'d', "Delta"}
};
static string WordMap(string value)
{
var strings = value
.Select(c =>
{
string word;
if(!wordMap.TryGetValue(c, out word))
word = c.ToString();
return word;
});
return string.Join("", strings);
}
Test:
string newString = WordMap("abcdeghj"); // AlphaBetaGammaDeltaeghj
Tips:
You don't have to create character array from string, you can easily access single characters in string by indexer:
char some = "123"[2];
When you use "" then you create string not char therefore you should use '' to create character for comparison:
if (some == 'a') Console.WriteLine("character is a, see how you compare chars!!!");
A good solution ...
string[] words = { "Alpha", "Beta", "C_word", "D_Word" }; // ....
string myPhrase = "aBC";
myPhrase.Replace(" ", string.Empty).ToList().ForEach(a =>
{
int asciiCode = (int)a;
/// ToUpper()
int index = asciiCode >= 97 ? asciiCode - 32 : asciiCode;
Console.WriteLine(words[index - 65]); // ascii --> 65-a , 66-b ...
});
One more variation of answer which contains not found option.
static Dictionary<char, string> Mapping =
new Dictionary<char, string>()
{ { 'a', "alpha" }, { 'b', "beta" }, { 'c', "gamma" }, { 'd', "zeta" } };
static void Main(string[] args)
{
string test = "abcx";
Console.WriteLine(string.Join(" ", test.Select(t => GetMapping(t))));
//output alpha beta gamma not found
Console.ReadKey();
}
static string GetMapping(char key)
{
if (Mapping.ContainsKey(key))
return Mapping.First(a => a.Key == key).Value;
else
return "not found";
}
Just declare a second variable where you build up your result.
And I think you have some syntax problems, you need to have "==" in a
condition, otherwise it's an assignment.
static string WordMap(string value)
{
string result = string.Empty;
char[] buffer = value.ToCharArray();
for (int i = 0; i < buffer.Length; i++)
{
if (letter == "a")
{
result += ("Alpha");
}
//and so on
}
return result;
}
But I would only do this that way, if this is "just for fun" code,
as it will not be very fast.
Building up the result like I did is slow, a better way would be
result = string.Concat(result, "(Alpha)");
And an even faster way is using a StringBuilder (s. documentation for that),
which offers you fast and convenient methods to deal with bigger strings.
Only downfall here is, that you need to know a little bit, how big the result
will be in characters, as you need to provide a starting dimension.
And here you should not start with simply 1, or 100. Each time, when the StringBuilder
is full, it creates a new bigger instance and copies the values, so multiple instances
of that will fill your memory, which can cause an out of memory exception,
when dealing with some ten thousands of characters.
But as said, for just for fun code, all of that does not matter...
And of course, you need to be aware, that if you do it like that, your result will
be in one straight line, no breaks. If you want line breaks add "\n" at the end of
the strings. Or add anything elese you need.
Regards,
Markus
I have a string which contains binary digits. How to separate string after each 8 digit?
Suppose the string is:
string x = "111111110000000011111111000000001111111100000000";
I want to add a separator like ,(comma) after each 8 character.
output should be :
"11111111,00000000,11111111,00000000,11111111,00000000,"
Then I want to send it to a list<> last 8 char 1st then the previous 8 chars(excepting ,) and so on.
How can I do this?
Regex.Replace(myString, ".{8}", "$0,");
If you want an array of eight-character strings, then the following is probably easier:
Regex.Split(myString, "(?<=^(.{8})+)");
which will split the string only at points where a multiple of eight characters precede it.
Try this:
var s = "111111110000000011111111000000001111111100000000";
var list = Enumerable
.Range(0, s.Length/8)
.Select(i => s.Substring(i*8, 8));
var res = string.Join(",", list);
There's another Regex approach:
var str = "111111110000000011111111000000001111111100000000";
# for .NET 4
var res = String.Join(",",Regex.Matches(str, #"\d{8}").Cast<Match>());
# for .NET 3.5
var res = String.Join(",", Regex.Matches(str, #"\d{8}")
.OfType<Match>()
.Select(m => m.Value).ToArray());
...or old school:
public static List<string> splitter(string in, out string csv)
{
if (in.length % 8 != 0) throw new ArgumentException("in");
var lst = new List<string>(in/8);
for (int i=0; i < in.length / 8; i++) lst.Add(in.Substring(i*8,8));
csv = string.Join(",", lst); //This we want in input order (I believe)
lst.Reverse(); //As we want list in reverse order (I believe)
return lst;
}
Ugly but less garbage:
private string InsertStrings(string s, int insertEvery, char insert)
{
char[] ins = s.ToCharArray();
int length = s.Length + (s.Length / insertEvery);
if (ins.Length % insertEvery == 0)
{
length--;
}
var outs = new char[length];
long di = 0;
long si = 0;
while (si < s.Length - insertEvery)
{
Array.Copy(ins, si, outs, di, insertEvery);
si += insertEvery;
di += insertEvery;
outs[di] = insert;
di ++;
}
Array.Copy(ins, si, outs, di, ins.Length - si);
return new string(outs);
}
String overload:
private string InsertStrings(string s, int insertEvery, string insert)
{
char[] ins = s.ToCharArray();
char[] inserts = insert.ToCharArray();
int insertLength = inserts.Length;
int length = s.Length + (s.Length / insertEvery) * insert.Length;
if (ins.Length % insertEvery == 0)
{
length -= insert.Length;
}
var outs = new char[length];
long di = 0;
long si = 0;
while (si < s.Length - insertEvery)
{
Array.Copy(ins, si, outs, di, insertEvery);
si += insertEvery;
di += insertEvery;
Array.Copy(inserts, 0, outs, di, insertLength);
di += insertLength;
}
Array.Copy(ins, si, outs, di, ins.Length - si);
return new string(outs);
}
If I understand your last requirement correctly (it's not clear to me if you need the intermediate comma-delimited string or not), you could do this:
var enumerable = "111111110000000011111111000000001111111100000000".Batch(8).Reverse();
By utilizing morelinq.
Here my two little cents too. An implementation using StringBuilder:
public static string AddChunkSeparator (string str, int chunk_len, char separator)
{
if (str == null || str.Length < chunk_len) {
return str;
}
StringBuilder builder = new StringBuilder();
for (var index = 0; index < str.Length; index += chunk_len) {
builder.Append(str, index, chunk_len);
builder.Append(separator);
}
return builder.ToString();
}
You can call it like this:
string data = "111111110000000011111111000000001111111100000000";
string output = AddChunkSeparator(data, 8, ',');
One way using LINQ:
string data = "111111110000000011111111000000001111111100000000";
const int separateOnLength = 8;
string separated = new string(
data.Select((x,i) => i > 0 && i % separateOnLength == 0 ? new [] { ',', x } : new [] { x })
.SelectMany(x => x)
.ToArray()
);
I did it using Pattern & Matcher as following way:
fun addAnyCharacter(input: String, insertion: String, interval: Int): String {
val pattern = Pattern.compile("(.{$interval})", Pattern.DOTALL)
val matcher = pattern.matcher(input)
return matcher.replaceAll("$1$insertion")
}
Where:
input indicates Input string. Check results section.
insertion indicates Insert string between those characters. For example comma (,), start(*), hash(#).
interval indicates at which interval you want to add insertion character.
input indicates Input string. Check results section. Check results section; here I've added insertion at every 4th character.
Results:
I/P: 1234XXXXXXXX5678 O/P: 1234 XXXX XXXX 5678
I/P: 1234567812345678 O/P: 1234 5678 1234 5678
I/P: ABCDEFGHIJKLMNOP O/P: ABCD EFGH IJKL MNOP
Hope this helps.
As of .Net 6, you can simply use the IEnumerable.Chunk method (Which splits elements of a sequence into chunks) then reconcatenate the chunks using String.Join.
var text = "...";
string.Join(',', text.Chunk(size: 6).Select(x => new string(x)));
This is much faster without copying array (this version inserts space every 3 digits but you can adjust it to your needs)
public string GetString(double valueField)
{
char[] ins = valueField.ToString().ToCharArray();
int length = ins.Length + (ins.Length / 3);
if (ins.Length % 3 == 0)
{
length--;
}
char[] outs = new char[length];
int i = length - 1;
int j = ins.Length - 1;
int k = 0;
do
{
if (k == 3)
{
outs[i--] = ' ';
k = 0;
}
else
{
outs[i--] = ins[j--];
k++;
}
}
while (i >= 0);
return new string(outs);
}
For every 1 character, you could do this one-liner:
string.Join(".", "1234".ToArray()) //result: 1.2.3.4
If you intend to create your own function to acheive this without using regex or pattern matching methods, you can create a simple function like this:
String formatString(String key, String seperator, int afterEvery){
String formattedKey = "";
for(int i=0; i<key.length(); i++){
formattedKey += key.substring(i,i+1);
if((i+1)%afterEvery==0)
formattedKey += seperator;
}
if(formattedKey.endsWith("-"))
formattedKey = formattedKey.substring(0,formattedKey.length()-1);
return formattedKey;
}
Calling the mothod like this
formatString("ABCDEFGHIJKLMNOPQRST", "-", 4)
Would result in the return string as this
ABCD-EFGH-IJKL-MNOP-QRST
A little late to the party, but here's a simplified LINQ expression to break an input string x into groups of n separated by another string sep:
string sep = ",";
int n = 8;
string result = String.Join(sep, x.InSetsOf(n).Select(g => new String(g.ToArray())));
A quick rundown of what's happening here:
x is being treated as an IEnumerable<char>, which is where the InSetsOf extension method comes in.
InSetsOf(n) groups characters into an IEnumerable of IEnumerable -- each entry in the outer grouping contains an inner group of n characters.
Inside the Select method, each group of n characters is turned back into a string by using the String() constructor that takes an array of chars.
The result of Select is now an IEnumerable<string>, which is passed into String.Join to interleave the sep string, just like any other example.
I am more than late with my answer but you can use this one:
static string PutLineBreak(string str, int split)
{
for (int a = 1; a <= str.Length; a++)
{
if (a % split == 0)
str = str.Insert(a, "\n");
}
return str;
}
Given a string
string result = "01234"
I want to get the separate integers 0,1,2,3,4 from the string.
How to do that?
1
The following code is giving me the ascii values
List<int> ints = new List<int>();
foreach (char c in result.ToCharArray())
{
ints.Add(Convert.ToInt32(c));
}
EDIT: I hadn't spotted the ".NET 2.0" requirement. If you're going to do a lot of this sort of thing, it would probably be worth using LINQBridge, and see the later bit - particularly if you can use C# 3.0 while still targeting 2.0. Otherwise:
List<int> integers = new List<int>(text.Length);
foreach (char c in text)
{
integers.Add(c - '0');
}
Not as neat, but it will work. Alternatively:
List<char> chars = new List<char>(text);
List<int> integers = chars.ConvertAll(delegate(char c) { return c - '0'; });
Or if you'd be happy with an array:
char[] chars = text.ToCharArray();
int[] integers = Arrays.ConvertAll<char, int>(chars,
delegate(char c) { return c - '0'; });
Original answer
Some others have suggested using ToCharArray. You don't need to do that - string already implements IEnumerable<char>, so you can already treat it as a sequence of characters. You then just need to turn each character digit into the integer representation; the easiest way of doing that is to subtract the Unicode value for character '0':
IEnumerable<int> digits = text.Select(x => x - '0');
If you want this in a List<int> instead, just do:
List<int> digits = text.Select(x => x - '0').ToList();
Loop the characters and convert each to a number. You can put them in an array:
int[] digits = result.Select(c => c - '0').ToArray();
Or you can loop through them directly:
foreach (int digit in result.Select(c => c - '0')) {
...
}
Edit:
As you clarified that you are using framework 2.0, you can apply the same calculation in your loop:
List<int> ints = new List<int>(result.Length);
foreach (char c in result) {
ints.Add(c - '0');
}
Note: Specify the capacity when you create the list, that elliminates the need for the list to resize itself. You don't need to use ToCharArray to loop the characters in the string.
You could use LINQ:
var ints = result.Select(c => Int32.Parse(c.ToString()));
Edit:
Not using LINQ, your loop seems good enough. Just use Int32.Parse instead of Convert.ToInt32:
List<int> ints = new List<int>();
foreach (char c in result.ToCharArray())
{
ints.Add(Int32.Parse(c.ToString()));
}
string result = "01234";
List<int> list = new List<int>();
foreach (var item in result)
{
list.Add(item - '0');
}
Index into the string to extract each character. Convert each character into a number. Code left as an exercise for the reader.
another solution...
string numbers = "012345";
List<int> list = new List<int>();
foreach (char c in numbers)
{
list.Add(int.Parse(c.ToString()));
}
no real need to do a char array from the string since a string can be enumerated over just like an array.
also, the ToString() makes it a string so the int.Parse will give you the number instead of the ASCII value you get when converting a char.
List<int> ints = new List<int>();
foreach (char c in result.ToCharArray())
{
ints.Add(Convert.ToInt32(c));
}
static int[] ParseInts(string s) {
int[] ret = new int[s.Length];
for (int i = 0; i < s.Length; i++) {
if (!int.TryParse(s[i].ToString(), out ret[i]))
throw new InvalidCastException(String.Format("Cannot parse '{0}' as int (char {1} of {2}).", s[i], i, s.Length));
}
return ret;
}