String substitution in C# - c#

I have a list of names and I loop through them to create a comma separated list in a string variable (Bob, George, Will, Terry).
I need the list to eventually look like (Bob, George, Will and Terry).
How do I find the LAST instance of the comma and replace it with the word "and"? Once I find the LAST instance, I think it's a simple matter of doing something like
string new=ori.Substring(0,start) + rep + ori.Substring(start+rep.Length);
Thoughts? Comments? Suggestions?
Thanks,
Bob

This should work for you. Added the alternative comma style as well.
var names = "Bob, George, Will, Terry";
var lastCommaPosition = names.LastIndexOf(',');
if (lastCommaPosition != -1)
{
names = names.Remove(lastCommaPosition, 1)
//.Insert(lastComma, " and");
.Insert(lastCommaPosition, ", and");
}
Console.WriteLine(names);

You can use a combination of LINQ and String.Join. This solution does not need the last index of a comma and is "more fluent" to read.
var list = new List<string> { "Bob", "George", "Will", "Terry" };
var listAsString = list.Count > 1
? string.Join(", ", list.Take(list.Count - 1)) + " and " + list.Last()
: list.First();

You can use Linq,
list.Select(i => i).Aggregate((i, j) => i + (list.IndexOf(j) == list.Count -1 ? " and " : " , ") + j);
Hope helps,

This should do the trick for you:
var foo = "Bob, George, Will, Terry";
if (foo.Contains(",")) {
foo = foo.Substring(0, foo.LastIndexOf(",")) + " and" + foo.Substring(foo.LastIndexOf(",")+ 1);
}

I'm not sure what you wanted to do, but the following code works:
string original = "(Bob, George, Will, Terry)";
string result = "";
string[] splited = original.Split(',');
for (int i = 0; i < splited.Count(); i++)
{
if(i == splited.Count() - 2)
{
result += splited[i] + " and";
}
else if(i == splited.Count() - 1)
{
result += splited[i];
}
else
{
result += splited[i] + ",";
}
}
I Used split to split the original string in a vector so i worked with this vector to replace the last comma to the word "and".

Related

Add all chars of a string to a list but merge numbers to one entry C#

You have given a string like "(32+5)*20" and you need to create a list with all chars, but need to merge the numbers to one entry like "32" and "20". How to do it?
I have already tried
string formel = "(32+5)*20";
char[] zeichen = formel.ToCharArray();
var liste = new List<string>();
for(int i = 0; i < zeichen.Length ; i++)
{
if(Char.IsNumber(zeichen[i]) && formel.Substring(i, i + 1).Any(char.IsNumber))
{
liste.Add(zeichen[i].ToString() + zeichen[i+1].ToString());
}
else
{
liste.Add(zeichen[i].ToString());
}
}
You could use regex to return the groups of numbers:
var formel = "(32+5)*20";
var pattern = "([0-9]+)";
var match = Regex.Match(formel, pattern);
and then use
match.Groups
The list as posted will still contain the same number of elements as the string did, since you added to it on every loop iteration. To "merge" into one entry, you need to not add on some iterations, but instead add that digit to the previous number.
This is easier if you check if the previous item was a number, rather than the next.
for (int i = 0; i < zeichen.Length; i++)
{
if (i > 0 && Char.IsNumber(zeichen[i]) && Char.IsNumber(zeichen[i - 1])) // if got a number, and had a number before
{
// Then dont Add to the list, instead add to the number/string
liste[list.Count - 1] += zeichen[i];
//liste.Add(zeichen[i].ToString() + zeichen[i + 1].ToString());
}
else
liste.Add(zeichen[i].ToString());
}
// "(", "32", "+", "5", ")", "*", "20"
You can do it by using Regex and LINQ
var dateText = "(32 + 5) * 20";
string splitPattern = #"[^\d]";
var result = Regex.Split(dateText, splitPattern).Where(x=>x!="");
I hope this will help you #Janic

Method name expected displaying strings in label c#

