Geeks With Blogs

Michael Freidgeim's Blog MS .Net Development

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 on Friday, November 11, 2005 1:11 PM .Net Framework | Back to top


Comments on this post: Differences between Visual Basic Trim function and String.Trim() method

# re: Differences between Visual Basic Trim function and String.Trim() method
Requesting 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!
Left by Beach Towels on Dec 06, 2006 1:20 PM

# re: Differences between Visual Basic Trim function and String.Trim() method
Requesting Gravatar...
Beach Towels,
Sorry, I don't understand your question. Expand it please.

Regards,
Michael Freidgeim
Left by Michael Freidgeim on Dec 06, 2006 1:28 PM

# re: Differences between Visual Basic Trim function and String.Trim() method
Requesting Gravatar...
Both r more or less same
Left by Kiran on Jul 09, 2007 8:11 PM

# spilit the character
Requesting Gravatar...
i have one record in textbox.i want to that record can be spilited in one by one using command button
Left by suresh on Aug 15, 2007 4:48 PM

# re: Differences between Visual Basic Trim function and String.Trim() method
Requesting Gravatar...
suresh,
Sorry, I don't understand your question.
Left by Michael Freidgeim on Aug 17, 2007 9:51 PM

# Differences between Visual Basic Trim function and String.Trim() method
Requesting Gravatar...
can any one tell Why we Use trim in Vb
Left by Adithya on Jan 20, 2008 4:58 PM

# re: Differences between Visual Basic Trim function and String.Trim() method
Requesting 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
Left by Michael Freidgeim on Jan 20, 2008 9:55 PM

# re: Differences between Visual Basic Trim function and String.Trim() method
Requesting 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.
Left by Michael Freidgeim on Feb 19, 2008 10:49 PM

# re: Differences between Visual Basic Trim function and String.Trim() method
Requesting 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.
Left by sreeraj on May 27, 2008 7:30 PM

# re: Differences between Visual Basic Trim function and String.Trim() method
Requesting Gravatar...
See "Trim$ vs Trim" post http://forums.devx.com/archive/index.php/t-77024.html
Left by Michael Freidgeim on May 27, 2008 11:53 PM

# re: Differences between Visual Basic Trim function and String.Trim() method
Requesting Gravatar...
Gud reference. For more VB help you can also look here

http://MindGrillQ.blogspot.com
Left by MindGrill on Nov 19, 2008 6:51 PM

# re:WHY WE USE TRIM OF VB
Requesting Gravatar...
KEEP IT UP BRO
Left by RAXTAR on Feb 09, 2009 7:55 PM

# re: Differences between Visual Basic Trim function and String.Trim() method
Requesting Gravatar...
Hi All,

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

SendKeys "{Home}+{End}"

txtPassword.Text = Trim(txtPassword.Text)
Left by Ripon Paul on Mar 04, 2009 4:47 PM

# re: Differences between Visual Basic Trim function and String.Trim() method
Requesting 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
Left by web development company on Aug 11, 2009 12:30 PM

# how to rectify the white space when we saved ?
Requesting Gravatar...
plz very urgent
Left by kutbu on May 24, 2010 7:14 AM

# re: Differences between Visual Basic Trim function and String.Trim() method
Requesting Gravatar...
kutbu,Expand your question- not clear what you asked
Left by Michael Freidgeim on May 24, 2010 11:49 AM

# re: Differences between Visual Basic Trim function and String.Trim() method
Requesting Gravatar...
Not got the answer...plz help...!
Left by msp on Jun 21, 2010 2:33 AM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net | Join free