String search: last index : String Search « Data Types « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Data Types » String SearchScreenshots 
String search: last index
String search: last index

/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/

 using System;

 namespace StringSearch
 {
    public class TesterStringLastIndex
    {
       public void Run()
       {
           // create some strings to work with
           string s1 = "One Two Three Four";

           int index;

           // get the index of the last space
           index=s1.LastIndexOf(" ");

           // get the last word.
           string s2 = s1.Substring(index+1);

           // set s1 to the substring starting at 0
           // and ending at index (the start of the last word
           // thus s1 has one two three
           s1 = s1.Substring(0,index);

           // find the last space in s1 (after two)
           index = s1.LastIndexOf(" ");

           // set s3 to the substring starting at
           // index, the space after "two" plus one more
           // thus s3 = "three"
           string s3 = s1.Substring(index+1);

           // reset s1 to the substring starting at 0
           // and ending at index, thus the string "one two"
           s1 = s1.Substring(0,index);

           // reset index to the space between
           // "one" and "two"
           index = s1.LastIndexOf(" ");

           // set s4 to the substring starting one
           // space after index, thus the substring "two"
           string s4 = s1.Substring(index+1);

           // reset s1 to the substring starting at 0
           // and ending at index, thus "one"
           s1 = s1.Substring(0,index);

           // set index to the last space, but there is
           // none so index now = -1
           index = s1.LastIndexOf(" ");

           // set s5 to the substring at one past
           // the last space. there was no last space
           // so this sets s5 to the substring starting
           // at zero
           string s5 = s1.Substring(index+1);

           Console.WriteLine ("s2: {0}\ns3: {1}",s2,s3);
           Console.WriteLine ("s4: {0}\ns5: {1}\n",s4,s5);
           Console.WriteLine ("s1: {0}\n",s1);
       }

       static void Main()
       {
          TesterStringLastIndex t = new TesterStringLastIndex();
          t.Run();
       }
    }
 }

           
       
Related examples in the same category
1.Search stringsSearch strings
2.use the IndexOf() and LastIndexOf() methods to search for substrings and characters;
3.String search DemoString search Demo
4.String split and searchString split and search
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.