I want to know how I can get the last character of any string with GO. So far the only thing I could do is get each character from a string.
for i := 0; i < len(palabra); i++ {
fmt.Println(string(len(palabra) [i]))
}
I want to know how I can get the last character of any string with GO. So far the only thing I could do is get each character from a string.
for i := 0; i < len(palabra); i++ {
fmt.Println(string(len(palabra) [i]))
}
I'm learning Lua and I know that it is possible to create a multiline string using [[
and ]]
, that is, it is possible to do this:
html = [[
<body>
<head>
</head>
<body>
</body>
</body>
]]
print(html)
The result would be:
<body>
<head>
</head>
<body>
</body>
</body>
Now I want the same thing with an example using XML and CDATA , since for the purposes of my tests I am trying to mix the brackets. I have defined this in my script:
xml = [[
<![CDATA[
<greeting>
Hello, world!
</greeting>
]]>
]]
print(xml)
But when I run it, I get the following error:
lua: test.lua:7: unexpected symbol near ']'
It seems you are mistaking the square brackets on line 6 as the end of my string. How can I make it work?
I am trying to copy the words from a file .txt
into a linked list. I wrote something but what happens is that the program closes in a moment, as if there was a memory allocation error. I don't understand where I am going wrong.
#include <stdio.h>
#include <stdlib.h>
struct s_nodo
{
char* palabra;
struct s_nodo*sig;
};
typedef struct s_nodo* t_nodo;
void agregar (t_nodo*,char*);
void imprimir(t_nodo);
int main()
{
char aux[30];
t_nodo lista=NULL;
FILE*fd;
fd=fopen("c:\\texto.txt","r");
while(!feof(fd))
{
fscanf(fd,"%s",aux);
agregar(&lista,aux);
}
imprimir(lista);
return 0;
}
void agregar (t_nodo*lista,char *aux)
{
if(*lista==NULL)
{
*lista=malloc(sizeof(t_nodo));
(*lista)->palabra=malloc((strlen(aux+1))*sizeof(char));
strcpy((*lista)->palabra,aux);
(*lista)->sig=NULL;
}
else agregar(&(*lista)->sig,aux);
}
void imprimir (t_nodo lista)
{
if(lista!=NULL)
{
printf("-%s-",lista->palabra);
imprimir(lista->sig);
}
}
There are times when it is difficult to understand when it is preferable to use strtr
and when to use str_replace
. It seems that it is possible to get the same result with either of them, although the order in which the substrings are replaced is reversed. For example:
echo strtr('test string', 'st', 'XY')."\n";
echo strtr('test string', array( 's' => 'X', 't' => 'Y', 'st' => 'Z' ))."\n";
echo str_replace(array('s', 't', 'st'), array('X', 'Y', 'Z'), 'test string')."\n";
echo str_replace(array('st', 't', 's'), array('Z', 'Y', 'X'), 'test string');
Delivery as output:
YeXY XYring
YeZ Zring
YeXY XYring
YeZ Zring
Aside from the syntax, is there any benefit to using one or the other? Are there cases where one would not be enough to obtain the desired result?
Original question: When to use strtr vs str_replace?
In C#, what is the difference between String
and string
? (see capitals)
Example:
string s = "¡Hola mundo!";
String S = "¡Hola mundo!";
What are the rules of use for each? And what are the differences?
Original question: What's the difference between String and string?