Getting Substring from String.LastIndexOf 
Author Message
 Getting Substring from String.LastIndexOf

When trying to use the sTemp.LastIndexOf() Method, it is
not finding the any occurances and therefore not returning
an index.

Any suggestions?

private String FileName()
                {
                        try
                        {
                                String sFileName;
                                String sTemp;
                                Char chr;
                                chr = Convert.ToChar(92);
                                sTemp =
File1.Value.ToString();
                                sFileName = sTemp.Substring
(sTemp.LastIndexOf(chr, 0, sTemp.Length) + 1);
                                return sFileName;
                        }
                        catch(Exception q)
                        {
                                Center = (string)q.Message;
                                return Center;
                        }
                }



Tue, 31 May 2005 07:00:16 GMT  
 Getting Substring from String.LastIndexOf
Just yesterday I've been banging my head against the wall about this until
I've reread the documentation about 30 times
The second parameter is starting point from which search proceeds
*backwards*. If you want to search whole string it must be string.Length -
1, not 0
Your call should be simply:
sTemp.LastIndexOf(chr)
which is equivalent to
sTemp.LastIndexOf(chr, sTemp.Length - 1, sTemp.Length)

Alex Feinman


Quote:
> When trying to use the sTemp.LastIndexOf() Method, it is
> not finding the any occurances and therefore not returning
> an index.

> Any suggestions?

> private String FileName()
> {
> try
> {
> String sFileName;
> String sTemp;
> Char chr;
> chr = Convert.ToChar(92);
> sTemp =
> File1.Value.ToString();
> sFileName = sTemp.Substring
> (sTemp.LastIndexOf(chr, 0, sTemp.Length) + 1);
> return sFileName;
> }
> catch(Exception q)
> {
> Center = (string)q.Message;
> return Center;
> }
> }



Tue, 31 May 2005 07:13:26 GMT  
 Getting Substring from String.LastIndexOf
try this


Rafat Sarosh

Quote:
>-----Original Message-----
>When trying to use the sTemp.LastIndexOf() Method, it is
>not finding the any occurances and therefore not
returning
>an index.

>Any suggestions?

>private String FileName()
>            {
>                    try
>                    {
>                            String sFileName;
>                            String sTemp;
>                            Char chr;
>                            chr = Convert.ToChar(92);
>                            sTemp =
>File1.Value.ToString();
>                            sFileName = sTemp.Substring
>(sTemp.LastIndexOf(chr, 0, sTemp.Length) + 1);
>                            return sFileName;
>                    }
>                    catch(Exception q)
>                    {
>                            Center = (string)q.Message;
>                            return Center;
>                    }
>            }
>.



Tue, 31 May 2005 07:19:35 GMT  
 Getting Substring from String.LastIndexOf
This version goes backwards from the given start position, so 0 is a
guaranteed to throw an exception!

Why not just use the LastIndexOf(char) version instead?

--
Nick Holmes
Coyote Software, GmbH.


Quote:
> When trying to use the sTemp.LastIndexOf() Method, it is
> not finding the any occurances and therefore not returning
> an index.

> Any suggestions?

> private String FileName()
> {
> try
> {
> String sFileName;
> String sTemp;
> Char chr;
> chr = Convert.ToChar(92);
> sTemp =
> File1.Value.ToString();
> sFileName = sTemp.Substring
> (sTemp.LastIndexOf(chr, 0, sTemp.Length) + 1);
> return sFileName;
> }
> catch(Exception q)
> {
> Center = (string)q.Message;
> return Center;
> }
> }



Tue, 31 May 2005 07:25:26 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. how to check that string contains substring?

2. Finding occurance of a substring in a string in C#

3. Searching string for substring question

4. How to do substring function for C string

5. longest substring in a string

6. How to remove a substring from a string

7. How to insert a substring in a string?

8. substring in a string

9. about searchin' substring in a string

10. How can I extract the substring of string??

11. function to search string for substring

12. newbie:Occurrences of a substring in a string

 

 
Powered by phpBB® Forum Software