Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, suppose I have a string

as e.g: "Var_str"

I need to check whether the string contains only "Var_Str" or it is appended by some more string

for example "Var_str_Value" or "Var_str_Var_Variable" or "Var_str.Comment"...

Please help on this.


[edit]spelling[/edit]
Posted
Updated 24-Jun-13 23:26pm
v4
Comments
The Doer 25-Jun-13 5:06am    
Your Question is not appropriate please eloborate on "for example Var_str_Value or Var_str_Var_Variable or Var_str.Comment."
rahuls1 25-Jun-13 5:12am    
i need to check wether Var_Str is a single string or it is appended by other string as shown in above example
Rajesh Anuhya 26-Jun-13 1:01am    
every string is start with "Var_str" or it may come like this also "Comment.Var_str"?

C#
if (myStringVariable.StartsWith("Var_str") && !myStringVariable.Equals("Var_str"))
{
    //There is something more to be seen
}
 
Share this answer
 
v2
You can try this code. If this returns true then there are no extra string. Here str is a varible which has a string that needs to be verified.

str.Replace("Var_str",string.Empty)==string.Empty
 
Share this answer
 
v2
Comments
The Doer 25-Jun-13 5:25am    
+5 good one Arun
Johnny J. 25-Jun-13 5:30am    
Won't work if the string is "Var_strVar_str" !!!
ArunRajendra 25-Jun-13 6:15am    
Yes it wont work. But the requirement doesnot say it should be replaced only once and not multiple times.
Try this
C#
const string s = "Var_str_Value";
          var result = s.Replace("Var_str","");
          if(result.Trim().Length>0)
          {
              //Contain more than characters
          }


Hope this helps
 
Share this answer
 
Comments
Johnny J. 25-Jun-13 5:30am    
Won't work if the string is "Var_strVar_str" !!!
From the kind of the question I also get the feeling that the poster wanted to implement something like a tokenizer and his expectation might me along these lines, e.g. if the Val_Str is followed by a whitespace, it is considered as being the first string by it self.

This code would look more like this:
C#
var str = "whatever";
var firstString = "Var_str";
if( str.StartWith(firstString) )
{
    if(str.Length == firstString.Length) { // contains only Var_Str }
    else {
        if( Char.IsWhiteSpace(str, firstString.Length)
        { // A whitespace follows, the first string is separated from the rest
        } else { // immediately followed by some other text
    }
}
else { // does not contain Var_Str at all
}


However this is just a guess on the requirements of the question, so some more details on the expected behavior provided by the poster would be helpful.
 
Share this answer
 
C#
var str = "whatever";
var firstString = "Var_str";
if( str.StartWith(firstString))
{
  if(str.Length == firstString.Length)
  {
    // contains only str
  }
  else {
     // also contains something else
  }
}
else{ // contains something else that is not starts with str }
 
Share this answer
 
v3
Comments
Steve44 26-Jun-13 0:45am    
Close sorawit, but your else branch is also entered when the string does not contain the required string at all.

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