current position:Home>[fun bath] C + + tips -- knowledge points: substr() interception function & replace() replacement function & find lookup function & similar to substring() function in Java

[fun bath] C + + tips -- knowledge points: substr() interception function & replace() replacement function & find lookup function & similar to substring() function in Java

2022-01-26 23:37:06 Playful bass

【 Play bass 】C++ tips—— Knowledge point : substr Intercept function & replace Substitution function & find Lookup function & similar JAVA in substring function

Welcome to my WeChat official account. :
Programming Pastor

ID:
bianchengzhizhen
Timely sharing algorithm 、 Computer science and game programming

I am CSDN Blog home page :
https://blog.csdn.net/D16100?spm=1000.2115.3001.5343&type=blog
Welcome to exchange and study

One 、 similar JAVA in substring() function

In the process of processing strings, there are many cases where you need to intercept strings , At this time Java Provided in substring Method to intercept is very convenient

There are two commonly used methods :
① public String substring(int beginIndex)

The string intercepted by this method is from the index beginIndex At the beginning , To the end of the entire string , for example : character string String s = “abcdef”;

call s.substring(2) Indicates the index from the string 2 Start intercepting to the end of the whole string , The intercepted string is cdef

② public String substring(int beginIndex, int endIndex)
The string intercepted by this method is from beginIndex Start , To the string index endIndex - 1 end , That is, the intercepted string does not include endIndex The character corresponding to this index , therefore endIndex The maximum value of is the length of the whole string , Therefore, when using this method, we should pay special attention to the problem that string interception is easy to cross the boundary

C++ in :

Two 、str.substr() function

The header file
#include “cstring”
perhaps <> here CSDN I can't type it out. I don't know why

  • string s=“abcdef.ghijklmn”;
  • Intercept substring
  • s.substr(pos, n)
    Intercept s In the from pos Start ( Include 0) Of n A substring of characters , And back to
    //s.substr(0,6)= “abcdef”;
  • s.substr(pos)
    Intercept s In the from pos Start ( Include 0) Substrings of all characters to the end , And back to //s.substr(7)=“ghijklmn”;

3、 ... and 、replace() function

  • Replace substring

s.replace(pos, n, s1)
use s1 Replace s In the from pos Start ( Include 0) Of n A substring of characters
//s.replace(0,6,“aaaaaa”) ss=“aaaaaa.ghijklmn”;

Four 、find() function

  • Find substring

     s.find(s1)          lookup s For the first time s1 The location of , And back to ( Include 0)
    
     s.rfind(s1)         lookup s Last occurrence in s1 The location of , And back to ( Include 0)
    
     s.find_first_of(s1)        Find in s1 Any character in s The first place in , And back to ( Include 0)
    
     s.find_last_of(s1)        Find in s1 Any character in s The last place in , And back to ( Include 0)
    
     s.fin_first_not_of(s1)          lookup s The first of them does not belong to s1 The position of characters in , And back to ( Include 0)
    
     s.fin_last_not_of(s1)          lookup s The last of these does not belong to s1 The position of characters in , And back to ( Include 0
    

copyright notice
author[Playful bass],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262337037297.html

Random recommended