Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to trim a string and convert it to lower case

Tags:

c#

.net

windows

I've written a class for processing strings and I have the following problem: the string passed in can come with spaces at the beginning and at the end of the string.

I need to trim the spaces from the strings and convert them to lower case letters. My code so far:

var searchStr = wordToSearchReplacemntsFor.ToLower();
        searchStr = searchStr.Trim();

I couldn't find any function to help me in StringBuilder. The problem is that this class is supposed to process a lot of strings as quickly as possible. So I don't want to be creating 2 new strings for each string the class processes.

If this isn't possible, I'll go deeper into the processing algorithm.

like image 641
ghet Avatar asked Mar 14 '11 19:03

ghet


People also ask

How do I convert a string to lowercase?

The toLowerCase() method converts a string to lower case letters. Note: The toUpperCase() method converts a string to upper case letters.

How can we convert a string to uppercase and lowercase?

An array of char type s[100] is declared which will store the entered string by user. Then, for loop is used to convert the string into upper case string and if block is used to check that if characters are in lower case then, convert them in upper case by subtracting 32 from their ASCII value.

How do I convert a string to lowercase in python?

lower() In Python, lower() is a built-in method used for string handling. The lower() methods returns the lowercased string from the given string. It converts all uppercase characters to lowercase.

Which function is used for converting a string into lower case?

The strtolower() function converts a string to lowercase.


1 Answers

Try method chaining.

Ex:

var s = " YoUr StRiNg".Trim().ToLower();
like image 195
Cyberdrew Avatar answered Oct 02 '22 03:10

Cyberdrew