Char Parsing newline (C#) - c#

the problem is simple but I can't figure out a way to overcome this.My applciation recieve a string (stringID) which is a list of IDs, either separated ";"+new line or just newline like :
ID1;
ID2;
ID3;
or
ID1
ID2
ID3
What im trying is to get a table with all those IDs ;
I tried :
string[] tabID = stringID.Split(';', char.Parse(Environment.NewLine));
And
string[] tabID = stringID.Split(';', '\r\n'));
string[] tabID = stringID.Split(';','\n');
nothing worked, can anyone help me ? thanks a lot

use the class StringReader and its method ReadLine to read each line individually.

The newline property is a string that can be one or two characters long, so use strings when you split. Use the RemoveEmptyEntries option, otherwise you will get the empty strings that are between the semicolon and the newlines.
string[] tabID = stringID.Split(
new string[] { ";", Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries
);

This should work:
string [] split = words.Split(new Char [] {';','\n'});
There is an overload for Split which takes a char[] of many separators.

Try:
String test = "ID1;ID2;ID3;";
String[] testarr = test.Split(';');

Try this:
string[] tabID = stringID.Split(new Char[] { ';', '\n', '\r'},
StringSplitOptions.RemoveEmptyEntries);

Related

Separate a string and place in array

How can I split this string and place in into an array can it be by a break line or new lines
This is the api url
https://stats.nba.com/stats/draftcombinenonstationaryshooting https://stats.nba.com/stats/draftcombinenonstationaryshooting https://stats.nba.com/stats/boxscoreplayertrackv2
I tried using but the string place it in index 0
string[] lines = split.Split(
new[] { Environment.NewLine },
StringSplitOptions.None
);
I would like to get each individual link from the string and store in an array
Source link: any-api.com
You can also call Split() without any arguments, in which case it will split on whitespace characters, so your sample code can simply be reduced to:
string[] lines = split.Split();
var array = urlString.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
This ensures there are no blank elements in array if URLs are separated by multiple spaces.
Here is a demo for you, you can try it.
string urlString = "https://stats.nba.com/stats/draftcombinenonstationaryshooting https://stats.nba.com/stats/draftcombinenonstationaryshooting https://stats.nba.com/stats/boxscoreplayertrackv2";
var array = urlString.Split(' ');
In your case, you can easily use the Regex like this:
string data = " https://stats.nba.com/stats/draftcombinenonstationaryshooting https://stats.nba.com/stats/draftcombinenonstationaryshooting https://stats.nba.com/stats/boxscoreplayertrackv2";
string regex = #"\s";
string[] result = Regex.Split(data.Trim(), regex);

How do I split a string on both commas and newlines in c#?

I'm trying to split a string that can come in with either commas or newlines, based on an input from a textarea. I'm not sure of the syntax to split this string in c#.
Currently I have:
string[] splitString = inputString.Split(','); //WORKS
//string[] splitString = inputString.Split(new string[] { ",","\r\n","\n" }, StringSplitOptions.None); //DOES NOT WORK
Since some text uses \r for new line.
You should use the code below and remove the empty entries to make the array cleaner.
string[] splitString = inputString.Split(new string[] { ",", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
or using Regex.Split. (This doesn't remove empty entries.)
String[] splitString = Regex.Split(inputString, "[,\r\n]");
Update
You can also use Regex.Split with empty entries removed, thanks to WiktorStribiżew's comment.
The code below removes the empty entries which aren't in the beginning or end of the string.
String[] splitString = Regex.Split(inputString, "[,\r\n]+");
To eliminate empty entries showing in the beginning or end of the line, use the code below.
Regex.Split(Regex.Replace(inputString, "^[,\r\n]+|[,\r\n]+$", ""), "[,\r\n]+");
Regular Expression Language
If you want more informations about Regex, or how it works, you can look here for a quick reference.
You can pass Environment.NewLine into your string array:
string[] splitString = inputString.Split(new string[] { ",", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

Split a string by word using one of any or all delimiters?

I may have just hit the point where i;m overthinking it, but I'm wondering: is there a way to designate a list of special characters that should all be considered delimiters, then splitting a string using that list? Example:
"battlestar.galactica-season 1"
should be returned as
battlestar galactica season 1
i'm thinking regex but i'm kinda flustered at the moment, been staring at it for too long.
EDIT:
Thanks guys for confirming my suspicion that i was overthinking it lol: here is what i ended up with:
//remove the delimiter
string[] tempString = fileTitle.Split(#"\/.-<>".ToCharArray());
fileTitle = "";
foreach (string part in tempString)
{
fileTitle += part + " ";
}
return fileTitle;
I suppose i could just replace delimiters with " " spaces as well... i will select an answer as soon as the timer is up!
The built-in String.Split method can take a collection of characters as delimiters.
string s = "battlestar.galactica-season 1";
string[] words = s.split('.', '-');
The standard split method does that for you. It takes an array of characters:
public string[] Split(
params char[] separator
)
You can just call an overload of split:
myString.Split(new char[] { '.', '-', ' ' }, StringSplitOptions.RemoveEmptyEntries);
The char array is a list of delimiters to split on.
"battlestar.galactica-season 1".Split(new string[] { ".", "-" }, StringSplitOptions.RemoveEmptyEntries);
This may not be complete but something like this.
string value = "battlestar.galactica-season 1"
char[] delimiters = new char[] { '\r', '\n', '.', '-' };
string[] parts = value.Split(delimiters,
StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < parts.Length; i++)
{
Console.WriteLine(parts[i]);
}
Are you trying to split the string (make multiple strings) or do you just want to replace the special characters with a space as your example might also suggest (make 1 altered string).
For the first option just see the other answers :)
If you want to replace you could use
string title = "battlestar.galactica-season 1".Replace('.', ' ').Replace('-', ' ');
For more information split with easy examples you may see following Url:
This also include split on words (multiple chars).
C# Split Function explained

split a string in to multiple strings

123\r\n456t\r\n789
How can i split the string above in to multiple strings based on the string text
.split has only over load that takes char :(
string.Split has supported an overload taking an array of string delimiters since .NET 2.0. For example:
string data = "123text456text789";
string[] delimiters = { "text" };
string[] pieces = data.Split(delimiters, StringSplitOptions.None);
use string.split("text"), hope it will help.
I believe you want to split 123, 456, 789 as you have \r\n after them.
Easiest way I see is
string textVal = "123\r\n456t\r\n789";
textVal = textVal.replace("\r", "").replace("\n",",");
string arrVal[] = textVal.split(',');
Now you have arrVal containing 123, 456, 789.
Happy coding
String.Split supports also an array of strings. In your case you can do:
string s = "123\r\n456t\r\n789";
string[] parts = s.Split(new string[] {"\r\n"}, StringSplitOptions.None);

Split string var

Let's say I have this string var:
string strData = "1|2|3|4||a|b|c|d"
Then, I make a Split:
string[] strNumbers = strData.Split("||"); //something like this, I know It's not this simple
I need two separate parts, each one containing this:
//strNumbers -> {"1","2","3","4"},{"a","b","c","d"}
So that after that, I could do this:
string[] strNumArray = strNumbers[0].Split('|');
//strNumArray -> '1', '2', '3', '4'
And same with the other part (letters).
Is it possible? to make this double split with the same character, but the first time the character is repeated twice?.
Thanks.
PD. I'm using C#.
It'll work fine, your syntax is just off.
So first, your declarations are off. You want the [] on the type, not the name.
Second, on String.Split, there is an overload that takes in a string array and a StringSplitOptions. Just trying to do "||" will call the param char overload, which is invalid.
So try this:
string strData = "1|2|3|4||a|b|c|d";
string[] strNumbers = strData.Split(new[] {"||"}, StringSplitOptions.None);
string[] strNumArray = strNumbers[0].Split('|');
You can change the StringSplitOptions to RemoveEmptyEntries if you wanted.
in .net 3.5:
string strData = "1|2|3|4||a|b|c|d";
var s1 = strData.Split(new string[] { "||" }, StringSplitOptions.None);
var numbers = s1[0].Split('|');
var letters = s1[1].Split('|');
Dim s As String = "1|2|3|4|5|6|7||a|b|c|d|e|f"
Dim nums() As String = s.Split(New String() {"||"}, StringSplitOptions.None)(0).Split("|")
Dim strs() As String = s.Split(New String() {"||"}, StringSplitOptions.None)(1).Split("|")

Categories

Resources