I have a char array that print A-Z if i convert it to a string but when I try to get a character from an index location I get nothing? ..
char[] codes= new char[156];
for (int i = 65; i < 91; i++) codes[i] = (char)i;
Console.WriteLine(codes[2]);
Because you're starting to store the char's at index 65..
Console.WriteLine(codes[65]); // A
It has to do with the fact that you created a gigantic array but only are populating a small section of it and indexing into a portion that contains no data.
You could consider simplifying using a range..
var chars = Enumerable.Range(65, 90).Select(c => (char)c).ToArray();
Console.WriteLine(chars[2]);
Or you can modify your code to this.
char[] codes = new char[26];
for (int i = 0; i < 26; i++)
{
codes[i] = (char)i;
codes[i] += 'A';
}
Console.WriteLine(codes[2]);
You try the below code
Console.WriteLine(codes[2].ToString());
The reason is Console.WriteLine can display only the string value.
Related
I have an array where user inputs random characters, and i need to replace all numbers with symbol "*". And the worst thing is, that i cant use built in functions! If you can, help please!
Here if char.Number is build in function you should use numbers values from ASCII TABLE for the numbers.
string input = "ArrayWithR23andomChar44acter3sWit55hNumbersI6nIt";
char[] array = input.ToCharArray();
for(int i=0; i < array.Length; i++)
{
if (!char.IsNumber(input[i]))
continue;
array[i] = '*';
}
Here without char.IsNumber you can do it like this:
string input = "ArrayWithR23andomChar44acter3sWit55hNumbersI6nIt";
char[] array = input.ToCharArray();
for(int i=0; i < array.Length; i++)
{
if ((int)input[i] >= 48 && (int)input[i] <=57)
{
array[i] = '*';
}
}
Basically array of characters is nothing than string. You can use this regex to do the job done. For example:
string test = "dsad54dsads56dasd7a8s 5468sda";
Regex:
string t1 = Regex.Replace(test, "[0-9]+", "*");
or
string t1 = Regex.Replace(test, "[0-9]", "*");
The difference is that the first one will replace all consecutive numbers with just one *. The second one will replace every single number with *.
Or, if regex is considered as built in function you can use something like this:
char[] t2 = test.Select(c =>
{
if (c >= '0' && c <= '9')
{
return '*';
}
return c;
}).ToArray();
G'day,
I have a char array that varies in size based on the length of some data. The array will never be larger than 24 though. It may however, be less than 24. As a result of this I need to "pad left" with 0's, as in add more elements to the left of the array to make it 24. I have below some code that does this, just wondering if there is a faster way or more efficient way to do so. Thanks!
Note: I don't think this is a duplicate as I am working with char[]'s not strings.
char[] dataLen = Convert.ToString(data.Length, 2).ToCharArray();
int j = 0;
char[] tmp = new char[24];
for (int i = 0; i < 24; i++)
{
if (i < (24 - dataLen.Length))
tmp[i] = '0';
else
tmp[i] = dataLen[j++];
}
dataLen = tmp;
Why don't you simply use String.PadLeft?
char[] data = "abcdefgh".ToCharArray(); // sample data
data = new string(data).PadLeft(24, '0').ToCharArray();
That should be efficient and is also very readable.
how about :
string z24 = "000000000000000000000";
tmp = z24.Take(24 - dataLen.Length).Union(dataLen).ToArray();
I would simply use a string for all operations, and use PadLeft to do the padding.
string input = new string(data);
string result = input.PadLeft(24, '0');
Then convert it to a char[] if you really need to:
char[] chars = result.ToCharArray();
(Also, your Convert.ToString(data.Length, 2) doesn't return their string representation, new string(data) does)
ListBox box = GetListBox(); //placeholder for the sample
string s = "123456776543219898989";
char[] c = s.ToCharArray();
for(int i=0;i<c.Length;i+=7)
{
box.Items.Add(new string(c, i, 7));
}
This is fast way to separate text.
you can do an easy for loop
ListBox box = null;//set it yourself
for(int i = 0; i < s.Length; i+= 7)
{
box.Items.Add(s.SubString(i, Math.Min(s.Length - i, 7));
}
Break the string into a character array, and use that to create your items. This string constructor overload will help:
http://msdn.microsoft.com/en-us/library/ms131424.aspx
This code is just a sample. What you actually need will depend on how you want to handle the situation where the number of characters in the original string is not evenly divisible by 7.
ListBox box = GetListBox(); //placeholder for the sample
string s = "123456776543219898989";
char[] c = s.ToCharArray();
for(int i=0;i<c.Length;i+=7)
{
box.Items.Add(new string(c, i, 7));
}
I could also do this directly on the string, instead of creating the array, but this should be much faster than calling .SubString() repeatedly.
var str = "123456776543219898989";
int count = 0;
var parts = str.GroupBy(_ => count++ / 7)
.Select(x => string.Concat(x))
.ToArray();
listBox1.Items.AddRange(parts);
i can't find any mistakes in my code.
here i'm trying to pick all numbers from the string:
(just to simplify the example,, i want to pick numbers that will satisfy some condition)
i use Queue cause i don't want to deal with array's indexes.
Console.Write("enter string: ");
string s = Console.ReadLine();
char[] array = s.ToCharArray();
Queue<char> q = new Queue<char>();
for (int i = 0; i < array.Length; i++)
{
q.Enqueue(array[i]);
}
char[] new_array = new char[q.Count];
for (int i = 0; i < q.Count; i++)
{
new_array[i] = q.Dequeue();
}
Console.WriteLine(new String(new_array));
Input string: 123456
And the output is a little weird:
123
another input: 123
output: 12
of course i made some mistake) but everything seems to be OK
Thank YOU in advance
The problem is the second loop:
for (int i = 0; i < q.Count; i++)
{
new_array[i] = q.Dequeue();
}
As q.Count decrements on every loop iteration, and i increases on every interation, you get only half of the elements.
try something like:
for (int i = 0; q.Count > 0; i++)
{
new_array[i] = q.Dequeue();
}
also consider: Queue.toArray
I would suggest using List<char> instead of Queue<char> and char[]. There's nothing here that particularly needs a queue, and it would avoid the problem that Rudolf pointed out, and a List is much easier to work with than an array. You can also use foreach instead of a for loop, and avoid the intermediate step.
Console.Write("enter string: ");
string s = Console.ReadLine();
List<char> new_array = new List<char>();
foreach(char c in s.ToCharArray())
{
new_array.Add(c);
}
Console.WriteLine(new String(new_array.ToArray()));
As the reason for your error is already stated,you can replace your two loops with just two statements
//A version of Queue constructor accepts IEnumerable object.
//you can directly pass the string to the queue constructor.
Queue<char> Que = new Queue<char>("123456");
//Copies the array and the position is preserved
var new_arr= Que.ToArray();
According to MSDN:
Removes and returns the object at the beginning of the Queue.
As you use Dequeue(), the q.Count value changes in each iteration.
So rather than using q.Count in this loop;
for (int i = 0; i < q.Count; i++)
use
int queueSize = q.Count;
for (int i = 0; i < queueSize; i++)
This will keep your looping limit as a constant number rather than calculating it in each iteration to find a different value because of using Dequeue().
char[] charArray = startno.ToCharArray();
//using this arry
//i want to cheque this
int i=0;
count = 0;
while (chenum [i] != "0")
{
count++;
i++;
}
string s = "0";
string zero = "0";
for (i = 1; i <= count; i++)
{
s = s + zero;
}
will u help me to correct this code...
eg:(00001101) i need to add this no with 1.
for that i want to convert this value to int.if i convert to int the no will be(1101)+1 no will be (1102).after adding i want the answer (00001102).
how many zeros do you want?? You can use string.pad
int count = 1102;
int NumOfZeros = 10;
string s = count.ToString().PadLeft(NumOfZeros, '0');
there is also the number formatter.
count.ToString("D10");
String num = "000001101";
int item = int.Parse(num);
item++;
String output = item.ToString("D8");
You need to use String.Format("{0:00000000}", 1101);, which would be 00001101
If you are storing this number as an int (and you should) 1102 annd 00001102 are the same thing. Use string formatting later when you need to output the value with some zeros.
1102.ToString("D8") will give you the string "00001102"
Also, possible duplicates of this question: Pad with leading zeros
Try int.parseInt(startNo) instead of converting it to a char array.