I am trying to add a specific part of a string to a label in c#
I only want the string up to the space to be displayed (in line 6 of this code)
bool BoolSpace = s.Contains(" ");
if (BoolSpace == true)
{
int IntSpacePos = s.IndexOf(" ");
int StrPos = IntSpacePos - 1;
LblLmcCode1.Text = LblLmcCode1.Text + s(0, StrPos);
}
else
{
LblLmcCode2.Text = LblLmcCode2.Text + '\n' + s;
}
However line 6 is returning an error method name expected about the 's' of s(0, StrPos)
You should use Substring method:
s.Substring(0, StrPos);
All together with fewer lines:
LblLmcCode1.Text = LblLmcCode1.Text + s.Substring(0, s.IndexOf(" ") - 1);
Also as BoolSpace is a boolean itself no need to check if it is equal to true but it is enough to write if(BoolSpace), and better still just place the Contains in the statement:
if(s.Contains(" "))
{
LblLmcCode1.Text = LblLmcCode1.Text + s.Substring(0, s.IndexOf(" ") - 1);
}
else
{
LblLmcCode2.Text = LblLmcCode2.Text + '\n' + s;
}

How to parse below string in C#?

Please someone to help me to parse these sample string below? I'm having difficulty to split the data and also the data need to add carriage return at the end of every event
sample string:
L,030216,182748,00,FF,I,00,030216,182749,00,FF,I,00,030216,182750,00,FF,I,00
batch of events
expected output:
L,030216,182748,00,FF,I,00 - 1st Event
L,030216,182749,00,FF,I,00 - 2nd Event
L,030216,182750,00,FF,I,00 - 3rd Event
Seems like an easy problem. Something as easy as this should do it:
string line = "L,030216,182748,00,FF,I,00,030216,182749,00,FF,I,00,030216,182750,00,FF,I,00";
string[] array = line.Split(',');
StringBuilder sb = new StringBuilder();
for(int i=0; i<array.Length-1;i+=6)
{
sb.AppendLine(string.Format("{0},{1} - {2} event",array[0],string.Join(",",array.Skip(i+1).Take(6)), "number"));
}
output (sb.ToString()):
L,030216,182748,00,FF,I,00 - number event
L,030216,182749,00,FF,I,00 - number event
L,030216,182750,00,FF,I,00 - number event
All you have to do is work on the function that increments the ordinals (1st, 2nd, etc), but that's easy to get.
This should do the trick, given there are no more L's inside your string, and the comma place is always the sixth starting from the beginning of the batch number.
class Program
{
static void Main(string[] args)
{
String batchOfevents = "L,030216,182748,00,FF,I,00,030216,182749,00,FF,I,00,030216,182750,00,FF,I,00,030216,182751,00,FF,I,00,030216,182752,00,FF,I,00,030216,182753,00,FF,I,00";
// take out the "L," to start processing by finding the index of the correct comma to slice.
batchOfevents = batchOfevents.Substring(2);
String output = "";
int index = 0;
int counter = 0;
while (GetNthIndex(batchOfevents, ',', 6) != -1)
{
counter++;
if (counter == 1){
index = GetNthIndex(batchOfevents, ',', 6);
output += "L, " + batchOfevents.Substring(0, index) + " - 1st event\n";
batchOfevents = batchOfevents.Substring(index + 1);
} else if (counter == 2) {
index = GetNthIndex(batchOfevents, ',', 6);
output += "L, " + batchOfevents.Substring(0, index) + " - 2nd event\n";
batchOfevents = batchOfevents.Substring(index + 1);
}
else if (counter == 3)
{
index = GetNthIndex(batchOfevents, ',', 6);
output += "L, " + batchOfevents.Substring(0, index) + " - 3rd event\n";
batchOfevents = batchOfevents.Substring(index + 1);
} else {
index = GetNthIndex(batchOfevents, ',', 6);
output += "L, " + batchOfevents.Substring(0, index) + " - " + counter + "th event\n";
batchOfevents = batchOfevents.Substring(index + 1);
}
}
output += "L, " + batchOfevents + " - " + (counter+1) + "th event\n";
Console.WriteLine(output);
}
public static int GetNthIndex(string s, char t, int n)
{
int count = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == t)
{
count++;
if (count == n)
{
return i;
}
}
}
return -1;
}
}
Now the output will be in the format you asked for, and the original string has been decomposed.
NOTE: the getNthIndex method was taken from this old post.
If you want to split the string into multiple strings, you need a set of rules,
which are implementable. In your case i would start splitting the complete
string by the given comma , and than go though the elements in a loop.
All the strings in the loop will be appended in a StringBuilder. If your ruleset
say you need a new line, just add it via yourBuilder.Append('\r\n') or use AppendLine.
EDIT
Using this method, you can also easily add new chars like L or at the end rd Event
Look for the start index of 00,FF,I,00 in the entire string.
Extract a sub string starting at 0 and index plus 10 which is the length of the characters in 1.
Loop through it again each time with a new start index where you left of in 2.
Add a new line character each time.
Have a try the following:
string stream = "L,030216,182748,00,FF,I,00, 030216,182749,00,FF,I,00, 030216,182750,00,FF,I,00";
string[] lines = SplitLines(stream, "L", "I", ",");
Here the SplitLines function is implemented to detect variable-length events within the arbitrary-formatted stream:
string stream = "A;030216;182748 ;00;FF;AA;01; 030216;182749;AA;02";
string[] lines = SplitLines(batch, "A", "AA", ";");
Split-rules are:
- all elements of input stream are separated by separator(, for example).
- each event is bounded by the special markers(L and I for example)
- end marker is previous element of event-sequence
static string[] SplitLines(string stream, string startSeq, string endLine, string separator) {
string[] elements = stream.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries);
int pos = 0;
List<string> line = new List<string>();
List<string> lines = new List<string>();
State state = State.SeqStart;
while(pos < elements.Length) {
string current = elements[pos].Trim();
switch(state) {
case State.SeqStart:
if(current == startSeq)
state = State.LineStart;
continue;
case State.LineStart:
if(++pos < elements.Length) {
line.Add(startSeq);
state = State.Line;
}
continue;
case State.Line:
if(current == endLine)
state = State.LineEnd;
else
line.Add(current);
pos++;
continue;
case State.LineEnd:
line.Add(endLine);
line.Add(current);
lines.Add(string.Join(separator, line));
line.Clear();
state = State.LineStart;
continue;
}
}
return lines.ToArray();
}
enum State { SeqStart, LineStart, Line, LineEnd };
f you want to split the string into multiple strings, you need a set of rules, which are implementable. In your case i would start splitting the complete string by the given comma , and than go though the elements in a loop. All the strings in the loop will be appended in a StringBuilder. If your ruleset say you need a new line, just add it via yourBuilder.Append('\r\n') or use AppendLine.

