Home Backend Development Python Tutorial Share an example code of a string function (partition)

Share an example code of a string function (partition)

May 22, 2017 pm 05:12 PM

It’s 2017, but the holidays seem to have nothing to do with me. I’m still doing hard things as hard as ever. However, I still hope that in the new year, all readers will be in good health and everything will go well, the earth will be at peace, that little rabbit will marry me obediently, and the compassionate HR staff of various listed companies in Hangzhou will be able to get Give me a good offer so that I can become rich, handsome, etc. as soon as possible.

Okay, dream back to reality, not so much time to be nonsense. I read an article www.codesec.net/view/165565.html, which mentioned the partition() function. This function is similar to split, both for cutting. But partition() is specifically used to split a string according to the delimiter in the brackets, and it is just a cut, and the result is a primitive.

AAA="http://123://.com"#There are two here://

#print(AAA.partition(":/ /")) #Use: // as the delimiter

#The generated results are: 'http', '://', ​​'123://.com')

What if we change it and use com as the separator?

print(AAA.partition("com"))

The result of carriage return is: ('http://123://.', 'com', '')

What if a non-existent character is used as the separator? Then what is returned is the character itself + two empty elements.

You must know that Yuanzu can be sorted, so you can easily think of it, partition()[1]=separator itself

These are just superficial things, but you can dig deeper one time. Partition can actually be used to cut large sections of source code. We still use the HTML from previous articles as an example.

html='''
<html>
<head>
<title>A</title>
</head>
<body>
<p><a herf="www.php.cn/welcome.html">AA!</a>
<p>
<url>
<li><a href="http:www.php.cn/1.html">AAA</a></li>
<li><a href="http:www.php.cn//2.html">AAAA</a></li>
<li><a href="http:www.php.cn//3.html">AAAAA</a></li>
</url>
</p>
</p>
</body>
</html>&#39;&#39;&#39;
str="<title>A</title>"
content=html.partition(str)[2]
str1="</url>"
content1=content.partition(str1)[0]
print(content1)
这个程序的答案就是两头切的中间那一段,即
</head>
<body>
<p><a herf="www.51cto.com/welcome.html">AA!</a>
<p>
<url>
<li><a href="http:www.php.cn/1.html">AAA</a></li>
<li><a href="http:www.php.cn/2.html">AAAA</a></li>
<li><a href="http:www.php.cn/3.html">AAAAA</a></li>
Copy after login

This can also achieve the purpose of "knocking on the seams with a sledgehammer"

[Related recommendations]

1. Share an article about strings in Python Detailed explanation of function (partition)

2. Partition string function example tutorial in Python

3. MySQL-data table partitioning technology PARTITION Brief analysis of code examples

The above is the detailed content of Share an example code of a string function (partition). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Guide to String Processing in PHP Guide to String Processing in PHP Jun 11, 2023 am 10:21 AM

PHP is a popular programming language that is widely used in web application development. In PHP, string is a basic data type that allows us to store and manipulate text information. In this article, we will cover guidelines for string processing in PHP. Definition of string In PHP, a string is a sequence of adjacent characters, which can be represented by single quotes or double quotes. For example: $str1='Hello,world!';$str2="We

PHP8.1 update: performance improvements for array and string functions PHP8.1 update: performance improvements for array and string functions Jul 08, 2023 am 08:25 AM

PHP8.1 update: Performance improvements for array and string functions The PHP programming language has continued to evolve and improve over time. The recently released version of PHP 8.1 brings many new features and performance enhancements, especially when it comes to array and string functions. These improvements allow developers to handle array and string operations more efficiently, improving overall performance and efficiency. Performance improvements of array functions In PHP8.1, array functions have been improved and optimized. Here are some important examples of performance improvements for array functions: (1

How to get the name of the current worksheet in Excel How to get the name of the current worksheet in Excel Jun 03, 2023 am 10:32 AM

Excel does not provide a built-in formula to immediately return the name of the active Excel worksheet. However, in some cases, you may need to dynamically populate the values ​​of the active worksheet in your Excel file. For example, if the table name on a worksheet must be the name of the worksheet itself, and if you hardcode the table name and later change the worksheet name, you must also change the table name manually. However, if the table's name is populated dynamically, such as using a formula, then if the sheet name changes, the table's name will also change automatically. As mentioned before, there is no direct formula for extracting the name of the active sheet, although the requirement is very possible. However, we do have some combinations of formulas that you can use to successfully extract the active sheet's

String functions in PHP8: How to use str_starts_with() String functions in PHP8: How to use str_starts_with() May 15, 2023 pm 11:01 PM

A new practical string function str_starts_with() has been added to PHP8. This article will introduce the introduction, usage and examples of this function. Introduction to str_starts_with() The str_starts_with() function can determine whether a string starts with another string and return a Boolean value. The syntax is as follows: str_starts_with(string$haystack,string$nee

PHP string processing methods and FAQs PHP string processing methods and FAQs Jun 09, 2023 pm 01:01 PM

PHP is a powerful server-side scripting language widely used for web development. In PHP, string processing is a very common operation. This article will introduce some PHP string processing methods and answer some common questions. String concatenation In PHP, you can use the "." symbol to concatenate two strings. For example: $str1="Hello";$str2="World";$str3=$str1.$

How to use PHP's string functions? How to use PHP's string functions? Apr 20, 2024 pm 05:15 PM

How to use string functions in PHP? PHP provides a series of built-in string functions that can be used to manipulate and process text data. These function operations include text formatting, search and replacement, comparison and validation, etc. Basic string manipulation function description strlen() returns the length of the string strtoupper() converts the string to uppercase strtolower() converts the string to lowercase substr() extracts a substring from the string str_replace() searches and replaces characters Specific substring text formatting function description trim() removes the leading and trailing whitespace characters from the string ltrim() removes the left whitespace characters from the string rtrim() removes the leading and trailing whitespace characters from the string

3 ways to recover the lost system reserved partition 3 ways to recover the lost system reserved partition May 09, 2023 pm 12:43 PM

It is very likely that the system reserved partition is missing on your computer, and when this happens, the PC will have difficulty booting. By default, a system reserved partition is created during a successful installation of a new Windows operating system (OS). It contains files such as bootmgr and BOOTSECT.BAK, files used to boot your computer, in case you used Bitlocker to encrypt your hard drive. To protect the files in this partition, they will not be displayed in File Explorer. Nonetheless, some users may delete the system reserved partition mistakenly or unknowingly. If this is your case, or you just can't find the system reserved partition, we've highlighted how to fix it easily. If the system retains points

Commonly used string processing functions in PHP and how to use them Commonly used string processing functions in PHP and how to use them Jun 25, 2023 am 10:49 AM

PHP is a server-side scripting language, and as we all know, string is the most basic data type in any programming language. There are many string processing functions in PHP. How to use these functions flexibly is very critical for developing efficient and standardized programs. This article will introduce the commonly used string processing functions in PHP and how to use them. 1. String length processing strlen function The strlen function returns the length of a string. This function does not count multi-byte characters, but only single-byte characters. For example: $st

See all articles