Android Question insert string into string without spliting

ArminKH

Well-Known Member
hi
i want insert a string to other string without spliting that(i want a faster way because my string is in a while loop and by using split the speed of that is slow)
after hours i found string builder.insert useful for doing this
but after many test and play with that i think the spliting and then again join splited texts is faster than string builder.insert
now please help me to find fastest way to insert a string into other string at special index without spliting that
i think reflection or java object are so fast every time
or any other waaaaaay just FASTEST
tnx
 

stevel05

Expert
Licensed User
Longtime User
The string object is immutable, which is why you have to create new strings from parts of the original and join them around the text you want to insert to create another new string. In a brief arbitrary test there wasn't much in it, but if you are doing multiple operations on the same string, I believe that String Builder should be faster.
 
Upvote 0

ArminKH

Well-Known Member
Tnx again stevel
For example i want insert a space after third word in this string("this is test text")
Which way is fastet?
1.Split all into array and insert space into third word and join again splited words
2.use stringbuilder.insert
3.use text.indexof(target word) and then split text into 2 part and after insert space again join 2 part

In a long loop it seen way 1 is faster
But i think way 2 should be faster
I dont know what is the algoritm of string builder.insert?it insert in a string directly without spliting?and if yes why it seen slower than split and join ?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
I guess the third word is something random?

this one line method is probably the shortest and easiest method.

B4X:
Dim t As String = "this is test text"
Log(t.Replace(Regex.Split(" ",t)(2),Regex.Split(" ",t)(2)&" "))

output is

B4X:
this is test  text
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Which way is fastet?
I think it depends on the object you are acting on. If it's a new string each time (you have created a new one with Dim, or assigned a different string from a file), then judging by the results of my brief test, I wouldn't expect to see much difference. If you are manipulating the same string multiple times, then I would expect string builder to be a lot faster.

The bottom line is, test it in the context of what you are doing. Then you will know which is fastest for that particular solution.
 
Upvote 0

ArminKH

Well-Known Member
I think it depends on the object you are acting on. If it's a new string each time (you have created a new one with Dim, or assigned a different string from a file), then judging by the results of my brief test, I wouldn't expect to see much difference. If you are manipulating the same string multiple times, then I would expect string builder to be a lot faster.

The bottom line is, test it in the context of what you are doing. Then you will know which is fastest for that particular solution.
Tnx stevel
iknow the difference is a bit for example:
We have a text(linecount =20 and in each line we have 10 word)now i want join a character into each word
After my test:
By using split => add my char => join again the insert time is 0.67 second
By using string builder.insert,insert time is 0.72
I know thats a bit difference but in a text with 1000 line the insert time difference is about 0.2 or 0.3 second
and at last thank u again for your response
Thank u mr stevel :)
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
why don't you add that extra char where the data is generated?

or is the data coming from a 3rd party ?
 
Upvote 0
Top