Differences between Visual Basic Trim function and String.Trim() method

Today I recognized that Visual Basic Trim function removes spaces only, but String.Trim() method removes all white space characters(see Remarks here) including newline and carriage return.

The following C# sample confirms this:

      public void VBTrim_Test()

      {

            string str="string" + '\r' + '\n';

            Debug.WriteLine(String.Format("length of {0} is {1}", str,Microsoft.VisualBasic.Strings.Len(str)));

            str=Microsoft.VisualBasic.Strings.Trim(str);

            Debug.WriteLine(String.Format("After VB trim length of {0} is {1}", str,Microsoft.VisualBasic.Strings.Len(str)));

            str=str.Trim();

            Debug.WriteLine(String.Format("After String trim length of {0} is {1}", str,Microsoft.VisualBasic.Strings.Len(str)));

      }                                  

The output is following:

length of string

 is 8

After VB trim length of string

 is 8

After String trim length of string is 6

 

posted @ Friday, November 11, 2005 1:11 PM
Print

Comments on this entry:

# re: Differences between Visual Basic Trim function and String.Trim() method

Left by Beach Towels at 12/6/2006 1:20 PM
Gravatar
What happens if you want to keep the white spaces but be able to search charactor sets within a multi-word string? I can only get search result matches for whole words!

# re: Differences between Visual Basic Trim function and String.Trim() method

Left by Michael Freidgeim at 12/6/2006 1:28 PM
Gravatar
Beach Towels,
Sorry, I don't understand your question. Expand it please.

Regards,
Michael Freidgeim

# re: Differences between Visual Basic Trim function and String.Trim() method

Left by Kiran at 7/9/2007 8:11 PM
Gravatar
Both r more or less same

# spilit the character

Left by suresh at 8/15/2007 4:48 PM
Gravatar
i have one record in textbox.i want to that record can be spilited in one by one using command button

# re: Differences between Visual Basic Trim function and String.Trim() method

Left by Michael Freidgeim at 8/17/2007 9:51 PM
Gravatar
suresh,
Sorry, I don't understand your question.

#  Differences between Visual Basic Trim function and String.Trim() method

Left by Adithya at 1/20/2008 4:58 PM
Gravatar
can any one tell Why we Use trim in Vb

# re: Differences between Visual Basic Trim function and String.Trim() method

Left by Michael Freidgeim at 1/20/2008 9:55 PM
Gravatar
Adithya,
Trim is used in legacy(converted from VB6) code.
Also it sometimes simpler to avoid check for null/Nothing, e.g.
if (not str is Nothing) then
str=str.Trim()
end if

# re: Differences between Visual Basic Trim function and String.Trim() method

Left by Michael Freidgeim at 2/19/2008 10:49 PM
Gravatar
Rahul,
You can use TrimEnd(string str, string sEndValue)
from My StringHelper class http://geekswithblogs.net/mnf/archive/2006/07/13/84942.aspx .
Or write your own method similar to what I've wrote in that class.

# re: Differences between Visual Basic Trim function and String.Trim() method

Left by sreeraj at 5/27/2008 7:30 PM
Gravatar
in a program of vb6 i have seen that Trime and Trime$. i know what Trim is, but what is Trime$. is there any difference.plz iam a beginner.

# re: Differences between Visual Basic Trim function and String.Trim() method

Left by Michael Freidgeim at 5/27/2008 11:53 PM
Gravatar
See "Trim$ vs Trim" post http://forums.devx.com/archive/index.php/t-77024.html

# re: Differences between Visual Basic Trim function and String.Trim() method

Left by MindGrill at 11/19/2008 6:51 PM
Gravatar
Gud reference. For more VB help you can also look here

http://MindGrillQ.blogspot.com

# re:WHY WE USE TRIM OF VB

Left by RAXTAR at 2/9/2009 7:55 PM
Gravatar
KEEP IT UP BRO

# re: Differences between Visual Basic Trim function and String.Trim() method

Left by Ripon Paul at 3/4/2009 4:47 PM
Gravatar
Hi All,

Foe what purpose following two statements are used. Can somebody help me????

SendKeys "{Home}+{End}"

txtPassword.Text = Trim(txtPassword.Text)

# re: Differences between Visual Basic Trim function and String.Trim() method

Left by web development company at 8/11/2009 12:30 PM
Gravatar
Nice post,

I never get a text from textbox without triming it, it makes the database cleaner and give a more consistenat data

Thanks

# how to rectify the white space when we saved ?

Left by kutbu at 5/24/2010 7:14 AM
Gravatar
plz very urgent

# re: Differences between Visual Basic Trim function and String.Trim() method

Left by Michael Freidgeim at 5/24/2010 11:49 AM
Gravatar
kutbu,Expand your question- not clear what you asked

# re: Differences between Visual Basic Trim function and String.Trim() method

Left by msp at 6/21/2010 2:33 AM
Gravatar
Not got the answer...plz help...!

Your comment:



(not displayed)

 
 
 
 
 

Live Comment Preview:

 
«October»
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678