VB의 문자열에서 공백을 제거합니다.그물
VB에서 문자열에서 공백을 제거하는 방법은 무엇입니까?NET?
모든 공백을 제거하려면:
myString = myString.Replace(" ", "")
선행 및 후행 공백을 제거하는 방법
myString = myString.Trim()
참고: 공백이 제거되므로 새 줄, 탭 등이 제거됩니다.
2015년: 더 새로운 LINQ & 람다.
- 이것은 오래된 Q(및 답변)이기 때문에, 새로운 2015 방법으로 업데이트할 생각입니다.
- 원래 "공백"은 공백이 아닌 공백(예: 탭, 새 줄, 문단 구분 기호, 줄 바꿈, 캐리지 리턴 등)을 나타낼 수 있습니다.
- 또한 Trim()은 문자열의 앞/뒤에서 공백만 제거하며 문자열 내부의 공백은 제거하지 않습니다. 예: "선행 및 후행 공백"이 "선행 및 후행 공백"이 되지만 내부 공간은 여전히 존재합니다.
Function RemoveWhitespace(fullString As String) As String
Return New String(fullString.Where(Function(x) Not Char.IsWhiteSpace(x)).ToArray())
End Function
이렇게 하면 모든 (흰색) 공백, 선행, 후행 및 문자열 내에서 제거됩니다.
원래 게시물의 "공간"은 공백을 나타낼 수 있으며, 문자열에서 모든 공백을 제거하는 방법을 보여주는 답변은 아직 없습니다.그 규칙적인 표현은 제가 찾은 가장 유연한 접근법입니다.
아래는 공백만 교체하는 것과 모든 공백을 교체하는 것의 차이를 확인할 수 있는 콘솔 응용 프로그램입니다.
자세한 내용은 에 대해 확인할 수 있습니다.http://msdn.microsoft.com/en-us/library/hs600312.aspx 및 http://msdn.microsoft.com/en-us/library/az24scfc.aspx 의 NET 정규 표현식
Imports System.Text.RegularExpressions
Module TestRegExp
Sub Main()
' Use to match all whitespace (note the lowercase s matters)
Dim regWhitespace As New Regex("\s")
' Use to match space characters only
Dim regSpace As New Regex(" ")
Dim testString As String = "First Line" + vbCrLf + _
"Second line followed by 2 tabs" + vbTab + vbTab + _
"End of tabs"
Console.WriteLine("Test string :")
Console.WriteLine(testString)
Console.WriteLine("Replace all whitespace :")
' This prints the string on one line with no spacing at all
Console.WriteLine(regWhitespace.Replace(testString, String.Empty))
Console.WriteLine("Replace all spaces :")
' This removes spaces, but retains the tabs and new lines
Console.WriteLine(regSpace.Replace(testString, String.Empty))
Console.WriteLine("Press any key to finish")
Console.ReadKey()
End Sub
End Module
줄에 공백이 두 개 이상 포함되지 않도록 문자열을 아래로 자릅니다.2개 이상의 공간이 있는 모든 인스턴스는 1개의 공간으로 축소됩니다.간단한 솔루션:
While ImageText1.Contains(" ") '2 spaces.
ImageText1 = ImageText1.Replace(" ", " ") 'Replace with 1 space.
End While
Regex는?솔루션을 교체하시겠습니까?
myStr = Regex.Replace(myStr, "\s", "")
공백만 제거되고 rtrim(ltrim(myString))의 SQL 기능과 일치합니다.
Dim charstotrim() As Char = {" "c}
myString = myString .Trim(charstotrim)
또한 공간을 순환하여 제거하는 작은 기능을 사용할 수도 있습니다.
이것은 매우 깨끗하고 간단합니다.
Public Shared Function RemoveXtraSpaces(strVal As String) As String
Dim iCount As Integer = 1
Dim sTempstrVal As String
sTempstrVal = ""
For iCount = 1 To Len(strVal)
sTempstrVal = sTempstrVal + Mid(strVal, iCount, 1).Trim
Next
RemoveXtraSpaces = sTempstrVal
Return RemoveXtraSpaces
End Function
이 코드를 시도해 보십시오.trim
a String
Public Function AllTrim(ByVal GeVar As String) As String
Dim i As Integer
Dim e As Integer
Dim NewStr As String = ""
e = Len(GeVar)
For i = 1 To e
If Mid(GeVar, i, 1) <> " " Then
NewStr = NewStr + Mid(GeVar, i, 1)
End If
Next i
AllTrim = NewStr
' MsgBox("alltrim = " & NewStr)
End Function
언급URL : https://stackoverflow.com/questions/1645546/remove-spaces-from-a-string-in-vb-net
'sourcecode' 카테고리의 다른 글
findOneAndDelete()와 findOneAndRemove()의 차이 (0) | 2023.05.14 |
---|---|
PyMongo를 사용하여 MongoDB 데이터베이스를 삭제하려면 어떻게 해야 합니까? (0) | 2023.05.14 |
Html의 차이점은 무엇입니까?부분(뷰, 모델) 및 Html.MVC2에서 부분 렌더링(뷰, 모델)? (0) | 2023.05.14 |
Git에서 FETCH_HEAD는 무엇을 의미합니까? (0) | 2023.05.14 |
이클립스에서 블록에 대한 의견을 어떻게 제시합니까? (0) | 2023.05.14 |