Resolved Get text after character

avreca-mortua

Member
Joined
Sep 4, 2012
Messages
6
Programming Experience
Beginner
Edit: not before, after
I want to get the text after the "="
if I have this "hello1=hello2" I want to get the "hello2" text

How can I do that? thanks and sorry for my English

Solved using maths and this example
Trimming and Removing Characters
thanks for your answers
jmcilhinney & lotok
 
Last edited:
In your example "hello2" comes after the "=", so do you want the substring before or after the "="? Regardless, you can call IndexOf on your String to get the index of the "=" character and then call Substring, using a bit of simple maths to determine what arguments to pass it.
 
Re: Get text before character

Or split the string on the = , then use 0 index on array for first half and 1 for other half.

Sent using XT910 with Android 4.0.4 and MIUI 2.8.10
 
Currently I have this for reading ini files:

VB.NET:
Dim INIFile As String = "Config.ini"
Dim INIListBox As New ListBox

Dim StartupSoundKey As Integer
Dim StartupSound As String

INIListBox.Items.AddRange(IO.File.ReadAllLines(INIFile))

StartupSoundKey = INIListBox.FindString("StartUpSound=")
StartupSound = INIListBox.Items(StartupSoundKey).ToString.Replace("StartUpSound=", "")

So it works very well but if the StartUpSound is StartUpSound.ogg then I get only a ".ogg" string

when I use
VB.NET:
StartupSound = INIListBox.Items(StartupSoundKey).ToString.IndexOf("=")

what else I have to do? is there any easiest way to read ini keys?

Edit: Sorry silly question I just figure it out I have to delete all characters before the IndexOf, I'll give it a try
 
Last edited:
Or split the string on the = , then use 0 index on array for first half and 1 for other half.

Sent using XT910 with Android 4.0.4 and MIUI 2.8.10

Yeah, that's better. I was trying to be too clever, which is an impossible dream anyway. ;)
 
Yeah, that's better. I was trying to be too clever, which is an impossible dream anyway. ;)

Both ways are good, and substring is safer, unless one foresees the catch: if there are further ocurrences of the chosen splitting character, retrieving the second element of the resulting array will truncate the expression, UNLESS we set the optional "limit" parameter to 2 - that is, to split the expression in no more than 2 pieces. If there was not this smart option, then we would have to count array's elements and concatenate all but the first again.

In code they stand so:

    Function TextAfterChar1(ByVal text As String, splitter As Char) As String
        If Not text.Contains(splitter) Then Return ""
        Return text.Substring(text.IndexOf(splitter) + 1)
    End Function

    Function TextAfterChar2(ByVal text As String, splitter As Char) As String
        If Not text.Contains(splitter) Then Return ""
        Return Split(text, splitter.ToString, 2)(1)
    End Function
 

Latest posts

Back
Top