Android Question Remove block of text mid string

William Hunter

Active Member
Licensed User
Longtime User
Is there a way to remove a block of text mid string, starting with a string of known characters, and ending with a string of known characters? For instance - how would I remove "block of text" from "Remove block of text mid string"? I want to remove the portion of the string starting with block and ending with text.

Best regards :)
 

Mahares

Expert
Licensed User
Longtime User
B4X:
Dim str1 As String = "Remove block of text mid string"
    Dim str2 As String =str1.Replace( "block of text ","")
    Log(str2)  'will display: Remove mid string
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
B4X:
Dim str1 As String = "Remove block of text mid string"
    Dim str2 As String =str1.Replace( "block of text ","")
    Log(str2)  'will display: Remove mid string
Thank you Mahares. I should have described my need more accurately. Let's say I have a string containing 100 words, and I want to remove a block of words starting with the 10th word and ending with the 80th word. I do not know their position in the string, I only know their spelling. ie block blah blah blah text.

Regards :)
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
I did it in notepad, so I'm not sure if it works...

B4X:
dim len = myString.Lenght
dim startSelection = -1 as int
dim endSelection = -1 as int
dim wordCount = 0 as int

for i = 0 to (len - 1)
    if myString.charAt(i) = " " or myString.charAt(i) = CRLF or myString.charAt(i) = TAB then wordCount = wordCount + 1 'Assuming single spaces between words
    if wordCount = 10 then startSelection = i + 1
    if wordCount = 80 then endSelection   = i
next

if startSelection >= 0 and startSelection < len and endSelection > 0 and endSelection <= len then
    myString = myString.replace(myString.subString2(startSelection, endSelection), "")
end if
 
Upvote 0

William Hunter

Active Member
Licensed User
Longtime User
I did it in notepad, so I'm not sure if it works...

B4X:
dim len = myString.Lenght
dim startSelection = -1 as int
dim endSelection = -1 as int
dim wordCount = 0 as int

for i = 0 to (len - 1)
    if myString.charAt(i) = " " or myString.charAt(i) = CRLF or myString.charAt(i) = TAB then wordCount = wordCount + 1 'Assuming single spaces between words
    if wordCount = 10 then startSelection = i + 1
    if wordCount = 80 then endSelection   = i
next

if startSelection >= 0 and startSelection < len and endSelection > 0 and endSelection <= len then
    myString = myString.replace(myString.subString2(startSelection, endSelection), "")
end if
Thank you wonder. I didn't test your code as I would have to know the position of the word by WordCount, and the position of the word is not static. I appreciate your efforts.

Regards :)
 
Upvote 0
Top