Click here to Skip to main content
12,400,556 members (61,945 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
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 24-Jun-13 23:00pm
rahuls11.1K
Updated 24-Jun-13 23:26pm
Johnny J.150.5K
v4
Comments
Teju B Gowda 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"?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

if (myStringVariable.StartsWith("Var_str") && !myStringVariable.Equals("Var_str"))
{
    //There is something more to be seen
}
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Comments
Teju B Gowda 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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Try this
const string s = "Var_str_Value";
          var result = s.Replace("Var_str","");
          if(result.Trim().Length>0)
          {
              //Contain more than characters
          }

Hope this helps
  Permalink  
Comments
Johnny J. 25-Jun-13 5:30am
   
Won't work if the string is "Var_strVar_str" !!!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

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:
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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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 }
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 26 Jun 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100