-
Jul 13th, 2014, 09:28 PM
#1
Thread Starter
Banned
[RESOLVED] String edit!
This is a string ....
Physical Dmg
+8.5
Armor
+9
Ability Power
+19.6
Magic Resist at level 18
+15
How can I make that look like this:
Physical Dmg
+8.5
Armor
+9
Ability Power
+19.6
Magic Resist at level 18
+15
This can be random text but always written the same way, so I want the string always to remove certain blank lines but not all of them..
Last edited by berny22; Jul 15th, 2014 at 12:12 PM.
-
Jul 13th, 2014, 10:04 PM
#2
Re: String edit!
From where these blank lines come from?
If you build the string yourself then there isn't problem, just remove the unneeded lines.
-
Jul 13th, 2014, 10:24 PM
#3
Re: String edit!
try something like this:
Code:
Dim nl As String = Environment.NewLine
Dim s As String = "Physical Dmg" & _
nl & nl & _
"+8.5" & _
nl & nl & _
"Armor" & _
nl & nl & _
"+9" & _
nl & nl & _
"Ability Power" & _
nl & nl & _
"+19.6" & _
nl & nl & _
"Magic Resist at level 18" & _
nl & nl & _
"+15"
Dim lines As New List(Of String)(s.Split(New String() {nl}, StringSplitOptions.None))
For x As Integer = lines.Count - 2 To 1 Step -2
lines.RemoveAt(x)
Next
For x As Integer = lines.Count - 2 To 2 Step -2
lines.Insert(x, "")
Next
s = String.Join(nl, lines.ToArray)
TextBox1.Text = s
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 15th, 2014, 10:04 AM
#4
Thread Starter
Banned
Re: String edit!
Originally Posted by 4x2y
From where these blank lines come from?
If you build the string yourself then there isn't problem, just remove the unneeded lines.
If if was building the string myself I wouldn't be posting this question LOL!
Originally Posted by .paul.
try something like this:
Code:
Dim nl As String = Environment.NewLine
Dim s As String = "Physical Dmg" & _
nl & nl & _
"+8.5" & _
nl & nl & _
"Armor" & _
nl & nl & _
"+9" & _
nl & nl & _
"Ability Power" & _
nl & nl & _
"+19.6" & _
nl & nl & _
"Magic Resist at level 18" & _
nl & nl & _
"+15"
Dim lines As New List(Of String)(s.Split(New String() {nl}, StringSplitOptions.None))
For x As Integer = lines.Count - 2 To 1 Step -2
lines.RemoveAt(x)
Next
For x As Integer = lines.Count - 2 To 2 Step -2
lines.Insert(x, "")
Next
s = String.Join(nl, lines.ToArray)
TextBox1.Text = s
I said the text can be random, cuz It's downloaded from the internet. I don't make the string. Text and numbers will always be different but the blank lines will always be in the same place
-
Jul 15th, 2014, 10:17 AM
#5
Hyperactive Member
Re: String edit!
You can use trim function between the texts..
-
Jul 15th, 2014, 10:38 AM
#6
Re: String edit!
I built the string to test the code example, which will work with any string in that format.
How can you ever expect to be helped when you disregard every suggestion?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 15th, 2014, 10:39 AM
#7
Re: String edit!
Originally Posted by hamza.saleem
You can use trim function between the texts..
can you post an example to prove that?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 15th, 2014, 10:51 AM
#8
Thread Starter
Banned
Re: String edit!
Excuse me .paul. your code actually did work I just used it the wrong way. Sry and thank you!
Btw can you as well explain the code, I don't want to just copy - paste a code not knowing how it actually works.
Last edited by berny22; Jul 15th, 2014 at 10:55 AM.
-
Jul 15th, 2014, 10:54 AM
#9
Hyperactive Member
Re: String edit!
Originally Posted by .paul.
can you post an example to prove that?
Yes, but you have to post that code. i will settle down the code to give no spaces using this trim function
-
Jul 15th, 2014, 11:04 AM
#10
Re: String edit!
Code:
'this splits the string into lines and puts the lines in the list
Dim lines As New List(Of String)(s.Split(New String() {nl}, StringSplitOptions.None))
'this loops backwards through the list, starting at the first empty line
'if it looped forwards, the indices would be wrong because of the removing elements
For x As Integer = lines.Count - 2 To 1 Step -2
lines.RemoveAt(x)
Next
'this loops backwards through the list, starting at the first line where you want an empty line
'if it looped forwards, the indices would be wrong because of inserting elements
For x As Integer = lines.Count - 2 To 2 Step -2
lines.Insert(x, "")
Next
'this rejoins the list into a string
s = String.Join(nl, lines.ToArray)
'puts the string in a textbox
TextBox1.Text = s
Last edited by .paul.; Jul 15th, 2014 at 11:08 AM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 15th, 2014, 12:11 PM
#11
Thread Starter
Banned
Re: String edit!
Originally Posted by .paul.
Code:
'this splits the string into lines and puts the lines in the list
Dim lines As New List(Of String)(s.Split(New String() {nl}, StringSplitOptions.None))
'this loops backwards through the list, starting at the first empty line
'if it looped forwards, the indices would be wrong because of the removing elements
For x As Integer = lines.Count - 2 To 1 Step -2
lines.RemoveAt(x)
Next
'this loops backwards through the list, starting at the first line where you want an empty line
'if it looped forwards, the indices would be wrong because of inserting elements
For x As Integer = lines.Count - 2 To 2 Step -2
lines.Insert(x, "")
Next
'this rejoins the list into a string
s = String.Join(nl, lines.ToArray)
'puts the string in a textbox
TextBox1.Text = s
Thank you And sorry for saying its wrong, I'm stupid... xD
-
Jul 15th, 2014, 12:22 PM
#12
Hyperactive Member
Re: String edit!
Originally Posted by berny22
Thank you And sorry for saying its wrong, I'm stupid... xD
Haha have a nice day man :P
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|