search
HomeWeb Front-endJS TutorialImplementation of TrimStart, TrimEnd, and Trim on javascript in C#_javascript skills

So, I wrote it myself!! I saw many people using regular expressions, but I didn’t know how to do it, so I used the most primitive method to achieve it! Post the code! I hope it will be helpful to everyone!!!

Copy code The code is as follows:

String.prototype.trimStart = function(trimStr){
if(!trimStr){return this;}
var temp = this;
while(true){
if(temp.substr(0,trimStr.length)!=trimStr){
break ;
}
temp = temp.substr(trimStr.length);
}
return temp;
};
String.prototype.trimEnd = function(trimStr){
if(!trimStr){return this;}
var temp = this;
while(true){
if(temp.substr(temp.length-trimStr.length,trimStr.length)!= trimStr){
break;
}
temp = temp.substr(0,temp.length-trimStr.length);
}
return temp;
};
String.prototype.trim = function(trimStr){
var temp = trimStr;
if(!trimStr){temp=" ";}
return this.trimStart(temp).trimEnd(temp);
};

Everyone should know how to use it!!! I won’t go into details here!!! If you have any questions, please point them out! Thank you!
Statement
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
实用技巧:如何利用PHP中的trim函数处理中文空格实用技巧:如何利用PHP中的trim函数处理中文空格Mar 27, 2024 am 11:27 AM

在PHP编程中,处理字符串时经常会遇到空格的问题,这其中包括中文空格。在实际开发中,我们常常会使用trim函数来去除字符串两端的空格,但是对于中文空格的处理相对复杂一些。本文将介绍如何利用PHP中的trim函数来处理中文空格,并提供具体的代码示例。首先,让我们了解一下中文空格的种类。在中文中,空格不仅包括常见的英文空格(space),还存在其他一些特殊的空格

PHP中trim()函数的使用指南PHP中trim()函数的使用指南Feb 20, 2024 am 08:39 AM

PHP中trim()函数的使用指南trim()函数在PHP中非常常用,用于去除字符串开头和结尾的空格或其他指定字符。本文将详细介绍trim()函数的使用方法,并提供具体的代码示例。一、函数语法trim()函数的语法如下:trim(string$str,string$character_mask=""):string该函数接受两个参数,

固态硬盘的trim功能有什么作用固态硬盘的trim功能有什么作用Nov 21, 2022 am 10:58 AM

固态硬盘的trim功能主要是优化固态硬盘,解决SSD使用后的降速与寿命的问题,通过准备数据块进行重用来提高SSD效率的功能。Trim功能是几乎所有SSD固态硬盘都具有的功能,是一个ATA指令,当系统确认SSD支持Trim在删除数据时,不向硬盘通知删除指令,只使用Volume Bitmap来记住这里的数据已经删除。从而实现更加快速的数据处理。

Windows 11 上的 TRIM 启用和禁用方法Windows 11 上的 TRIM 启用和禁用方法Sep 29, 2023 pm 03:13 PM

使用SSD驱动器会让您一直担心丢失数据并且无法恢复数据。但是,Windows允许您通过执行仅写入必要数据的TRIM命令来实现最佳性能,而无需管理旧数据块。为此,您需要确保您的SSD支持TRIM并在您的操作系统中启用它。如何检查是否启用了TRIM?在大多数情况下,默认情况下,在现代SSD中启用TRIM功能。但为了确保这一点签出,您可以使用管理权限运行命令。只需打开提升的命令提示符,运行fsutil行为查询DisableDeleteNotify命令,您的SSD就会列出。0表示已启用,1表示已禁用。如

使用strings.Trim函数去除字符串首尾的指定字符集使用strings.Trim函数去除字符串首尾的指定字符集Jul 24, 2023 pm 04:27 PM

使用strings.Trim函数去除字符串首尾的指定字符集在Go语言中,strings.Trim函数是一个非常实用的函数,可以去除字符串首尾指定字符集,让字符串更加整洁和规范。本文将介绍如何使用strings.Trim函数,以及展示一些代码示例。首先我们来看一下strings.Trim函数的原型:funcTrim(sstring,cutsetstri

使用PHP函数 "trim" 去除字符串两端的空白字符使用PHP函数 "trim" 去除字符串两端的空白字符Jul 25, 2023 pm 04:45 PM

PHP函数"trim"是一种非常有用的字符串处理函数。它可以帮助我们去除字符串两端的空白字符,包括空格、制表符、换行符等等。在编写PHP程序时,我们经常会遇到需要清理用户输入的情况,这时候使用"trim"可以确保我们得到干净的字符串,避免因为用户输入不规范而引发错误。下面是一个简单的代码示例,展示了如何使用"trim"函数:<?php

PHP教程:学习使用trim函数去除中文空格PHP教程:学习使用trim函数去除中文空格Mar 27, 2024 am 11:00 AM

PHP教程:学习使用trim函数去除中文空格,需要具体代码示例在PHP开发中,经常会遇到需要处理字符串的情况,而其中一个常见的需求就是去除字符串两端的空格。在处理英文字符串时,我们可以直接使用PHP内置的trim函数来实现这一操作,但是当字符串中包含中文字符时,trim函数可能并不能正常去除中文空格,这时就需要使用一些特殊处理方法来实现我们的需求。本文将介绍

Java中trim如何用Java中trim如何用May 02, 2023 pm 01:31 PM

1、说明trim()是Java开发人员最常用的删除前导和尾随空格的方法。对于trim()方法,空格字符是指*ASCII值小于或等于32('U+0020')*的任何字符。trim()方法返回调用字符串对象的一个副本,但是所有起始和结尾都被删掉了。2、实例publicclassFunTester{publicstaticvoidmain(String[]args){Stringstring="onetwothree";System.out.prin

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools