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.
Related
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
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 6 years ago.
Improve this question
I have two urls:
http://sp2013/sites/1234
and
http://sp2013/pwa/projectsites/projectdetails.aspx?projuid=1234-123-123-123456-123456
I want to get from the first url the last part (always a number)
1234
and from the second url also the last part (guid)
1234-123-123-123456-123456
How could i achieve this through RegEx or maybe with string operations in C#?
string s = "http://sp2013/sites/1234";
var firstURLlastPart = new Uri(s).Segments.Last();
string s = "http://sp2013/pwa/projectsites/projectdetails.aspx?projuid=1234-123-123-123456-123456";
var secondURLlastPart = s.Split('=').Last();
Just split at '/' and take the last chunk for the first case.
str.Split('/').Last()
Split at '=' and take last chunk for second case
str.Split('=').Last()
(?!\/|\=).[-0-9]+$
It's working for both. But it also accepts if in first example will be number in format like this:
12345-4568
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 6 years ago.
Improve this question
Hello experts, I have to generate series of folders from a TextBox into specified location.I am having two textboxes to specify the limit of folders(say 30 folders).The problem am facing is that the folder names that i will be providing are alpha-numeric(say 121cs3h101) .
How to set limit when i provide an alpha-numeric values?
(For example: i provide textbox1=12cs3h101 and textbox2=12cs3h131 , i need the series limit to be generated). I am working with visual studio 2013 in c# windows form application. Thanks in advance.
ok I will try to give you a lead.
To parse a string or find specific characters one can use RegEx.Match or a simler method called String.Split. In both cases you have to be aware how your string is structured and how it can vary. The limits of variation are very important.
If as you say the beginning is always"12cs3h" you can either split the string at the character 'h'.
string[] sa = s.Split('h');
Or you can even use the index of 'h' (since the length seems to be fixed) and take the rest of the string to get the numbers.
int index = s.IndexOf('h');
The rest is up to you, ... convert, enumerate and so on.
EDIT: There is a nice method that does the enumeration job for you: Enumerable.Range Good luck
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.
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 am using Microsoft Visual Developer C#. I am trying to validate a textbox so that the Book Code (int) entered is exactly 4 characters long. I used the Range Validator control to do so. For the MaximumValue and MinimumValue properties of the Range Validator I made them both equal 4. However this doesnt seem to work. Am I doing it wrong?
This is very simple, you should probably be thinking more about your problem before posting. However, I will empathize with a beginner and give a couple of solutions.
option 1 - convert to a string and check it's length;
string myVar = BookCode.ToString()
if (myVar.Length < 5)
// it's good!
else
// ERROR
option 2 - the largest value less than ten thousand is 9999, a four digit value.
if (BookCode < 10000)
// it's good
else
// it's bad
If you're just having users enter text into a text box and once they press some type of submit button you want to confirm that the text is 4 characters long then you can check that with inputControl.Text.Length == 4
from there you can show a message box and return if it's not equal to 4 or continue if it is.