How to "Multiply" a String (In C#)

Can I multiply a string (in C#)?

In .NET 4 you can do this:

String.Concat(Enumerable.Repeat("Hello", 4))

Multiplying strings in C#

There is an extension method for it in this post.

public static string Multiply(this string source, int multiplier)
{
StringBuilder sb = new StringBuilder(multiplier * source.Length);
for (int i = 0; i < multiplier; i++)
{
sb.Append(source);
}

return sb.ToString();
}

string s = "</li></ul>".Multiply(10);

How to multiply the number of two labels in C#

so the problem was a dollar sign ( $ ) that i put before the numbers.

i just deleted the sign and this is what the code looks like now:

double AA;
if (!double.TryParse(BTC_A.Text, out AA))
{
MessageBox.Show($"Unable to convert the BTC_A \"{BTC_A.Text}\" to a floating point number");
return;
}
double btcA;
if (!double.TryParse(BTCPrice_Label.Text, out btcA))
{
MessageBox.Show($"Unable to convert the price \"{BTCPrice_Label.Text}\" to a floating point number");
return;
}
label1.Text = (AA * btcA).ToString();

How to multiply two string values and store result with , as a separator?

Try using Decimal.Parse to convert the strings to a "number", then calculate the result.

The result can be converted to a string using Decimal.ToString with your own culture (IFormatProvider).

See https://learn.microsoft.com/en-us/dotnet/standard/base-types/how-to-define-and-use-custom-numeric-format-providers.

how to multiply odd position in a string

Keep in mind that strings start at index 0. So based on your input you actually want to multiply the numbers on the even positions.

Change

if(cijfer%2==0)
{
cijfer = cijfer*2
}

into

if(i % 2 == 0)
{
cijfer = cijfer*2
}

Converting String to Integer without using Multiplication C#

If you assume a base-10 number system and substituting the multiplication by bit shifts (see here) this can be a solution for positive integers.

public int StringToInteger(string value)
{
int number = 0;
foreach (var character in value)
number = (number << 1) + (number << 3) + (character - '0');

return number;
}

See the example on ideone.

The only assumption is that the characters '0' to '9' lie directly next to each other in the character set. The digit-characters are converted to their integer value using character - '0'.

Edit:

For negative integers this version (see here) works.

public static int StringToInteger(string value)
{
bool negative = false;
int i = 0;

if (value[0] == '-')
{
negative = true;
++i;
}

int number = 0;
for (; i < value.Length; ++i)
{
var character = value[i];
number = (number << 1) + (number << 3) + (character - '0');
}

if (negative)
number = -number;
return number;
}

In general you should take errors into account like null checks, problems with other non numeric characters, etc.

Is there an easy way to return a string repeated X number of times?

If you only intend to repeat the same character you can use the string constructor that accepts a char and the number of times to repeat it new String(char c, int count).

For example, to repeat a dash five times:

string result = new String('-', 5);
Output: -----

how do I do a multiplication calculation with a string (textbox) and a Double value in c#?

Of course the parsing will work, that's why the double.Parse() method exists.
You could use it like this

txtCost.Text = (double.Parse(txtHours.Text) * costPerHour + costOfInsurance).ToString();

you're converting txtHours.Text to double, multiplying it with costPerHour and adding CostOfInsurance. After that is calculated, result is converted to string and put into txtCost control

Multiplying the ToString value in Unity C#

You will need to convert it back into a numerical Type first.
Try:

float.Parse(transform.localScale.x.ToString()) *= 10f;

Or, if what you are trying to achieve is to multiply it and then convert it to a string you could try:

(transform.localScale.x * 10f).ToString();


Related Topics



Leave a reply



Submit