This question already has answers here:
How to split a string by a specific character? [duplicate]
(6 answers)
Closed 10 months ago.
I want to add ' char as delimiter to my string array but I have error telling conversion string to char impossible.
I see this How to split a string by a specific character?
string ineedtosplitthis = "i want 'to' split this";
string[] splitTest = ineedtosplitthis.Split("'").ToString();
The issues is that you have the .ToString() after your statement.
Change it to this:
string ineedtosplitthis = "i want 'to' split this";
string[] splitTest = ineedtosplitthis.Split('\'');
The reason why this code doesn't work:
string[] splitTest = ineedtosplitthis.Split("'").ToString();
Is because you are converting it to a string via ToString();, while assigning it to a string[].
Split(); already returns a string[], so there is no need to convert it.
Hope this explanation helped :)
Related
This question already has answers here:
split a string on newlines in .NET
(17 answers)
Closed 1 year ago.
I need to convert string to IEnumerable<string> by every newline character.
I just want the same functionality as File.ReadLines without loading any file, rather taking it from a previously loaded string.
String.Split can split your string and give you string[]
Usage
string someString = ...;
string[] lines = someString.Split('\n');
IEnumerable<string> linesAsEnumerable = lines;
DoSomethingWithLines(linesAsEnumerable);
You could split it according to the newline character:
String[] lines = fileContenets.Split('\n');
This question already has answers here:
C#: splitting a string and not returning empty string
(7 answers)
Closed 2 years ago.
I have a string that contains email addresses like this :
string emailIDs = "xyz#gmail.com;abc#yahoo.com;frog#gmail.com;whyme#hotmail.com;"
I want to add these emails in List.
I tried splitting it at ';' but this creates an extra record in the list which causes a null exception.
I want to split and add this to my List without any extra record. Please let me know how can I achieve this in c#?
I am trying like this:
List<string> To = new List<string>(emailIDs.Cc.Split(new[] { ";" },StringSplitOptions.RemoveEmptyEntries));
Following is the result:
I am trying to remove this extra record at the end be cause Cc contains only two records:
Use StringSplitOptions.RemoveEmptyEntries to get rid of blank entries when splitting.
See documentation: https://learn.microsoft.com/en-us/dotnet/api/system.stringsplitoptions?view=netframework-4.8
Example:
var list = emailIDs.Split(new[] {';'}, StringSplitOptions.RemoveEmptyEntries);
Edit:
I think that the string you posted "xyz#gmail.com;abc#yahoo.com;frog#gmail.com;whyme#hotmail.com;" does not match the actual string you are passing to String.Split() method. Looking closely at the end of email.Cc, it contains whitespace at the end, generating a non-empty but blank string that will not be removed by StringSplitOptions.RemoveEmptyEntries.
What about trimming that whitespace before?
var list = emailIDs.TrimEnd().Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
^^^^^^^^^
This question already has answers here:
Best way to specify whitespace in a String.Split operation
(11 answers)
Closed 4 years ago.
In java,
String.split("");
is possible.
ex)
String[] str = "Hello world!".split("")
Like this, I want to split String without escape sequence.
But in C#, I tried it and IDE said 'Error'. Is there any solution?
edit)
Code:
String[] str = "Hello world!".split("");
the result is str[0] = H, str[1] = e, ...(in java)
In C#, I tried
strI[i] = "Hello World!".Split('');
And the result is
'error CS1011: Empty character literal
I want to split string with Empty literal.
Please note that no overloaded methods of String.Split() accepts a string as first argument like what you used. Following are the possible overloaded methods
The thing you are looking for is to split a string into characters. For that you can depend on String.ToCharArray Method, which will Copies the characters in this instance to a Unicode character array.:
char[] charArray = "Hello world!".ToCharArray();
So that we can access each characters using its index, that means - charArray[0] will be H.
If you need the splitted characters into a string array means you can use something like this:
string[] strArray = "Hello world!".Select(x=> x.ToString())
.ToArray();
The reason you are getting an error is because there is no method with the signature matching your call. In other words, when you call Split method, you must pass the first parameter as a character array or string array, whereas you are passing the first argument as simply a string.
Following call would not throw any error since it matches the signature of Split method.
string[] sArray = s.Split(new string[] {""}, StringSplitOptions.None);
However, above method will result in nothing productive since the resulting array will contain only one element whose value will be original string.
If your goal was to split a string into individual characters then you can use code like below.
string s = "some string";
var splitString = s.ToCharArray();
This question already has answers here:
Replacing all the '\' chars to '/' with C#
(7 answers)
Closed 6 years ago.
In textbox1 user enters string "Test\u0021Test" and I would like to convert escaped character "\u0021" to "!"
string x = "Test\u0021Test"; // this is easy
string y = textbox1.Text; // here textbox1.Text = "Test\u0021Test" and this I don't know how to convert
Thanks for help
EDIT
Answered by #Simo Erkinheimo
Allow user to enter escape characters in a TextBox
Use string Replace method this case
string x = "Test\u0021Test"; // this is easy
string y = textbox1.Text.Replace("\u0021", "!");
You can do something like this. It will work for all unicode escaped symbols in input string.
var result = Regex.Replace(x, #"\\[u]([0-9a-f]{4})",
match => char.ToString(
(char)ushort.Parse(match.Groups[1].Value, NumberStyles.AllowHexSpecifier)));
This question already has an answer here:
Splitting the array with / slash
(1 answer)
Closed 7 years ago.
I have a string which includes integers between slashes.
For example:
string myString = "/1//2//56//21/";
I need to take these integers and add them into a list. How can I split this string to its integers?
string myString = "/1//2//56//21/";
int[] arrayInt = Regex.Split(myString, "/+").Where(s => !String.IsNullOrWhiteSpace(s)).Select(Int32.Parse).ToArray();