Putting the '~' sign in a text;

A get a string that sometimes is 130 to 590 at length. I need to seperate this string by putting the ~ sign in every 160 charecter postition. So if the string is 580 at length I need to add to the string ~ at postition 160 and 320 and 480. Is there any easy way to do this ?  
yngviAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

heyhey_Commented:
very quick example



public class test
{
  public static String seperateString(String st, char ch, int n)
  {
    String t = "";
    if (n < 1) n = 1;
    while (st.length() > 0)
    {
      if (t.length() > 0) t += ch;
      t += st.substring(0, n);
      st = st.substring(n);
    }
    return t;
  }
  public static void main(String[] args)
  {
    String st = "";
    for (int i = 33; i < 48; i++)
    {
      st += (char)i;
      st += (char)i;
      st += (char)i;
      st += (char)i;
      st += (char)i;
      st += (char)i;
    }
   
    System.out.println(st);
    st = seperateString(st, '~', 3);
    System.out.println("--");
    System.out.println(st);

  }
}
0
heyhey_Commented:
you'll have to use it with

st = seperateString(st, '~', 160);
0
vladi21Commented:
Better use StringBuffer and dont append String within a loop
0
yngviAuthor Commented:
That did not seem to work. If I used the seperatestring method I got IndexOutOfBoundException error.
I tried it like this :

  public static void main(String[] args)
  {
    String st = "bla bla bla bla bla";
     
    System.out.println(st);
    st = seperateString(st, '~', 3);
    System.out.println("--");
    System.out.println(st);

  }
}

What I should have got out of this was the string = "bla~bla~bla~bla~... etc. instead I got exception
0
s_franklinCommented:
String parsing is always fun - this example works based on my five minutes of unit testing :) Note that this is not optimized for speed or size - just straightforward code that you should be able to tweak as you like.

public static String split(String str, String split, int interval) {
  String splitString = "";
  int stringWidth = str.length();

  // Warning: Embedded return to avoid division by zero or negative
  if ( interval <= 0 ) {
    return str;
  }      

  int delimCount = stringWidth/interval;

  int startPos = 0, endPos = 0;
  for (int i=0; i<= delimCount; i++) {
    startPos = i*interval;
    endPos = (i+1)*interval;
    if (endPos > stringWidth ) {
      // Don't go beyond end of string
      endPos = stringWidth;
    }
    splitString += str.substring(startPos,endPos);
    if ( i != delimCount ) {
      splitString += split;
    }
  }

  return splitString;
}

Here are sample function calls to test it a bit:

System.out.println("IN: \"123456789012345678901234567890\",\"-\",3");
System.out.println("OUT: " + split("123456789012345678901234567890","-",3));
System.out.println("IN: \"\",\"-\",5)");
System.out.println("OUT: " + split("","-",5));
System.out.println("IN: \"123456789012345678901234567890\",\"\",3");
System.out.println("OUT: " + split("123456789012345678901234567890","",3));
System.out.println("IN: \"123456789012345678901234567890\",\"---\",3");
System.out.println("OUT: " + split("123456789012345678901234567890","---",3));
System.out.println("IN: \"123456789012345678901234567890\",\"---\",0");
System.out.println("OUT: " + split("123456789012345678901234567890","---",0));
System.out.println("IN: \"123456789012345678901234567890\",\"---\",-1");
System.out.println("OUT: " + split("123456789012345678901234567890","---",-1));
System.out.println("IN: \"123456789012345678901234567890\",\"---\",30");
System.out.println("OUT: " + split("123456789012345678901234567890","---",30));
System.out.println("IN: \"123456789012345678901234567890\",\"---\",31");
System.out.println("OUT: " + split("123456789012345678901234567890","---",31));

Here's the output - you may have to make a simple change if you didn't want the last delimiter to go in at termination of the string when the interval evenly divides into the string length. That's pretty easy so you can change that yourself if you need to.

IN: "123456789012345678901234567890","-",3
OUT: 123-456-789-012-345-678-901-234-567-890-
IN: "","-",5)
OUT:
IN: "123456789012345678901234567890","",3
OUT: 123456789012345678901234567890
IN: "123456789012345678901234567890","---",3
OUT: 123---456---789---012---345---678---901---234---567---890---
IN: "123456789012345678901234567890","---",0
OUT: 123456789012345678901234567890
IN: "123456789012345678901234567890","---",-1
OUT: 123456789012345678901234567890
IN: "123456789012345678901234567890","---",30
OUT: 123456789012345678901234567890---
IN: "123456789012345678901234567890","---",31
OUT: 123456789012345678901234567890

Steve
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.