How can i split the string only once using C# - c#

Example : a - b - c must be split as
a and b - c, instead of 3 substrings

Specify the maximum number of items that you want:
string[] splitted = text.Split(new string[]{" - "}, 2, StringSplitOptions.None);

string s = "a - b - c";
string[] parts = s.Split(new char[] { '-' }, 2);
// note, you'll still need to trim off any whitespace

"a-b-c".Split( new char[] { '-' }, 2 );

You could use indexOf() to find the first instance of the character you want to split with, then substring() to get the two aspects. For example...
int pos = myString.IndexOf('-');
string first = myString.Substring(0, pos);
string second = myString.Substring(pos);
This is a rough example - you'll need to play with it if you don't want the separator character in there - but you should get the idea from this.

string[] splitted = "a - b - c".Split(new char[]{' ', '-'}, 2, StringSplitOptions.RemoveEmptyEntries);

var str = "a-b-c";
int splitPos = str.IndexOf('-');
string[] split = { str.Remove(splitPos), str.Substring(splitPos + 1) };

I have joined late and many of above answers are matched with my following words:
string has its own
Split
You can use the same to find the solution of your problem, following is the example as per your issue:
using System;
public class Program
{
public static void Main()
{
var PrimaryString = "a - b - c";
var strPrimary = PrimaryString.Split( new char[] { '-' }, 2 );
Console.WriteLine("First:{0}, Second:{1}",strPrimary[0],strPrimary[1]);
}
}
Output:
First:a , Second: b - c

Related

How to separate string after whitespace in c#

