Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How to handle string with characters

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to handle string with characters

    Hi,

    Can anyone suggest on how we can handle the string with special characters. For instance:
    123456789BNJKNBJNBJKJKBNJKBJKNBJK"VJKNBJNHBJNJKBVJ KBJKNB"VHBVBBVNBVNBVBVBVBNBVNBNBJHFBVFJB FNVJBNVJNVJDFNVJKNVJKNVJKVNNVJ NN"


    I get some user inputs with double quotes and i need to split to 80 chars line.

    .length fails to get the length if it contains special characters like double quotes and ?

    Thanks,
    rkumar


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to handle string with characters

    Are you able to put it into a char array?
    char[] chars = str.toCharArray();
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,682
    Thanks
    53
    Thanked 2,421 Times in 2,374 Posts

    Default Re: How to handle string with characters

    .length fails to get the length if it contains special characters like double quotes and ?
    Can you post a small complete program that compiles, executes and shows the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    My Mood
    Bored
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to handle string with characters

    This is the sample string we get from another system
    123456789BNJKNBJNBJKJKBNJKBJKNBJK"VJKNBJNHBJNJKBVJ KBJKNB"VHBVBBVNBVNBVBVBVBNBVNBNBJHFBVFJB FNVJBNVJNVJDFNVJKNVJKNVJKVNNVJ NN

    so i tried to assign it to a string which eclipse complaints.
    String str = "123456789BNJKNBJNBJKJKBNJKBJKNBJK"VJKNBJNHBJNJKBV JKBJKNB"VHBVBBVNBVNBVBVBVBNBVNBNBJHFBVFJB FNVJBNVJNVJDFNVJKNVJKNVJKVNNVJ NN";

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to handle string with characters

    You need to escape special characters, like the "
    Use: \" to add a single " to a String
    For example:
    String str = "1234\"567"
    Is equal to:
    1234"567
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to handle string with characters



    If the String is coming from another system then internal quotes should not be a problem.
    Improving the world one idiot at a time!

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How to handle string with characters

    Quote Originally Posted by Junky View Post


    If the String is coming from another system then internal quotes should not be a problem.
    Yes, but it sounds like he manually copied and pasted the String into the code. If he did do that, the internal quotes will be a problem.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to handle string with characters

    That's why I'm confused. What exactly is the OP doing?
    Improving the world one idiot at a time!

Similar Threads

  1. Ignore certain characters in a string.
    By JaAnTr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2014, 03:18 PM
  2. Eliminating Unicode Characters and Escape Characters from String
    By bilalonjavaforum in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 19th, 2013, 05:26 AM
  3. Replies: 2
    Last Post: March 28th, 2013, 09:54 AM
  4. Split a string into individual characters
    By djl1990 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 6th, 2012, 03:55 PM
  5. How to check whether the string contains only the specified characters ????
    By j_kathiresan in forum Java Theory & Questions
    Replies: 3
    Last Post: April 30th, 2010, 08:49 AM