Getting only the last entry on counting

I'm only getting the last entry of the counting typed like this
public string ZodziuSkaiciavimas()
{
foreach (var sentence in Sakiniai.TrimEnd('.').Split('.'))
{
Rezultatas=(eilute.ToString() + " sakinyje zodziu:" + (sentence.Trim().Split(' ').Count() + sentence.Trim().Split('-').Count() + sentence.Trim().Split(';').Count() + sentence.Trim().Split(':').Count() + sentence.Trim().Split(',').Count() - 4));
eilute++;
}
return Rezultatas;
And I need to get the answer with a return type.
If I type code like this than i get what i want,but no returns.
public string ZodziuSkaiciavimas()
{
foreach (var sentence in Sakiniai.TrimEnd('.').Split('.'))
{
Console.WriteLine(eilute.ToString() + " sakinyje zodziu:" + (sentence.Trim().Split(' ').Count() + sentence.Trim().Split('-').Count() + sentence.Trim().Split(';').Count() + sentence.Trim().Split(':').Count() + sentence.Trim().Split(',').Count() - 4));
eilute++;
}
return Rezultatas;
}
Why arent you appending your results as below
Rezultatas +=(eilute.ToString() + " sakinyje zodziu:" + (sentence.Trim().Split(' ').Count() + sentence.Trim().Split('-').Count() + sentence.Trim().Split(';').Count() + sentence.Trim().Split(':').Count() + sentence.Trim().Split(',').Count() - 4)) + "\n";
It looks like you want to return multiple numbers from your method, but Rezultatas is a single string. You can fix it by changing the return type to List<int>, and returning a list:
public List<int> ZodziuSkaiciavimas() {
var Rezultatas = new List<int>()
foreach (var sentence in Sakiniai.TrimEnd('.').Split('.')) {
var res = sentence.Trim().Split(' ', '-', ';', ':', ',').Length;
Rezultatas.Add(res);
}
return Rezultatas;
}
When the callers decide to print the Rezultatas they gets back from your method, they could decide what character to put between the numbers (say, a comma ',') and print it like this:
var numbers = ZodziuSkaiciavimas();
Console.WriteLine(string.Join(", ", numbers));

How do i find the third longest word in string

Hello im a bit stuck i dont know how to find the third longest word in a string, i have got my code to find the longest but i cant manage to get it to find the third longest. any help?
public void longestWord()
{
string sentance, word;
word = " ";
char[] a = new char[] { ' ' };
sentance = textBox1.Text; //<--string here
foreach (string s1 in sentance.Split(a))
{
if (word.Length < s1.Length)
{
word = s1;
}
}
label9.Text = ("The longest word is " + word + " and its length is " + word.Length + " characters long");
}
P.S an example of the string im testing is:
1.
DarkN3ss is my most experienced provider of Windows based business solutions. I focus on delivering my business value in best possible understanding of this technologies and directions.
DarkN3ss recognising me as an “elite business partner” for implementing solutions based on my capabilities and experience with Windows and Linux products.
how about using linq?
sentance.Split(' ').OrderByDescending(w => w.Length).Skip(2).FirstOrDefault()
in a function :
public void nthLongestWord(int index = 0)
{
string word = null;
if(index <= 0)
{
word = sentance.Split(' ').OrderByDescending(w => w.Length).FirstOrDefault();
}
else
{
word = sentance.Split(' ').OrderByDescending(w => w.Length).Skip(index - 1).FirstOrDefault();
}
if(!string.IsNullOrWhitespace(word))
{
label9.Text = ("The longest word is " + word + " and its length is " + word.Length + " characters long");
}
else
{
// display something else?
}
}
Solution: To get all the third largest words
string[] splitStr = sentence.Split(' ');
if (splitStr.Length > 2)
{
List<int> allLengths = splitStr.Select(x => x.Length).Distinct().ToList();
int thirdLargestWordLength = allLengths.OrderByDescending(x => x)
.Skip(2).Distinct().Take(1).FirstOrDefault();
if (splitStr[0].Length != thirdLargestWordLength &&
splitStr[1].Length != thirdLargestWordLength)
{
string[] theThirdLargestWords = splitStr.Where(x => x.Length == thirdLargestWordLength)
.ToArray();
if (theThirdLargestWords.Length == 1)
{
label9.Text = "The third longest word is " + theThirdLargestWords[0];
}
else
{
string words = "";
for (int i = 0; i < theThirdLargestWords.Length; i++)
{
if (i == 0)
{
words = theThirdLargestWords[i];
}
//else if ((i + 1) == theThirdLargestWords.Length)
//{
// words += " and " + theThirdLargestWords[i];
//}
else
{
words += ", " + theThirdLargestWords[i];
}
}
label9.Text = "The third longest words are " + words;
}
}
}
I commented out the "and" part as i'm not sure if you want that in your string. I also added the first if statement so you don't get an error if you have less then 3 words.
If you'd like to make minimal changes to your current code, then what you should do is store the three longest words (i.e., instead of word, have word1, word2, and word3, or an array if you'd prefer).
Then, in your if statement, set word3=word2, word2=word1, and word1=s1.
That way, the third largest word will end up in word3.
Not the most efficient, but you'll be able to keep your current code, to a degree.
If you feel confortable using a temporary array, you should copy your words there, sort them and take the 3rd longest one.
This might have compile error, but just to give you an idea.
var words = sentence.split();
words.OrderBy (w => w.length ).ToArray ()[2]

Categories

Resources