Solved

VB.NEt - Find Position of the First Character in a String

Posted on 2015-01-14
8
359 Views
Last Modified: 2015-01-21
Good Day Experts!

I have another inquiry that I cannot seem to figure out how to achieve.

I have a string of data that I need to find the position of the first character other than the leading spaces.  This is so I can determine when the line pattern changes.

     12345Order
     5678Order
  5678Total

Is it possible to get the position of first character in a string other than spaces?

Thanks,
jimbo99999
0
Comment
Question by:Jimbo99999
8 Comments
 
LVL 24

Assisted Solution

by:Phillip Burton
Phillip Burton earned 250 total points
ID: 40549258
Len(myString)-Len(LTrim(mystring))+1
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 40549389
See "Regular Expression Language - Quick Reference":
http://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx

Use the "\S" pattern (case-sensitive!) to match the first non-white-space character.

Example:
Dim data As String = "     5678Order"
Dim index As Integer = System.Text.RegularExpressions.Regex.Match(data, "\S").Index ' Find first non-white-space character

Open in new window


*You could alternatively use "\d" to find the first decimal digit.
0
 

Expert Comment

by:Paul Weegar
ID: 40549762
IS there a need for the leading space(s)?  If you don't need them, use the trim function (which removes leading and trailing spaces). Then use the Instr function to locate whatever you need to find.
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 11

Expert Comment

by:LordWabbit
ID: 40549997
All valid responses, although I can't help mentioning my favorite quote about RegEx.  "When you have a problem, use regular expressions, and now you have two problems".  A simple trim should be sufficient to remove leading and trailing spaces.  However if you are needing to trim things and seek for line pattern changes to convert text to valid data maybe your parsing strategy needs to be changed.
0
 

Author Comment

by:Jimbo99999
ID: 40551362
Hello everyone. Yes the leading spaces are required as I am using it to trigger when to stop processing.  As I process the Order lines(which can vary in number 1 or 2 or 10 etc), I am wanting to look for the leading spaces.  Then as soon as the amount of leading spaces change then I know to stop processing.

     12345Order
     5678Order
  5678Total
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 40551424
You could also explicitly check for a space at the beginning using StartsWith(), then Trim() it afterwards to get rid of the spaces:
        If line.StartsWith(" ") Then
            line = line.Trim
            ' ... now do something with "line" ...
        Else
            ' ... do something else ...
        End If

Open in new window

0
 

Author Comment

by:Jimbo99999
ID: 40556682
I am still working on this over the weekend trying all comments.
0
 

Author Closing Comment

by:Jimbo99999
ID: 40562018
Thanks everyone...it is working.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
I need help using System.Web.HttpUtility.HtmlEncode in my VB.Net application 3 79
Sql server insert 13 32
Footer for each row on Gridview 2 21
vb.net 1 month apart 11 31
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question