split string into an array not working - c#

I have a string like so 1500, 1500 and I am trying to split it into array like so:
string[] PretaxArray = Pretax.Split(", ");
but I get this error:
The best overloaded method match for 'string.Split(params char[])' has some invalid arguments
what am I doing wrong ?

String.Split has another overload
string Pretax = "1500, 1500";
string[] PretaxArray = Pretax.Split(new[] {", "}, StringSplitOptions.RemoveEmptyEntries);

You should try this:
string[] PretaxArray = Pretax.Split(',');
In the Split method we usually pass a character or an array of characters and not a string.
Actually, in order to be more precise, you could pass an array of strings. But this is not your case - I assume that from the code you have posted.
You could take a look here, where all the overloaded versions of Split method are aggregated.

Related

Ignore specific occurrence of string while replacing

I'm trying to parse a database and replace ":" with another delimiter such as "+delimiter+". The problem with this is this specific database has a hash:salt combo. Some of the salts contain extra ":"'s and some don't. How would I go about replacing all ":"'s and ignoring the ones in the hashes
Example:
1:john:john#john.com:127.0.0.1:341b4d30d4f5bb31f291633e0c97a8ba:J:|
I want to ignore the colons in:
341b4d30d4f5bb31f291633e0c97a8ba:J:|
But I want to replace the other colons with "+delimiter+"
If your string will always be in the format you've specified:
1:john:john#john.com:127.0.0.1:341b4d30d4f5bb31f291633e0c97a8ba:J:|
You can use the String.Split(Char[], Int32) overload to specify the maximum number of substrings returned. Specify 5 substrings and the final substring will contain the remainder of the input string i.e. the hashed field.
string input = "1:john:john#john.com:127.0.0.1:341b4d30d4f5bb31f291633e0c97a8ba:J:|";
string[] array = input.Split(new char[] { ':' }, 5);
You can then use the String.Join method to concatenate the string array with the desired separator.
string output = String.Join("+delimiter+", array);
The String.Split() method takes a count as its second argument:
string input = "1:john:john#john.com:127.0.0.1:341b4d30d4f5bb31f291633e0c97a8ba:J:|";
string[] fields = input.Split(new[]{ ':' }, 5);
The string fields[4] now holds the value 341b4d30d4f5bb31f291633e0c97a8ba:J:|
To complete the replace operation, apply String.Join to concatenate the strings using the new delimiter:
string result = String.Join("+delimiter+", fields);

Get a specific value from comma separated string

I have a instrument which returns me a string value as this
string var = "0,0,0.1987,22.13";
I want to use only the 4th value "22.13". I tried with Trim() and Replace() methods but couldn't exactly get what I want. How to achieve this?
The Split method is the best to use here!
The Split method does what it says on the lid. It splits a string into different parts at a character separator that you specify. e.g. "Hello World".Split(' ') splits the string into a string[] that contains {"Hello", "World"}.
So in this case, you can split the string by ',':
var.Split (',') //Use single quotes! It's of type char
If you want to access the fourth item, use an indexer! (of course)
var.Split(',')[3] //Remember to use 3 instead of 4! It's zero based!
Now you can happily store the string in some variable
string fourthThing = var.Split (',')[3];
Additional Information:
The Split method also has a overload that takes a char and a StringSplitOptions. If you do something like this:
var.Split (',', StringSplitOptions.RemoveEmptyEntries)
Empty entries will be automatically removed! How good!
More information: https://msdn.microsoft.com/en-us/library/system.string.split(v=vs.110).aspx
You can use string Split method
string data = "0,0,0.1987,22.13";
string value = data.Split(',')[3];
To pick up the last element, you can use Split function and Linq as below
string GetLastElement(string data) {
return data.Split(',').Last();
}

Select a part of string when find a character

I need to select a part of a string ,suppose i have a string like this :Hello::Hi,
I use this characters :: as a separator, so i need to separate Hello and Hi.I am using C# application form .
I googled it ,i found something like substring but it didn't help me.
Best regards
string.Split is the right method, but the syntax is a little tricky when splitting based on a string versus a character.
The overload to split on a string takes the input as an array of strings so it can be distinquished from the overload that takes an array of characters (since a string can be easily cast to an array of characters), and adds a parameter for StringSplitEntries, which you can set to None to use the default option (include "empty" entries):
string source = "Hello::Hi";
string[] splits = source.Split(new string[] {"::"}, StringSplitOptions.None);
You can split a string into multiple parts based on a semaphore using the Split function:
var stringToSearch = "Hello::Hi";
var foundItems = stringToSearch.Split(new[] {"::"},
StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < foundItems.Count(); i++)
{
Console.WriteLine("Item #{0}: {1}", i + 1, foundItems[i]);
}
// Ouput:
// Item #1: Hello
// Item #2: Hi

Split String on the Basis of string having character as well as special character

I want to split my String which I am getting in format::
string filter=" Status~contains~''~and~DossierID~eq~40950~and~CustomerName~eq~'temp'"
I want to split it with ("~and~")
I am doing something like ::
var test=filter.Split("~and~");
But getting Exception.
You're not getting an exception; this won't even compile.
The .Split() method doesn't accept a string, only an array of strings.
Try this instead:
var test = filter.Split(new[] {"~and~"}, StringSplitOptions.None);
You should get a list of three strings back:
Status~contains~''
DossierID~eq~40950
CustomerName~eq~'temp'
string filter = " Status~contains~''~and~DossierID~eq~40950~and~CustomerName~eq~'temp'";
string[] tokens = Regex.Split(filter, "~and~");
foreach (string token in tokens)
{
//Do stuff with token here
}
Bear in mind that you need to import System.Text.RegularExpressions in order to use Regex. You could use the string method to split, but I prefer the Regex one, since it takes a string insted of a char[].

String.Split with a String?

I have what is probably a very simple question.
I want to do a classic String.Split(), but with a string, not a character. Like string.Split("word") and get back an array just like if I had done string.Split('x').
You can use String.Split(string[], StringSplitOptions options).
The code will look like:
var results = theString.Split(new[] {"word"}, StringSplitOptions.None);
There is an available function overload on string.Split but it takes an array and an enum.
string test = "1Test2";
string[] results = test.Split(new string[] { "Test" }, StringSplitOptions.None);
Will yield an array containing "1" and "2".

Categories

Resources