Click here to Skip to main content
15,921,959 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

This is how my text looks like:
\blabla1\73\blabla2\54\blabla3\21\blabla4\blabla6\34...

and it goes on until let'say \blabla500\ (even though I don't think it really matters.)

I want my form to display:
blabla1:73
blabla2:54
blabla3:21
...


I don't know if it helps but in the eyes of those editing texts like these, the strings are in the "\ \" symbols and not the integers.
Posted
Comments
Sergey Alexandrovich Kryukov 29-Dec-13 20:18pm    
Look, what's going on? Do you understand that this is a forum for software developers? And a software developer does such things with closed eyes. Do you want to know what does a really inexperienced software developer? Such person, who cannot do it with closed eyes, does it with open eyes, and, in absolutely worst rare cases, shows her/his code and asks for further advice on this forum.
Here is what you really need to read: what have you tried?
—SA

Hi,

you can use instr command to achieve this

example:
'TextString = input string where the last char is not a '\'
dim Index as long, TextString as string, Result as string
index=1
Result=""
do until instr(Index,TextString,"\",CompareMethod.Text)=0
Index = instr(Index,TextString,"\",CompareMethod.Text)+1
result = result & mid(TextString,index, instr(Index,TextString,"\",CompareMethod.Text)-index) & ":"
Index = instr(Index,TextString,"\",CompareMethod.Text)+1
result = result & mid(TextString,index, instr(Index,TextString,"\",CompareMethod.Text)-index) & vbcrlf
loop
 
Share this answer
 
VB
Imports System.Text.RegularExpressions


' Assumes the delimiter is always \.
Dim data As String ="\blabla1\73\blabla2\54\blabla3\21\blabla4\blabla6\34"
Dim fragments As String() = data.Split(new Char(){"\"}, _
                                       StringSplitOptions.RemoveEmptyEntries)

' Providing that each fragment is {alpha sequence}{digit sequence}
' this'll work.  If you have alternating alpha and numeric sub-strings
' within a fragment it won't.
For Each fragment As String In fragments
 ' Pick out the non-digit prefix
 Console.Write(string.Format("{0}:", Regex.Match(fragment,"\D{1,}", RegexOptions.Multiline)))
 ' Pick out the digit suffix
  Console.WriteLine(Regex.Match(fragment,"\d{1,}", RegexOptions.Multiline))
Next


You could also split the source string using a regex, but that'd be overkill in this case.

The nice thing about using the regex to pick out the numeric part is that you don't have to worry about fragments that don't have an alpha prefix. Nor do you have to write lots of your own string splitting code that you then have to maintain.
 
Share this answer
 
v7

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900