I'm using c# and have a string like x="12 $Math A Level$"` that could be also x="12 Math A Level"
How can I separate this string in order to have a variable year=12 and subject=Math A Level?
I was using something like:
char[] whitespace = new char[] { ' ', '\t' };
var x = item.Split(whitespace);
but then I didn't know what to do after or if there's a better way to do this.
You could use the override of split that takes the count :
var examples = new []{"2 $Math A Level$", "<some_num> <some text>"} ;
foreach(var s in examples)
{
var parts = s.Split(' ', count: 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
Console.WriteLine($"'{parts[0]}', '{parts[1]}'");
}
This prints:
'2', '$Math A Level$'
'<some_num>', '<some text>'
You could do
var item = "12 Math A Level";
var index = item.IndexOf(' ');
var year = item.Substring(0, index);
var subject = item.Substring(index + 1, item.Length - index-1).Trim('$');
This assumes that the year is the first word, and the subject is everything else. It also assumes you are not interested in any '$' signs. You might also want to add a check that the index was actually found, in case there are no spaces in the string.
To add a Regex-based answer:
using System;
using System.Text.RegularExpressions;
public class Program
{
public static readonly Regex regex = new Regex(#"(?<ID>[0-9]+)\s+[$]?(?<Text>[^$]*)[$]?", RegexOptions.Compiled);
public static void Main()
{
MatchCollection matches = regex.Matches("12 $Math A Level$");
foreach( Match m in matches )
{
Console.WriteLine($"{(m.Groups["ID"].Value)} | {(m.Groups["Text"].Value)}");
}
matches = regex.Matches("13 Math B Level");
foreach( Match m in matches )
{
Console.WriteLine($"{(m.Groups["ID"].Value)} | {(m.Groups["Text"].Value)}");
}
}
}
In action: https://dotnetfiddle.net/6XEQw8
Output:
12 | Math A Level
13 | Math B Level
To explain the expression:
(?[0-9]+)\s+[$]?(?[^$]*)[$]?
(?[0-9]+) - Named Catpure-Group "ID"
[0-9] - Match literal chars '0' to '9'
+ - ^^ One or more times
\s+ - Match whitespace one or more times
[$]? - Match literal '$' one or zero times
(?[^$]*) - Named Capture-Group "Text"
[^$] - Match anything that is _not_ literal '$'
* - ^^ Zero or more times
[$]? - Match literal '$' one or zero times
See also https://regex101.com/r/WV366l/1
Mind: I personally would benchmark this solution against a (or several) non-regex solutions and then make a choice.
var x = "12 $Math A Level$".Split('$', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
string year = x[0];
string subject = x[1];
Console.WriteLine(year);
Console.WriteLine(subject);
If you can rely on the string format specified ("12 $Math A Level$"), you could split with at $ like this:
using System;
public class Program
{
public static void Main()
{
var sample = "12 $Math A Level$";
var rec = Parse(sample);
Console.WriteLine($"Year={rec.Year}\nSubject={rec.Subject}");
}
private static Record Parse(string value)
{
var delimiter = new char[] { '$' };
var parts = value.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
return new Record { Year = Convert.ToInt32(parts[0]), Subject = parts[1] };
}
public class Record
{
public int Year { get; set; }
public string Subject { get; set; }
}
}
Output:
Year=12
Subject=Math A Level
▶️ Try it out here: https://dotnetfiddle.net/DAFLjA

C# parsing for lot number hidden with a string of chars [duplicate]

What if I want to split a string using a delimiter that is a word?
For example, This is a sentence.
I want to split on is and get This and a sentence.
In Java, I can send in a string as a delimiter, but how do I accomplish this in C#?
http://msdn.microsoft.com/en-us/library/system.string.split.aspx
Example from the docs:
string source = "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]";
string[] stringSeparators = new string[] {"[stop]"};
string[] result;
// ...
result = source.Split(stringSeparators, StringSplitOptions.None);
foreach (string s in result)
{
Console.Write("'{0}' ", String.IsNullOrEmpty(s) ? "<>" : s);
}
You can use the Regex.Split method, something like this:
Regex regex = new Regex(#"\bis\b");
string[] substrings = regex.Split("This is a sentence");
foreach (string match in substrings)
{
Console.WriteLine("'{0}'", match);
}
Edit: This satisfies the example you gave. Note that an ordinary String.Split will also split on the "is" at the end of the word "This", hence why I used the Regex method and included the word boundaries around the "is". Note, however, that if you just wrote this example in error, then String.Split will probably suffice.
Based on existing responses on this post, this simplify the implementation :)
namespace System
{
public static class BaseTypesExtensions
{
/// <summary>
/// Just a simple wrapper to simplify the process of splitting a string using another string as a separator
/// </summary>
/// <param name="s"></param>
/// <param name="pattern"></param>
/// <returns></returns>
public static string[] Split(this string s, string separator)
{
return s.Split(new string[] { separator }, StringSplitOptions.None);
}
}
}
string s = "This is a sentence.";
string[] res = s.Split(new string[]{ " is " }, StringSplitOptions.None);
for(int i=0; i<res.length; i++)
Console.Write(res[i]);
EDIT: The "is" is padded on both sides with spaces in the array in order to preserve the fact that you only want the word "is" removed from the sentence and the word "this" to remain intact.
...In short:
string[] arr = "This is a sentence".Split(new string[] { "is" }, StringSplitOptions.None);
Or use this code; ( same : new String[] )
.Split(new[] { "Test Test" }, StringSplitOptions.None)
You can use String.Replace() to replace your desired split string with a character that does not occur in the string and then use String.Split on that character to split the resultant string for the same effect.
Here is an extension function to do the split with a string separator:
public static string[] Split(this string value, string seperator)
{
return value.Split(new string[] { seperator }, StringSplitOptions.None);
}
Example of usage:
string mystring = "one[split on me]two[split on me]three[split on me]four";
var splitStrings = mystring.Split("[split on me]");
var dict = File.ReadLines("test.txt")
.Where(line => !string.IsNullOrWhitespace(line))
.Select(line => line.Split(new char[] { '=' }, 2, 0))
.ToDictionary(parts => parts[0], parts => parts[1]);
or
enter code here
line="to=xxx#gmail.com=yyy#yahoo.co.in";
string[] tokens = line.Split(new char[] { '=' }, 2, 0);
ans:
tokens[0]=to
token[1]=xxx#gmail.com=yyy#yahoo.co.in
string strData = "This is much easier"
int intDelimiterIndx = strData.IndexOf("is");
int intDelimiterLength = "is".Length;
str1 = strData.Substring(0, intDelimiterIndx);
str2 = strData.Substring(intDelimiterIndx + intDelimiterLength, strData.Length - (intDelimiterIndx + intDelimiterLength));

How to sort characters in a string containing spaces by keeping the spaces

Input String may look like this :
"2 4 8 6" or "a b z g h"
Output String should look like this:
"2 4 6 8" and "a b g h z"
I'm trying to find an answer using C# LINQ and any other simple methods will be fine.
As a beginner I went through this question in this link : Is there a simple way that I can sort characters in a string in alphabetical order
However, I'm unable to sort the array properly in the order I wish it to be.
Edit : Please note that number of spaces in between the characters will be 1. If possible please consider this test case "2 15 3" as well.
string text = "a b z g h";
string[] textWithoutSpaces = text.Split(new[]{' '},
StringSplitOptions.RemoveEmptyEntries);
Array.Sort(textWithoutSpaces);
string result = String.Join(" ", textWithoutSpaces);
Split your text at the spaces. Then order the resulting values and join them together with a space:
var result = string.Join(" ", text.Split(' ').OrderBy(v => v));
Try split input string and sort it:
IEnumerable<string> sequence = "a b z g h".Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).OrderBy(p => p);
Another possibility:
var res = "1 2 3 4 5".Split(' ')
.OrderBy(x=>x)
.Aggregate((r,x) => r +" "+ x)
Using LINQ
var input1 = "2 4 8 6";
var input2 = "a b z g h";
var input3 = "2 4 8 6 a b z g h";
var output = input3.Split(' ').OrderBy(x => x).ToList();
foreach (var sortedChars in output)
{
Console.WriteLine(sortedChars);
}
Using built-in Array.Sort:
var arr = input3.Split(' ');
Array.Sort(arr);
foreach (var s in arr)
{
Console.WriteLine(s);
}

Extract table name from schema and table name

I'm trying to get the table name from a string that is in the format:
[schemaname].[tablename]
I think this can be done with split but not sure how to handle the trailing ] character.
A simple approach is using String.Split and String.Trim in this little LINQ query:
string input = "[schemaname].[tablename]";
string[] schemaAndTable = input.Split('.')
.Select(t => t.Trim('[', ']'))
.ToArray();
string schema = schemaAndTable[0];
string table = schemaAndTable[1];
Another one using IndexOf and Substring:
int pointIndex = input.IndexOf('.');
if(pointIndex >= 0)
{
string schema = input.Substring(0, pointIndex).Trim('[', ']');
string table = input.Substring(pointIndex + 1).Trim('[', ']');
}
//find the seperator
var pos = str.IndexOf('].[');
if (pos == -1)
return null; //sorry, can't be found.
//copy everything from the find position, but ignore ].[
// and also ignore the last ]
var tableName = str.Substr(pos + 3, str.Length - pos - 4);
Just to be the different here is another version with regex;
var result = Regex.Match(s, #"(?<=\.\[)\w+").Value;
Split by 3 characters. i.e [.] with option RemoveEmptyEntries that is pretty self explanatory.
var result = input.Split(new [] {'[','.',']'}, StringSplitOptions.RemoveEmptyEntries);
Try this:
var tableAndSchema = "[schemaname].[tablename]";
var tableName = tableAndSchema
.Split('.')[1]
.TrimStart('[')
.TrimEnd(']');
Split will split the string on the . character and turn it into an array of two strings:
[0] = "[schemaname]"
[1] = "[tablename]"
The second (index 1) element is the one you want. TrimStart and TrimEnd will remove the starting and ending brackets.
Another way to do this is with Regular Expressions:
var tableAndSchema = "[schemaname].[tablename]";
var regex = new Regex(#"\[.*\].\[(.*)\]");
var tableName = regex.Match(tableAndSchema).Groups[1];
The regex pattern \[.*\].\[(.*)\] creates a capture group for the characters within the second pair of brackets and lets you easily pull them out.
var res = input.Split('.')[1].Trim('[', ']');
Another LINQ solution:
var tableName = String.Join("", input.SkipWhile(c => c != '.').Skip(1)
.Where(c => Char.IsLetter(c)));

How to keep the delimiters of Regex.Split?

I'd like to split a string using the Split function in the Regex class. The problem is that it removes the delimiters and I'd like to keep them. Preferably as separate elements in the splitee.
According to other discussions that I've found, there are only inconvenient ways to achieve that.
Any suggestions?
Just put the pattern into a capture-group, and the matches will also be included in the result.
string[] result = Regex.Split("123.456.789", #"(\.)");
Result:
{ "123", ".", "456", ".", "789" }
This also works for many other languages:
JavaScript: "123.456.789".split(/(\.)/g)
Python: re.split(r"(\.)", "123.456.789")
Perl: split(/(\.)/g, "123.456.789")
(Not Java though)
Use Matches to find the separators in the string, then get the values and the separators.
Example:
string input = "asdf,asdf;asdf.asdf,asdf,asdf";
var values = new List<string>();
int pos = 0;
foreach (Match m in Regex.Matches(input, "[,.;]")) {
values.Add(input.Substring(pos, m.Index - pos));
values.Add(m.Value);
pos = m.Index + m.Length;
}
values.Add(input.Substring(pos));
Say that input is "abc1defg2hi3jkl" and regex is to pick out digits.
String input = "abc1defg2hi3jkl";
var parts = Regex.Matches(input, #"\d+|\D+")
.Cast<Match>()
.Select(m => m.Value)
.ToList();
Parts would be: abc 1 defg 2 hi 3 jkl
For Java:
Arrays.stream("123.456.789".split("(?<=\\.)|(?=\\.)+"))
.forEach((p) -> {
System.out.println(p);
});
outputs:
123
.
456
.
789
inspired from this post (How to split string but keep delimiters in java?)
Add them back:
string[] Parts = "A,B,C,D,E".Split(',');
string[] Parts2 = new string[Parts.Length * 2 - 1];
for (int i = 0; i < Parts.Length; i++)
{
Parts2[i * 2] = Parts[i];
if (i < Parts.Length - 1)
Parts2[i * 2 + 1] = ",";
}
for c#:
Split paragraph to sentance keeping the delimiters
sentance is splited by . or ? or ! followed by one space (otherwise if there any mail id in sentance it will be splitted)
string data="first. second! third? ";
Regex delimiter = new Regex("(?<=[.?!] )"); //there is a space between ] and )
string[] afterRegex=delimiter.Split(data);
Result
first.
second!
third?

Categories

Resources