Any way to split two different strings using one c# method? - c#

I have two Strings. For which the output required is as follows.
Input : "Abc (1.23)(12a/b)"
Output : "Abc (1.23)"
Input : "Abc(12a/b)"
Output : "Abc"
I am using C#.
I used string.Split("(")[0]. This gives me output for second but not for first requirement.
I need an equation that works for both. Any help would be appreciated. Thanks.

Here is what you want:
class Program
{
static void Main(string[] args)
{
var result1 = Remove12aSlashb("Abc (1.23)(12a/b)");
var result2 = Remove12aSlashb("Abc(12a/b)");
}
public static string Remove12aSlashb(string str)
{
return str.Replace("(12a/b)", string.Empty);
}
}

It appears that what you are actually trying to do (from your example strings) is to chop off the last set of text in parenthesis. If so, then string.Split("(") can still do it, but then you need to combine the results with a slightly more complicated logic.
Something like this could do it.
public static string RemoveFinalParens(string str) {
var pieces=str.Split("(");
string retString=null;
for (int i=0;i<pieces.Count;i++) {
return+=pieces[i];
if ((i+1)<pieces.Count) {return+="("};
}
return retString;
}

Related

words stemmer class c#

I am trying the following stemming class :
static class StemmerSteps
{
public static string stepSufixremover(this string str, string suffex)
{
if (str.EndsWith(suffex))
{
................
}
return str;
}
public static string stepPrefixemover(this string str, string prefix)
{
if (str.StartsWith(prefix)
{
.....................
}
return str;
}
}
this class works with one prefix or suffix. is there any suggestion to allow a list of prefixes or suffixes to go through the class and compare against each (str). your kind action really appreciated.
Instead of creating your own class from scratch (unless this is homework) I would definitive use an existing library. This answer provides an example of code that that implements the Porter Stemming Algorithm:
https://stackoverflow.com/questions/7611455/how-to-perform-stemming-in-c
Put your suffix/prefixes in a collection (like a List<>), and loop through and apply each possible one. This collection would need to be passed into the method.
List<string> suffixes = ...;
for (suffix in suffixes)
if (str.EndsWith(suffix))
str = str.Remove(str.Length - suffix.Length, suffix.Length);
EDIT
Considering your comment:
"just want to look if the string starts-/endswith any of the passed strings"
may be something like this can fit your needs:
public static string stepSufixremover(this string str, IEnumerable<string> suffex)
{
string suf = suffex.Where(x=>str.EndsWith(x)).SingleOrDefault();
if(!string.IsNullOrEmpty(suf))
{
str = str.Remove(str.Length - suf.Length, suf.Length);
}
return str;
}
If you use this like:
"hello".stepone(new string[]{"lo","l"}).Dump();
it produces:
hel
The simplest code would involve regular expressions.
For example, this would identify some English suffixes:
'^(.*?)(ing|ly|ed|ious|ies|ive|es|s|ment)?$'
One problem is that stemming is not as accurate as lemmatization. Lematization would require POS tagging for accuracy. For example, you don't want to add an -ing suffix to dove if it's a noun.
Another problem is that some suffixes also require prefixes. For example, you must add en- to -rich- to add a -ment suffix in en-rich-ment -- unlike a root like -govern- where you can add the suffix without any prefix.

Beginner logic development

I am a beginner programmer in C# who just got started. I have a task at hand where a program needs to read a string and perform some string manipulation. The UI provides a TextBox and all the options below as CheckBoxes. User can select any or all.
Remove any spaces.
Remove any special chars like ',' etc.
Remove any numbers.
Convert to camelCase.
There can be more options as part of the string cleanup. I have wrttten the string processing in a method, that has a chasm of if ... else ifs ...
I am sure there is a way around.
Appreciate any help.
Thanks for all the solutions, but I think my point did was not put across correctly.
The string processing will be done in a particular order depending on the checkbox value.
User might select just one or every option provided. In case there is more than one selected, it should be like
if(RemoveSpaces.checked)
{
RemoveSpaces(string inputString);
// After removing spaces do the other operations
}
else if (RemoveSpecialChars.checked)
{
RemoveSpecialChars(string inputString);
// Do other processing
}
For easy String manipulation, use String.replace
See String.replace
This code example might also help:
string start = "a b 3 4 5.7";
string noSpace = start.Replace(" ", "");
string noDot = noSpace.Replace(".", "");
string noNumbers = Regex.Replace(noDot, "[0-9]", "");
Console.WriteLine(start);
Console.WriteLine(noSpace);
Console.WriteLine(noDot);
Console.WriteLine(noNumbers);
The output will then be as follows
"a b 3 4 5.7" // start
"ab345.7" // noSpace
"ab3457" // noDot
"ab" // noNumbers
You can make some class and 4 functions inside. for example:
public static class StringOperations
{
public static string RemoveSpaces(string sourceString)
{
string convertedString = "";
//some operations
return convertedString;
}
public static string RemoveCharacters(string sourceString, params char[] charactersToRemove)
{
string convertedString = "";
//some operations
return convertedString;
}
public static string RemoveAnyNumbers(string sourceString)
{
string convertedString = "";
//some operations
return convertedString;
}
public static string ConvertToCamelCase(string sourceString)
{
string convertedString = "";
//some operations
return convertedString;
}
}
In Your UI you just call one of functions...

C# 2.0 function which will return the formatted string

I am using C# 2.0 and I have got below type of strings:
string id = "tcm:481-191820"; or "tcm:481-191820-32"; or "tcm:481-191820-8"; or "tcm:481-191820-128";
The last part of string doesn't matter i.e. (-32,-8,-128), whatever the string is it will render below result.
Now, I need to write one function which will take above string as input. something like below and will output as "tcm:0-481-1"
public static string GetPublicationID(string id)
{
//this function will return as below output
return "tcm:0-481-1"
}
Please suggest!!
If final "-1" is static you could use:
public static string GetPublicationID(string id)
{
int a = 1 + id.IndexOf(':');
string first = id.Substring(0, a);
string second = id.Substring(a, id.IndexOf('-') - a);
return String.Format("{0}0-{1}-1", first, second);
}
or if "-1" is first part of next token, try this
public static string GetPublicationID(string id)
{
int a = 1 + id.IndexOf(':');
string first = id.Substring(0, a);
string second = id.Substring(a, id.IndexOf('-') - a + 2);
return String.Format("{0}0-{1}", first, second);
}
This syntax works even for different length patterns, assuming that your string is
first_part:second_part-anything_else
All you need is:
string.Format("{0}0-{1}", id.Substring(0,4), id.Substring(4,5));
This just uses substring to get the first four characters and then the next five and put them into the format with the 0- in there.
This does assume that your format is a fixed number of characters in each position (which it is in your example). If the string might be abcd:4812... then you will have to modify it slightly to pick up the right length of strings. See Marco's answer for that technique. I'd advise using his if you need the variable length and mine if the lengths stay the same.
Also as an additional note your original function of returning a static string does work for all of those examples you provided. I have assumed there are other numbers visible but if it is only the suffix that changes then you could happily use a static string (at which point declaring a constant or something rather than using a method would probably work better).
Obligatory Regular Expression Answer:
using System.Text.RegularExpressions;
public static string GetPublicationID(string id)
{
Match m = RegEx.Match(#"tcm:([\d]+-[\d]{1})", id);
if(m.Success)
return string.Format("tcm:0-{0}", m.Groups[1].Captures[0].Value.ToString());
else
return string.Empty;
}
Regex regxMatch = new Regex("(?<prefix>tcm:)(?<id>\\d+-\\d)(?<suffix>.)*",RegexOptions.Singleline|RegexOptions.Compiled);
string regxReplace = "${prefix}0-${id}";
string GetPublicationID(string input) {
return regxMatch.Replace(input, regxReplace);
}
string test = "tcm:481-191820-128";
stirng result = GetPublicationID(test);
//result: tcm:0-481-1

Splitting a string at every character

let's say I have a string "hello world". I would like to end up with " dehllloorw". As I don't find any ready-made solution I thought: I can split the string into a character array, sort it and convert it back to a string.
In perl I can do s// but in .Net I'd have to do a .Split() but there's no overload with no parameters... if I do .Split(null) it seems to split by whitespace and .Split('') won't compile.
how do I do this (I hate to run a loop!)?
Array.Sort("hello world".ToCharArray());
Below is a quick demo console app
class Program
{
static void Main(string[] args)
{
var array = "hello world".ToCharArray();
Array.Sort(array);
Console.WriteLine(new String(array));
Console.ReadLine();
}
}
The characters in a string can be directly used, the string class exposed them as an enumeration - combine that with Linq / OrderBy and you have a one-liner to create the ordered output string:
string myString = "hello world";
string output = new string(myString.OrderBy(x => x).ToArray()); // dehllloorw
You could always do this:
private static string SortStringCharacters(string value)
{
if (value == null)
return null;
return new string(value.ToList().Sort().ToArray());
}

C#: Is there a way to search a string for a number without using regex?

Is there a way to check to see if a string contains any numeric digits in it without using regex? I was thinking of just splitting it into an array and running a search on that, but something tells me there is an easier way:
//pseudocode
string aString = "The number 4"
If (aString contains a number) Then enter validation loop
Else return to main
//output
"The string contains a number. Are you sure you want to continue?"
var containsdigit = somestring.Any(char.IsDigit);
You could use String.IndexOfAny as:
bool isNumeric = mystring.IndexOfAny("0123456789".ToCharArray()) > -1;
You could create an extension method for string and use a combination of LINQ and the Char.IsNumber function e.g.
public static class StringExt
{
public static bool ContainsNumber(this string str)
{
return str.Any(c => Char.IsNumber(c));
}
}
Then your logic would look like:
//pseudocodestring
string str = "The number 4";
If (aString.ContainsNumber())
enter validation

Categories

Resources