How to split a string into multiple strings in c#? [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Split String into smaller Strings by length variable
I have seen solutions that split strings by a certain character but I wanted to seperate a string every certain amount of characters. Does anyone know how to do this?

its been asked
Split String into smaller Strings by length variable

Related

C# Break Line of Text considering Uppercase and Lowercase [duplicate]

This question already has answers here:
Calculate the display width of a string in C#?
(6 answers)
Measure a String without using a Graphics object?
(5 answers)
How can I convert a string length to a pixel unit?
(6 answers)
Determining text width
(2 answers)
Closed 2 years ago.
I'm making in Unity a Dialog Box with Public Variables. I put the Texts and the Dialog Box Typed in sequence.
I wanna just put the Text and make the code break lines automically, and the problem is that I can't just set a Limit of Characters, cause exist a big difference if I try to use a lot of Uppercase Letters or just a little
I need a help to Break Line for Occupied Space, and not a Limit of Characters
Sorry if I wrote a confused question, I'm trying to learn more english ^^

Can't get 2 different Substrings from a String C# [duplicate]

This question already has answers here:
split a string on newlines in .NET
(17 answers)
Closed 4 years ago.
I have a task.
I should extract two substrings from a string.
The lenght of the String will be different each time, so the method should be generic.
Suppose I have the following String:
/*Description:\r\n*RANGE:\r\n*HIGH\r\n*LOW\r\n*/
I need to get the substring1= HIGH and substring2= LOW.
The substring1 and substring2 will be all the time between \r\n, but they values will be different.
I would be very grateful if anybody helps me. It can be a pseudocode, anything.
Thanks in advance.
UPDATE1: I'm searching first for "RANGE:\r\n*" and get the index of the character * and the index og character "H". But next don't know how to get the whole substring.
If the pattern you've provided is similar to what you'd expect all the time, a stupid simple approach would be:
public static string[] GetParts(string input)
{
string[] parts= input.Split("\r\n", StringSplitOptions.RemoveEmptyEntries);
return parts.TakeLast(2).Select(item=>item.Replace("*", string.Empty)).ToArray();
}
Note: This is not a production quality code.

Locating one of two characters in a string using indexOf [duplicate]

This question already has answers here:
Check if a string contains one of 10 characters
(6 answers)
Closed 2 years ago.
I have this array:
char[] signOfDecimal = {',', '.'};
and I'm trying to find the location of one of these 2 characters in my string:
locationOfDotInRate = myString.IndexOf(signOfDecimal[0], signOfDecimal[1]);
Basically I'm trying to check where one of these characters might occur in my string, but its not working. The reason I'm doing it this way is because my string sometimes contain '.' or ','.
You can use IndexOfAny method
locationOfDotInRate = myString.IndexOfAny(signOfDecimal);

Why string.format? [duplicate]

This question already has answers here:
String output: format or concat in C#?
(32 answers)
Why use String.Format? [duplicate]
(7 answers)
Closed 9 years ago.
Why shouldn't we simply use
string s=product.Name+" has been saved";
instead of:
string s=string.Format("{0} has been saved", product.Name);
One naive reason would be that it helps to prevent exactly the string formatting issue that you've presented in your original (unedited) question i.e.
string s=product.Name+"has been saved";
requires an extra space. The format method aids readability.
You could do that, no one say that you cannot. But mainly for readability, the second approach is prefered. It's even more obvious as soon as you concat more than 2 strings, it gets really messy, hard to read and mantain.
If you have many strings that you want to add, each + operation create new string.
For adding many strings you can use StringBuilder Class or String.Format

What is the maximum size for a string in C#? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What is the maximum possible length of a .NET string?
I need to know how many characters a string can contain in C#?
Is it the same as the max length of a char array?
String.Length is an int so the its size is 2,147,483,647. But consider using StringBuilder if you find yourself asking this question !

Categories