Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 25-Jun-13 0:00am
rahuls11.1K
Edited 25-Jun-13 0:26am
Johnny J.115.6K
v4
Comments
Teju B Gowda at 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 at 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 at 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 at 25-Jun-13 5:25am
   
+5 good one Arun
Johnny J. at 25-Jun-13 5:30am
   
Won't work if the string is "Var_strVar_str" !!!
ArunRajendra at 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. at 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 at 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
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 519
2 Maciej Los 305
3 BillWoodruff 250
4 Mathew Soji 195
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,777
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 26 Jun 2013
Copyright © CodeProject, 1999-2014
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