Click here to Skip to main content
15,921,169 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have written a code in which it checks that what ever is in ' ' it should display it as a string literal but its not taking white spaces in it. when there is no whitespace in '' then it display it as a string literal
here is my code.............


C#
//string literals
                                      bool startSym = ss.StartsWith("'");
                                      bool endSym = ss.EndsWith("'");
                                      string strr = ss.Substring(1);
                                      bool strliteral = strr.Any(char.IsLetterOrDigit) || strr.Any(char.IsPunctuation) || strr.Any(char.IsSymbol);
                                      bool invalid_str = strr.StartsWith("'") && startSym && strliteral  && endSym;
                                      string strspace = ss;
                                      //bool space = strspace;
                                      //&& strr.Any(char.IsSeparator)
                                      if (startSym  && strliteral && endSym)
                                      {
                                        if (invalid_str)
                                          {
                                              MessageBox.Show("Error: Line num  " + linecount + "  Invalid String Literal b/c of multiple starting symbol");
                                              errorcount++;
                                              break;
                                          }
                                         // if (ss.Length > 2) //valid string literal
                                          else
                                          {
                                              MessageBox.Show("this is a String Literal");
                                              break;
                                          }
                                      }




can any one please help me............
Posted

1 solution

You don't use Any like that: http://msdn.microsoft.com/en-us/library/bb534972(v=vs.90).ASPX[^]
You don't just give a method name!
Try:
C#
bool areThereAnyLetterOrDigitsInStrrAtAll = strr.Any(c => char.IsLetterOrDigit(c));
 
Share this answer
 
Comments
Member 10740412 13-Apr-14 2:29am    
not working still.........
OriginalGriff 13-Apr-14 2:39am    
So show us exactly what code you are using now.
OriginalGriff 13-Apr-14 2:53am    
And while you are at it, "not working" doesn't help much!
Try showing your input, what you expect and why, and what you actually get - it helps us work out what is wrong. :laugh:
Member 10740412 13-Apr-14 2:59am    
'this is my string'
this is the input i am giving and it should be displayed as that it is a string literal but its ignoring white spaces and does not display anything.
but when i use only single word like 'this' without any space it displays it is a string literal...
OriginalGriff 13-Apr-14 3:51am    
I just tried your original code, and it "works" for me: it's wrong, in so many ways, but:
string ss = "'this is a string'";
bool startSym = ss.StartsWith("'");
bool endSym = ss.EndsWith("'");
string strr = ss.Substring(1);
bool strliteral = strr.Any(char.IsLetterOrDigit) || strr.Any(char.IsPunctuation) || strr.Any(char.IsSymbol);
bool invalid_str = strr.StartsWith("'") && startSym && strliteral && endSym;
string strspace = ss;
if (startSym && strliteral && endSym)
{
if (invalid_str)
{
MessageBox.Show("Error: Line num Invalid String Literal b/c of multiple starting symbol");
return;
}
else
{
MessageBox.Show("this is a String Literal");
return;
}
}
Gives me the MessageBox "this is a string literal".
So the initial problem is almost certainly that the string isn't what you think it is!

But... it won't work in practice, because Any returns true is any of the conditions is true - so checking is Any character is a letter will pass even if your string is
''''''s'
You want to know if the whole string only contains valid characters, I suspect, which is rather different.
What I would do is:
Check the string starts and ends with a quote.
Then check each character to make sure it is valid - and you would need to list what characters are valid, and I don't know what that list is - and fail the string if any character does *not* match, rather than as you are which passes the string if any character does match.

Out of interest, have you considered using a Regex?

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