Find_first_of () and find_last_of () "Get path, file name"

Source: Internet
Author: User

Find_first_of () and find_last_of () "Get path, file name" (2011-06-11 12:44:46) Reprint Tags: Category: c
The string class provides strings handling functions that allow programmers to find characters within a string, extract sequential sequences of characters (called substrings), and delete and add them in a string. We'll cover some of the main functions.

1. The function find_first_of () and find_last_of () perform a simple pattern match, such as finding a single character C in a string. The function find_first_of () finds the 1th occurrence of the character C in a string, and the function find_last_of () finds the last occurrence of C. The matching position is the return value. If no match occurs, the function returns-1.

int find_first_of (char c, int start = 0):
Finds the 1th occurrence of C in a string, starting with the position start. If there is a match, the matching position is returned; otherwise, 1 is returned. By default, start is 0, and the function searches the entire string.

int find_last_of (char c):
Finds the last occurrence of C in a string. If there is a match, the matching position is returned; 1. The search finds a match at the end of the character, so there is no starting position provided.

Example:
String str = "Mississippi";
int index;
' s ' appears at index 2, 3, 5, 6
index = str.find_first_of (' s ', 0); Index is 2
index = str.find_first_of (' s ', 4); Index is 5
index = str.find_first_of (' s ', 7); Index is-1

The last appearance of ' s ' in index= 6
index = str.find_last_of (' s ');
The while loop outputs the index of each ' I '
while (index = str.find_first_of (' i ', index))!! =-1)
{
cout << "index" << index << "";
index++; Restart search at Next indx
}

Output Result: Index 1 index 4 Index 7 index 10

2. Extracts successive character sequences, both substrings, in a string.
This operation assumes the position start and the number of characters count.

string substr (int start=0,int count=-1);
Copies the count characters in a string from the starting position and returns these characters as substrings.
If the tail of the string is less than the count character or count is-1, the end of the string stops copying.
If you do not use a parameter call to include only the position start, then substr () returns a substring starting from the position to the tail of the string.

The Find () function finds the specified pattern in a string. The function takes the string s and position start as the argument, and finds the match of S as a substring.

int find (const string& s,int start = 0):
The search obtains the string s and position start, and finds the match of S as a substring.                                                                        If there is a match, the matching position is returned, otherwise-1. By default, start is 0, and the function searches the entire string.

Example:
String fullname = "Mark Tompkin", FirstName, LastName;
int index;

index = str.find_last_of ("); Index is 4
FirstName = "Mark" LastName = "Tompkin"
FirstName = fullname.sub string (0,index);
LastName = Fullname.substring (index+1);

index = fullname.find ("kin"); Match "Kin" at index = 9
index = Fullname.find ("Omp", 0); Match "Omp" at index = 6
index = Fullname.find ("Omp", 7); Index is-1 (no match)

3. Adding and removing strings

A character join (+, + =) is a string that is added at the end of a string. The insert () function expands this capability to allow strings to be added at any location. In order to get from the string. In order to remove the string from the string,
The function erase () can delete characters starting at the specified position.

void Insert (int statr,const string& s):
Put the substring s into the string, starting at position start. The insert operation increases the length of the original string.

void Erase (int start=0,int count=-1):
From start, remove count characters from the string. If the existing string is less than count characters, or count is-1, all characters to the end of the string are deleted. By default, start is 0, and the function deletes the string from the beginning of the string. By default, the function is also deleted to the end of the string. It is important to note that when you call the erase () function without parameters, the string is truncated to an empty string of length 0.

Example:
String str = "Endfile";
string s = "string Object type";
str + = "Mark";
Str.inset (3, "-of-"); STR is "End-of-file mark"
S.erase (7,7); S is "string type"
Delete 4 characters from index 3
S.erase (3,4);
cout << S; Output: "Strtype"

4.C_STR () returns the address of the C-language style string.
Converts a string object to a C-language style string.
Char *c_str ();
Returns the address of a C-style string equivalent to a string object. The return type char* represents the address of the 1th character of a C-style string.

Example:
string filename = "Input.dat";
Open requires file name to be a C language style string
Fin.open (Filename.c_str ());

5. Methods for separating string paths

Programs that process files may want to parse the file name. This algorithm is used for string processing. The file can be specified by pathname, and the pathname includes a set of names separated by the delimiter "\". The name sequence before the last "\" is called a path. The last name is the file name and may also include the extension.

Path name \class\programs\testfile.cpp
Path \class\programs\
File name Testfile.cpp
Name extension CPP

To parse the file name, we read the full pathname from the keyboard and output the path and file name. If the file name has an extension of "CPP", the extension "CPP" will be replaced with "EXE" when the executable file name is created. The following is the outline of the program structure, and the description of how to use the String function:

1. Enter a file name and use the function find_last_of () to search the string for the last "\" that appears. This character determines the end of the path and the beginning of the file name.
2. A path is a substring of all the strings before the last "\". The file name is all characters after the last "\". Use the last "\" Location and substr () to extract the path and file name.
3. The extension is the best one in the file name "." After the string. Call Find_last_of () to search for the last match, copy the file name, delete the current extension, and add the new extension "EXE". The executable file name produced by the output.

File Prg1_3.cpp
This program prompts the user to enter a path to the file
It uses the string class operation to identify and output
Path name and file name. If the file name has
Extension "CPP", create and output
The name of the executable file with an extension of "EXE", replacing
Name extension "CPP"

WJ.cpp: Defines the entry point of the console application.
//
#i nclude "StdAfx.h"
#i nclude<iostream>
#i nclude<string>

using namespace Std;

int main ()
{
String pathname, path, filename,executablefile;
' \ ' and '. ' The location
int Backslashindex, Dotindex;
cout << "Enter The path name:";
CIN >> Pathname;

Identify the location of the last ' \ '. Note: because
Escape codes such as ' \ n ' start with \
C + + uses ' \ \ ' to represent \

Backslashindex = pathname.find_last_of (' \ \ ');

The path name is the last character before the ' \ '
Path = Pathname.substr (0,backslashindex);

cout << "path:" << path << Endl;

The trailing path name is the file name
filename = pathname.substr (backslashindex+1,-1);
cout << "FileName:" << filename << Endl;

//To see if the file name has a '. cpp ' extension.
//First find the last '. ' The location. If
//does not have '. ', then Dotindex is-1
Dotindex = filename.find_last_of ('. ');
//test for '. ', whether the remaining characters are "CPP"
if (dotindex! =-1 && filename.substr (dotindex+1) = = "cpp")
{
   //delete "CPP" and add "EXE" to set executable string
   executablefile = filename;
   executablefile.erase (dotindex+1,3);
   executablefile+= "EXE";
   cout << executable: << executablefile << Endl;
}

return 0;
}
Output Result:
1th allowed Results:

Enter The path name: \class\programs\testfile
Path: \class\programs
Filename:testfile

2nd allowed Results:

Enter the path name:programs\strings\filedemp.cpp
Path:programs\strings
Filename:filedemo.cpp
Executable:filedemo.exe

3rd allowed Results:

Enter The path name: \program.cpp
Path
Filename:program.cpp

Find_first_of () and find_last_of () "Get path, file name"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.