Add periods in a string [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm unable to determine why I don't get my expected output, given this code:
int periods = (location.Length / 2) - 1;
for (int index = 2, i = 0; i < periods; index += 3, ++i )
{
location = location.Insert(index, ".");
}
And a location of "C5032AC", I expect that location will equal "C.50.32.A.C" after my loop terminates; it is instead "C5.03.2AC". Can anyone explain what I'm missing here?

I would investigate using regular expressions to help you achieve this goal. You should be able to create a regular expression that matches specific patterns in the string, and you should be able to insert characters between those matches. Please see this article Regular Expressions MSDN
I've been asked to provide a bit of code to help support this. I don't believe regular expressions are overkill, and I believe something along the lines of this example will provide at least a step in the right direction.
line=Regex.Replace(line,#"([\w])(\d{2})(\d{2})(\w)(\w)","$1.$2.$3.$4.$5");

Related

nth root of a number based on two user inputs [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
var result6 = Math.Sqrt(num1, num2);
Console.Write(name + " this is the final result of square rooting = "+ result6);
I would like to find the nth root of a number based on two user inputs. I am trying to use the Math.Sqrt() method to achieve this.
This piece of script is outputting an error No overload for method 'Sqrt' takes 2 arguments [Main2], is there a method to fix this error?
As stated in the docs the Math.sqrt(double) method only takes one parameter, no overloads.
If you meant to take the nth root, you could use Math.Pow(Double, Double) and put 1 over the second parameter, such as
Math.Pow(64, 1/3); // Cube root of 64
// Output: 4

C# Regex meet first match and take everything from start of string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a string input which looks like:
var price = "£1.33(including postage)";
I'd like to take out the first part of the string before ( so that the output of regex would look like this:
"£1.33"
I'm new with Regex so I'm not quite sure how to do this, can someone help me out?
P.S. I thought of doing a substring, but that wouldn't work since price can have more decimals, and can be a larger price, so this option definitely wouldn't work
do you have to use regex?
much easier if you use split
string result = price.Split('(').First();
You don't need Regex for this if you have the same basic format of the "price" just different values.
var result = price.Substring(0, price.IndexOf("("));.

how do i display how many digits the number has? [closed]

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 7 years ago.
Improve this question
Currently very new to C# and coding , so i will be more than happy if someone will explain me how to display how many digits the number has. For example the number 12345 has 5 digits.the main theme in the class is while loops so the answer probably need to contain while loop.TY
You can either use this
Math.Abs(myint).ToString().Length
and if you absolutely must use a while loop then
number = Math.Abs(number);
int length = 1;
while ((number /= 10) >= 1)
length++;
To test code
string.Trim().Replace("-","").Length
so if you have a number you should make it a string first using ToString()
The Length returns the number of characters that you hold within your string minus your white spaces (Because of the Trim()),i don't see why you would want to use the while loop in the first place.
Edit : if you have a minus number the .Replace() will take care of that.

What does string.IndexOf do? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm working through string manipulation, where I need to find characters or copy some part of the string that the user has input and divide it to 3 different areas. I'm not asking anything here about code, I'll do it myself, but I was searching in the documentation that Microsoft provide about the IndexOf method and its 8 overloads and I can't really understand how to apply it. I simply can't understand what it is supposed to do.
Returns the first appearance of a specified char.
For example
string x = "Hello World";
x.indexOf("W");
it will return 6 (0 based count).
The overloads let you choose for example, where you want to start searching.. like
x.indexOf("W", 7); it will return -1 because W is at position 6 so if starts at 7 it won't find any.
I hope this helps ! the best way is to play with it
This also works with arrays.
I believe the MSDN explanation is pretty clear.
For example:
string something = "something";
int indexOfT = something.IndexOf("t"); // => returns 4
Reports the zero-based index of the first occurrence of the specified string in this instance.
So if "t" is in the fifth position of "something", 4 would be it's zero-based index.

c# regular expression pattern [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
[code]>
10 /td>[/code]
I want to get 10 from this line. But I need to preserve the pattern. How can I write an expression that gets:
, newline, ignorewhitespace OR included indeterminate spaces, 10, ignorewhitespace OR included all space, /td
Thanks,
JOe K.
Ok, I'll be nice and help you this time. =)
I assumed that the number you are looking for can change.
var result = Regex.Match(
"[code]> 101 /td>[/code]",
#"(?<=\>\s*)\d+(?=\s*/td\>)").Value;
But please, try doing it the next time... I'll point you a tool that could help in designing and learning Regex:
http://www.radsoftware.com.au/regexdesigner/

Categories

Resources