function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
taralextaralex 

string.indexof

string.indexof returns -1 even though the source string definitely contains the target string according to the logs.
the source string is being read from an uploaded file, so I'm wondering if there can be any encoding issues involved.
I'm getting the source string from Blob.ToString() method and the target string is hard coded.
Raj VakatiRaj Vakati


string.indexof is Returns the index of the first occurrence of the specified substring. If the substring does not occur, this method returns -1.
taralextaralex
Well the problem is that the substring does occur, but it still returns -1. Is there a limitation as to the string legnth? The source string is about 2000 characters long.
Raj VakatiRaj Vakati
No .. can u give me sub string and string ?
taralextaralex
Well, the problem is: it works fine if I copy the string and substring out of the debug window and run the following anonimous code:
string str = mystring;
string substr = mysubstring;
if (str.indexof(substr) != -1)
    system.debug('yes');
else
    system.debug('no');
However, in the actual code, the string is the contents of an uploaded file unzipped using Zippex library, so I'm wondering if there are any encoding issues involved. Although I couldn't find any ways to control the encoding, the string is returned from the Blob.toString() method and it should be unicode.
 
Jolly_BirdiJolly_Birdi
Hello @Tara

Hope So this below example helps you to understand the working of indexOf:

indexOf(String subString)
    - It will use to get index of string/character from the string.
    Example: If you want to get index of "A" from "TESTABC"
    
    String strMainString = 'TESTABC';
    Integer intIndex = strMainString.indexOf('A');
    
    Result: intIndex = 4;

Thanks 
Jolly Birdi