C# Trim charaters from the end of string - c#

I have this string
"1.3.1.\tProduction and Sales Analysis:"
I want to trim numbers and escape sequences from the start and end of string.
Output Should be :
"Production and Sales Analysis:"
My code :
Char[] trimArray = new Char[] {'0','1','2','3','4','5','6','7','8','9','.',',',':','\\','/'};
String test = "1.3.1.\tProduction and Sales Analysis:";
test = test.TrimEnd(trimArray);
but problem is when a string like 23232-232123-asd-323 comes it also removes the digits
I want to remove the unwanted characters from start and end of string But Want to keep the string like 23232-232123-asd-323 or mobile numbers
Thanks.

Is there always '\t' in the middle?
You can try to split by '\t' and trim end.
Split(new char[]{'\t'});
If colon is always at the end of what you want to get you can split again ;)

If there is something in common separating the bad number and the good numbers like say "t", then how about finding the index of the common letter/symbol then creating a substring of everything after. For example:
String test = "1.3.1.\tProduction and Sales Analysis:";
index = test.LastIndexOfAny(new char[] { 't' });
test = test.Substring(index +1);
This should give you "Production and Sales Analysis:". You could do the same for the ":" except you want everything before it like so
int index = test.LastIndexOfAny(new char[] { ':' });
test = test.Substring(0, index);

With regex:
using System.Text.RegularExpressions;
string input = "1.3.1.\tProduction and Sales Analysis:";
string rgx = #"(?:[0-9.]+)*(?:\\[a-zA-Z])*([\w\s\.\:-_]+)(?:\\[a-zA-Z])*";
string result = Regex.Match(input, rgx).Groups[1].Value.TrimStart();

Related

My Regex.Split with '\n' takes up two spaces instead of 1

