Click here to Skip to main content
15,905,420 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my c# sample i have a string where i split them into multiple lines with coma, but i need last line with full stop.

example: You can update the driver which is creating issues to secure your system from further problems in the future

in my sample i parsed the above string like below:

You can update the driver,
which is creating issues,
to secure your system from further problems in the future,

but at the end of the string in last line, i need to replace coma with full stop

the lines may be more or less, the last line should be ended with full stop instead of coma

What I have tried:

i tried:

C#
logEventDataList = (IList<eventlogsdatatype>)eventLogs.SetChannelType(EventChannelType.Application).SetLogType(EventLogLevel.Error).SetDays(1).GetLogs();
            if (logEventDataList.Count > 0)
            {
                var app = logEventDataList.GroupBy(n => n.Provider).Select(c => new { Key = c.Key, total = c.Count() }).ToList();
                if (app.Count > 0)
                {
                    noErrors = false;
                    result += "Application: \r\n ";
                    foreach (var item in app)
                    {
                        if(item.total > 1 ) result += item.Key + " repeated " + item.total + ((app.Count > 1) ? " times,\r\n " : " times ");
                        else result += item.Key + ((app.Count > 1)?" once,\r\n " : " once ");
                    }
                    result += Environment.NewLine + Environment.NewLine;
                }
            }
Posted
Updated 22-Feb-18 22:56pm
v3
Comments
F-ES Sitecore 23-Feb-18 3:57am    
Use "EndsWith to see if something ends with a character, "TrimEnd" to remove characters from the end of a string and basic concatenation to add a character to a string

if string ends with ","
remove ","
add "."

1 solution

That code doesn't do any replacement, with comma or with full stop.
And to be honest, the choice of characters is a bit poor - comma and full stop will both naturally occur in lines of text, particularly if it's a message designed to be presented to a user - you should consider using less likely characters such as '|' and '¬' perhaps.

But ... assuming that you will be adding the line terminator in your foreach loop, the simplest way to do this is to change it to a for loop:
C#
for (int i = 0; i < app.Count(); i++)
   {
   var item = app[i];
   ...
   result += i == (app.Count() -1) ? "." : ",";
   result += Environment.NewLine + Environment.NewLine;
   }
 
Share this answer
 
Comments
justin1204 23-Feb-18 4:49am    
thanks for the reaply.. yes it worked but the issue is after the first line break if it has a second line, the coma starts at the start of the second line and after the last line, the full stop comes in the next line
OriginalGriff 23-Feb-18 5:31am    
You'll have to be more explicit, and show better input and output examples, both "i get this" and "I want this" - remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
So typing as little as possible helps no one in the long run!
justin1204 23-Feb-18 5:35am    
ok sure.. and sorry for my unclear comment

logEventDataList = (IList<eventlogsdatatype>)eventLogs.SetChannelType(EventChannelType.Application).SetLogType(EventLogLevel.Error).SetDays(1).GetLogs();
if (logEventDataList.Count > 0)
{
var app = logEventDataList.GroupBy(n => n.Provider).Select(c => new { Key = c.Key, total = c.Count() }).ToList();
if (app.Count > 0)
{
noErrors = false;
result += "Application: \r\n ";
for (int i = 0; i < app.Count(); i++)
{
var item = app[i];
if (item.total > 1 ) result += item.Key + " repeated " + item.total + ((app.Count > 1) ? " times\r\n" : " times");
else result += item.Key + ((app.Count > 1)?" once\r\n" : " once");
result += i == (app.Count() - 1) ? "." : ",";
}
result += Environment.NewLine + Environment.NewLine;
}
}

this is my output after modifying the code:

Application:
SupportAssistAgent repeated 3 times.

System:
Microsoft-Windows-DistributedCOM repeated 10 times
,BTHUSB once
.


comma starts at the start of the second line and full stop in the next line.
OriginalGriff 23-Feb-18 5:49am    
And what exactly is the input?
What did you expect as output?
justin1204 23-Feb-18 5:59am    
Actually i am reading the event logs from the system(Application, System and Security).Where the logs for each channel(application, system, security) are fetched. it may contain repeated logs, so i need to sort them and show like

Application:
provider name repeated 3 times.

Security:
Provider name1 repeated 10 times,
provider name2 repeated 4 times,
provider name3 once.

system:
provider name1 repeated 4 times,
provider name2 once.


this is the output i want.. if there are multiple entries, they need to be shown in different lines with comma at the end and for the last entry it should end with full stop

am i clear, thanks for your patience

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900