Results 1 to 12 of 12

Thread: [RESOLVED] String edit!

  1. #1

    Thread Starter
    Banned
    Join Date
    Jul 2014
    Posts
    221

    Resolved [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.

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    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.



  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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

  4. #4

    Thread Starter
    Banned
    Join Date
    Jul 2014
    Posts
    221

    Re: String edit!

    Quote Originally Posted by 4x2y View Post
    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!


    Quote Originally Posted by .paul. View Post
    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

  5. #5
    Hyperactive Member
    Join Date
    Jun 2014
    Location
    Lahore, Pakistan
    Posts
    450

    Re: String edit!

    You can use trim function between the texts..

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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?

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: String edit!

    Quote Originally Posted by hamza.saleem View Post
    You can use trim function between the texts..
    can you post an example to prove that?

  8. #8

    Thread Starter
    Banned
    Join Date
    Jul 2014
    Posts
    221

    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.

  9. #9
    Hyperactive Member
    Join Date
    Jun 2014
    Location
    Lahore, Pakistan
    Posts
    450

    Re: String edit!

    Quote Originally Posted by .paul. View Post
    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

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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.

  11. #11

    Thread Starter
    Banned
    Join Date
    Jul 2014
    Posts
    221

    Re: String edit!

    Quote Originally Posted by .paul. View Post
    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

  12. #12
    Hyperactive Member
    Join Date
    Jun 2014
    Location
    Lahore, Pakistan
    Posts
    450

    Re: String edit!

    Quote Originally Posted by berny22 View Post
    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
  •  



Click Here to Expand Forum to Full Width