Searching TString with special chars

Hi all.

I have a TString

which I want to search for the first (or any) occurrence of a TSubString

If I now do the straight-foward solution

if(letscallthis.First(searchfor) > -1) return true; else return false;

I get true (i.e. First() does not give me -1) although “&T:” is not in the TString. What I get is actually the position of the T of the word “Test” in the variable letscallthis. Why is this? And how can I prevent it?

The reason why I am searching for such a ridiculous substring is that I use a few strings to tell my C++ program what to do. And I want to use such a very uncommon substring (or any special character actually, besides &T: also %T:, @T: or something like that would work) in order to identify a special group of things that the program should do. If this uncommon substring is not part of the actual string, it takes that string and sends it to the, say, “main” function, if the substring is part of the string, it sends the string to a “special” function. I could do it with another boolean variable or such (e.g. setting “callspecialfunction” to true or false) but that ain’t nice and actually more confusing in my case :slight_smile:

Either way, I still am curious why First() does not return -1 in the case mentioned above.

Thank you for your help and comments.
heico

The TString::First(const char* cs) method returns the first occurrence of ANY character in “cs”.
Try: if (letscallthis.Index(searchfor) != kNPOS) return kTRUE; return kFALSE; or: if (letscallthis.Contains(searchfor)) return kTRUE; return kFALSE;

Hi.
Thank you for your reply. I tried both Index() and Contains() but I get a segmentation violation each time.
For example when calling the Index() method the segfault message is:

root [0]
Processing test2.C...

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x0000003757a997b5 in waitpid () from /lib64/libc.so.6
#1  0x0000003757a3c761 in do_system () from /lib64/libc.so.6
#2  0x00002adabd284c72 in TUnixSystem::StackTrace() ()
   from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libCore.so
#3  0x00002adabd281bba in TUnixSystem::DispatchSignals(ESignals) ()
   from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libCore.so
#4  <signal handler called>
#5  0x0000003757a6234c in _IO_seekpos_unlocked () from /lib64/libc.so.6
#6  0x0000003757a60ccd in fsetpos

GLIBC_2.2.5 () from /lib64/libc.so.6
#7  0x00002adabdd34604 in G__interpret_func ()
   from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libCint.so
#8  0x00002adabdd22e8e in G__getfunction ()
   from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libCint.so
#9  0x00002adabde12e36 in G__getstructmem(int, G__FastAllocString&, char*, int, char*, int*, G__var_array*, int) ()
   from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libCint.so
#10 0x00002adabde08d25 in G__getvariable ()
   from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libCint.so
#11 0x00002adabdcf5a4f in G__getitem ()
   from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libCint.so
#12 0x00002adabdd021b3 in G__getexpr ()
   from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libCint.so
#13 0x00002adabdd81d89 in G__exec_statement ()
   from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libCint.so
#14 0x00002adabdd34e72 in G__interpret_func ()
   from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libCint.so
#15 0x00002adabdd22ff8 in G__getfunction ()
   from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libCint.so
#16 0x00002adabdcf5bbd in G__getitem ()
   from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libCint.so
#17 0x00002adabdd001e1 in G__getexpr ()
   from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libCint.so
#18 0x00002adabdd0761d in G__calc_internal ()
   from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libCint.so
#19 0x00002adabdd970c1 in G__process_cmd ()
   from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libCint.so
#20 0x00002adabd23da3d in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) () from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libCore.so
#21 0x00002adabd23d513 in TCint::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) () from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libCore.so
#22 0x00002adabd195a9e in TApplication::ExecuteFile(char const*, int*, bool) ()
   from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libCore.so
#23 0x00002adabd192eed in TApplication::ProcessLine(char const*, bool, int*) ()
   from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libCore.so
#24 0x00002adabda1262c in TRint::Run(bool) ()
   from /swshare/ROOT/root_v5.34.18_slc5_amd64//lib/root/libRint.so
#25 0x0000000000400fcd in main ()
===========================================================


The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#5  0x0000003757a6234c in _IO_seekpos_unlocked () from /lib64/libc.so.6
#6  0x0000003757a60ccd in fsetpos

GLIBC_2.2.5 () from /lib64/libc.so.6
===========================================================


Root > Function test2() busy flag cleared

I did it with a macro, i.e. a file test2.C executing via root test2.C. The code reads:

  1 #include <TString>
  2
  3
  4 void test2(){
  5
  6     TString letscallthis = "a Test string with no &T: special content";
  7
  8     std::cout << letscallthis.Index("&T:") << std::endl;
  9
 10 }

#include “TString.h”
#include

Oups, yeah, you’re right. I forgot the .h for the TString library. The iostream i didn’t need btw.
Thank you!

You should "#include ", otherwise you may get: [...]$ root test2.C++ root [0] Processing test2.C++... Info in <TUnixSystem::ACLiC>: creating shared library /..././test2_C.so In file included from /.../test2_C_ACLiC_dict.h:34:0, from /.../test2_C_ACLiC_dict.cxx:17: /..././test2.C: In function ‘void test2()’: /..././test2.C:5:3: error: ‘cout’ is not a member of ‘std’ std::cout << letscallthis.Index("&T:") << std::endl; ^ /..././test2.C:5:49: error: ‘endl’ is not a member of ‘std’ std::cout << letscallthis.Contains("&T:a") << std::endl; ^ g++: error: /.../test2_C_ACLiC_dict.o: No such file or directory Error in <ACLiC>: Compilation failed! Error: Function test2() is not defined in current scope :0: *** Interpreter error recovered ***