I need to split my text into each word, space, and new line.
Although the words and spaces are properly working, the \n is taking up two spaces only if it's not after a word.
Example: "\nTest\nword", here, the first \n takes up two spaces while the second one takes up one.
How would I write the proper regex?
My code:
string delimiterChars = "([ \r\n])";
wordArray = Regex.Split(myTexy, delimiterChars);
For context, I am using Unity.
Input: enter image description here
Output: enter image description here
On the output of the picture: The first element is empty and the second is \n here. I don't want the empty element.
Regex.Split will always produce empty items where the matches are consecutive, or when they are at the start/end of string.
Instead, you can use a matching and extracting approach:
string delimiterChars = "[^ \r\n]+|[ \r\n]";
string[] wordArray = Regex.Matches(myTexy, delimiterChars)
.Cast<Match>()
.Select(m => m.Value)
.ToArray();
The [^ \r\n]+|[ \r\n] regex matches one or more chars other than a space, CR and LF, or a space, CR or an LF char.
You can use regular expressions to remove leading delimiter characters.
var myTexy = "\nTest\nword";
string delimiterChars = "([ \r\n])";
myTexy = Regex.Replace(myTexy, "^" + delimiterChars, "");
var wordArray = Regex.Split(myTexy, delimiterChars);
The "^" regex option says only look for these characters at the beginning of the string.
Also, just so you are aware the behavior you are seeing is intended and is documented here:
If a match is found at the beginning or the end of the input string,
an empty string is included at the beginning or the end of the
returned array.
Let me know if this is what you are looking for -
String text = "\nTest\nword";
string[] words = Regex.Split(text, #"(\n+)");
Output -
Try this :-
string myStr = "This is test text";
wordArray = myStr.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
Output:

Split string with character :

I have a string which is ant: man : jack.
I want to split the string from :so that I get man : jack as the output.
Note: The string should split from the first occurrence of the : character. how can I do this ?
What I tried works, But I need another method to produce this result.
var.Substring(var.IndexOf(':') + 1);
string[] split = var.Split(new char[] { ':' }, 2);
split[1] = split[1].TrimStart();
We could try doing a regex replace:
string input = "ant: man : jack";
string s = Regex.Replace(input, #"^[^:]+\s*:\s*", "");
Console.WriteLine(s);
man : jack
But, I think that splitting actually would scale better given your input string.
You are interested in only the substring after colon, so you want to drop the characters before, you need to use skipWhile:
string test = "ant:man:jack";
var results = test.SkipWhile(t => t != ':').Skip(0).ToList();
Remember skipWhile will skip characters till the predicate is true, that means at first instance of : it will return a list of chars.

Get everything before dot or comma c#

how can I get a substring of everything before dot or comma?
For example:
string input = "2.1";
int charLocation = text.IndexOf(".", StringComparison.Ordinal);
string test = input.Substring(0, charLocation );
but what if I have an input = "2,1" ?
I would like to do it in one method, not using twice a substring (once for dot and once for comma)?
string test = input.Split(new Char[] { ',', '.' })[0];
This will split the string for either comma or period...
input.Split(',','.');
Use the IndexOfAny function. It allows you to specify a list of characters to look for, rather than just a single character. You could then make a substring up to the return value of that function.
e.g.
char[] chars = { '.', ',' }
String out = s.Substring(0,s.IndexOfAny(chars));

C# Coding using strings in order to find a full name

I am using Visual studio to create a C# windows form that helps me find the suffix,first and last name of the user. I am using string.split to find the first space and split from there but it only gives me from the first space onward. if the user input " Mr. Donald duck " I can not manage to make it work in the situation.
"Mr. -5 spaces- Donald -5spaces- Duck"
the code doesn't read past the first space.
any suggestions?
Trimming is only going to take care of leading and trailing white-space characters. Here's what you need in order to get just the 3 useful parts of the text when you have all those extra spaces between words:
string name = "Mr. Donald Duck";
string[] split = name.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
The string array will contain 3 items: Mr., Donald, and Duck. The StringSplitOptions.RemoveEmptyEntries will take care of repeating white-space when you split the original string.
Without it, you get something like this: Mr., , , , , Donald, , , , , Duck
You should always use String.Trim() function. (To remove leading and trailing white-space from string) when you deal with user input as a string.
string s = " Mr. Donald duck ";
// Split string on spaces.
// ... This will separate all the words.
string[] words = s.Trim().Split(' ');
//.....check size of array.
if(words.Length ==3)
{
string suffix=words[0];
string firstname=words[1];
string lastname=words[2];
}
I am not getting -5 in your question but hope this will help.
Split with remove empty string option, then you will get non empty word array as result. From that you can get name parts.
Demo
The Syntax for String.Split would be like this:
// 0 1 2
// ooo|oooooo|oooo
string str = "Mr. Donald Duck";
string suffix = str.Split(' ')[0];
string fname = str.Split(' ')[1];
string lname = str.Split(' ')[0];
Just for explanation
According to MSDN You can easily remove white spaces from both ends of a string by using the String.Trim method. You can read it here. For more good understanding you can visit here
string input = Console.ReadLine();
// This will remove white spaces from both ends and split on the basis of spaces in string.
string[] tokens = input.Trim().Split(' ');
string title = tokens[0];
string firstname = tokens[1];
string secondname = tokens[2];

Splitting on “,” but not “/,”

Question: How do I write an expression to split a string on ',' but not '/,'? Later I'll want to replace '/,' with ', '.
Details...
Delimiter: ','
Skip Char: '/'
Example input: "Mister,Bill,is,made,of/,clay"
I want to split this input into an array: {"Mister", "Bill", "is", "made", "of, clay"}
I know how to do this with a char prev, cur; and some indexers, but that seems beta.
Java Regex has a split functionality, but I don't know how to replicate this behavior in C#.
Note: This isn't a duplicate question, this is the same question but for a different language.
I believe you're looking for a negative lookbehind:
var regex = new Regex("(?<!/),");
var result = regex.Split(str);
this will split str on all commas that are not preceded by a slash. If you want to keep the '/,' in the string then this will work for you.
Since you said that you wanted to split the string and later replace the '/,' with ', ', you'll want to do the above first then you can iterate over the result and replace the strings like so:
var replacedResult = result.Select(s => s.Replace("/,", ", ");
string s = "Mister,Bill,is,made,of/,clay";
var arr = s.Replace("/,"," ").Split(',');
result : {"Mister", "Bill", "is", "made", "of clay"}
Using Regex:
var result = Regex.Split("Mister,Bill,is,made,of/,clay", "(?<=[^/]),");
Just use a Replace to remove the commas from your string :
s.Replace("/,", "//").Split(',').Select(x => x.Replace("//", ","));
You can use this in c#
string regex = #"(?:[^\/]),";
var match = Regex.Split("Mister,Bill,is,made,of/,clay", regex, RegexOptions.IgnoreCase);
After that you can replace /, and continue your operation as you like

Categories