Foro de elhacker.net

Programación => Java => Mensaje iniciado por: + 1 Oculto(s) en 20 Mayo 2016, 01:39 am



Título: como trabajar con string que contenga ""
Publicado por: + 1 Oculto(s) en 20 Mayo 2016, 01:39 am
me explico tengo esta cadena

"To be or not to be," quoth the Bard, "that
is the question".

ahora tengo que trabajar sobre esa cadena y al pasarlo como parametro me sale error, bueno es obvio ya que

quoth the Bard      no se encuentra entre comillas


como se soluciona ?


Título: Re: como trabajar con string que contenga ""
Publicado por: engel lex en 20 Mayo 2016, 01:43 am
no estoy seguro si en java, pero normalmente cundo quieres incluir comillas o caracteres especiales lo escapas (le colocas un \ previo para que se asuma literal, ejemplo "el dijo: \"hola mundo\"")


Título: Re: como trabajar con string que contenga ""
Publicado por: AlbertoBSD en 20 Mayo 2016, 01:47 am
Es correcto con \"

Mas info es http://stackoverflow.com/questions/1367322/what-are-all-the-escape-characters-in-java


Título: Re: como trabajar con string que contenga ""
Publicado por: + 1 Oculto(s) en 20 Mayo 2016, 01:49 am
tengo que trabajar sobre las comillas:

Sample Input

"To be or not to be," quoth the Bard, "that
is the question".
The programming contestant replied: "I must disagree.
To `C' or not to `C', that is The Question!"

Sample Output

``To be or not to be,'' quoth the Bard, ``that
is the question''.
The programming contestant replied: ``I must disagree.
To `C' or not to `C', that is The Question!''



Título: Re: como trabajar con string que contenga ""
Publicado por: AlbertoBSD en 20 Mayo 2016, 01:52 am
Cual es el codigo que las lee y cual es que las manda a pantalla?


Título: Re: como trabajar con string que contenga ""
Publicado por: + 1 Oculto(s) en 20 Mayo 2016, 01:55 am
bueno este es el problema que quiero resolver

TeX Quotes

TeX is a typesetting language developed by Donald Knuth. It takes source text together with a few typesetting instructions and produces, one hopes, a beautiful document. Beautiful documents use `` and " to delimit quotations, rather than the mundane " which is what is provided by most keyboards. Keyboards typically do not have an oriented double-quote, but they do have a left-single-quote ` and a right-single-quote '. Check your keyboard now to locate the left-single-quote key ` (sometimes called the ``backquote key") and the right-single-quote key ' (sometimes called the ``apostrophe" or just ``quote"). Be careful not to confuse the left-single-quote ` with the ``backslash" key \. TeX lets the user type two left-single-quotes `` to create a left-double-quote `` and two right-single-quotes '' to create a right-double-quote ''. Most typists, however, are accustomed to delimiting their quotations with the un-oriented double-quote ".

If the source contained

"To be or not to be," quoth the bard, "that is the question."

then the typeset document produced by TeX would not contain the desired form:

``To be or not to be," quoth the bard, ``that is the question."

In order to produce the desired form, the source file must contain the sequence:

``To be or not to be,'' quoth the bard, ``that is the question.''

You are to write a program which converts text containing double-quote (") characters into text that is identical except that double-quotes have been replaced by the two-character sequences required by TeX for delimiting quotations with oriented double-quotes. The double-quote (") characters should be replaced appropriately by either `` if the " opens a quotation and by '' if the " closes a quotation. Notice that the question of nested quotations does not arise: The first " must be replaced by ``, the next by '', the next by ``, the next by '', the next by ``, the next by '', and so on.

Input and Output

Input will consist of several lines of text containing an even number of double-quote (") characters. Input is ended with an end-of-file character. The text must be output exactly as it was input except that:

    the first " in each pair is replaced by two ` characters: `` and
    the second " in each pair is replaced by two ' characters: ''.

Sample Input

"To be or not to be," quoth the Bard, "that
is the question".
The programming contestant replied: "I must disagree.
To `C' or not to `C', that is The Question!"

Sample Output

``To be or not to be,'' quoth the Bard, ``that
is the question''.
The programming contestant replied: ``I must disagree.
To `C' or not to `C', that is The Question!''




mi codigo es el siguiente aunque no esta bien creo

Código
  1. public class TexQuotes {
  2. public String tes(String textoMain){
  3.    char caracter=' ';
  4.    int contador=0;
  5.    String answer="";
  6.    for (int i = 0; i < textoMain.length(); i++) {
  7.        caracter=textoMain.charAt(i);
  8.  
  9.        if(caracter==34){
  10.            contador=1;
  11.            if(contador==1){
  12.            answer=answer+96;
  13.            }else{
  14.            answer=answer+caracter;
  15.            contador=0;
  16.            }
  17.        }else{
  18.        answer=answer+caracter;
  19.        }
  20.    }
  21.    System.out.println(answer);
  22.    return answer;
  23. }    
  24. public static void main(String args[]){
  25.    TexQuotes t=new TexQuotes();
  26.    t.tes("To be or not to be," quoth the Bard, "that is the question".);
  27. }
  28. }
  29.  


Título: Re: como trabajar con string que contenga ""
Publicado por: engel lex en 20 Mayo 2016, 02:00 am
algo que creo es que tienes un error XD colocan el quote de cerrado como comilla simple, pero es acento es decir

``aquí inicia, pero aquí termina´´
``aquí inicia y esto es algo diferente''

Código
  1. t.tes("\"To be or not to be,\" quoth the Bard, \"that is the question\".");


Título: Re: como trabajar con string que contenga ""
Publicado por: + 1 Oculto(s) en 20 Mayo 2016, 02:26 am
Citar
``aquí inicia, pero aquí termina´´
``aquí inicia y esto es algo diferente''

te refieres al input y output???


Título: Re: como trabajar con string que contenga ""
Publicado por: engel lex en 20 Mayo 2016, 02:43 am
las comillas

el texto está
Citar
``To be or not to be,'' quoth the Bard, ``that
is the question''.
The programming contestant replied: ``I must disagree.
To `C' or not to `C', that is The Question!''

pero las smart quotes son (el mismo texto lo dice)
Citar
``To be or not to be,´´ quoth the Bard, ``that
is the question´´.
The programming contestant replied: ``I must disagree.
To `C´ or not to `C´, that is The Question!´´


Título: Re: como trabajar con string que contenga ""
Publicado por: + 1 Oculto(s) en 20 Mayo 2016, 04:31 am
muchas gracias por la ayuda me sirvio de mucho