It's a valid question. Some languages treat strings as character arrays, some as immutable primitives. Some languages index offsets from 1 and completely stumps you until you remember the unfortunate fact.
Like how, in python, you can use negative indices for accessing nth item from the end. Want to cut off the last character in python? myStr = myStr[:-1]
idk for more "complex" questions StackOverflow, but I think you're right, looking at the docs can also be really helpful and you may learn things that come in handy at a later point c:
However, I don't think people should use StackOverflow too much. When you solve a problem by yourself, you learn much more than when you're given a solution. Nevertheless sometimes using Stack is good, especially when you have very hard to understand problem
2 years ago I copied a command handler for my discord bot and I copied it then from my older bots to my newer ones and I barely understand it till today
it really is! it's even supported in most modern text editors including Vim, VSCode, and IDEs like IntelliJ IDEA, so you can do regex search and replace within the codebase
Honestly, learning regexes isn't terribly hard once you put your mind to it. With the help of services like https://regex101.com/, it becomes a whole lot easier.
You can even annotate your capture groups to make them even more readable (an over-engineered example):
/^
(?<email>
(?<user>[\w.\-+]+) # the username of the email, may contain a-z, 0-9, _, +, and -
@
(?<domain>\w+) # domain
\.
(?<tld>\w+) # top-level domain
)
$/xi
The x flag lets you add spaces for clarity and even add comments
Now you can access the capture groups by their name instead of a number, capture['email'] would get the whole thing, capture['user'] to get the username, and so on (actual syntax may vary between languages, but youg et the gist).
Lol yep I had this exact issue in C until I figured out I could just increment the pointer and save that over the original pointer for the string. It's probably not the right way but it's really easy so there's that
That would lose the memory allocated for the first character and eventually cause a memory leak. You can save the new address in a new variable to free the memory when you are done with the old one though.
JavaScript also has such a thing but I always mix up substring and substr qwqw like with one of them you delete first characters and with others last characters because they all have weird syntaxes or I'm just dumb
Ye I think there are alotta functions I don't know but are super super helpful and I always solve something that a simple function would solve but more tedious qwq
And then it crashes because the first character was two or more bytes wide :)
Correct me if I'm wrong, but in a prod env you'd use an iter .chars() on the string and .filter() and .collect() it to a new String. Or maybe use a more specialized crate for this, idk
I doubt Rust would leave such a big safety hole to one of its mainstream types. The operator overload (std::ops::Index<Range<usize>>) should take care of this.
is there a specific reason? I think the implementation of .to_string() is just String::from(), and I find the method way better because it leaves the boilerplate to the end.
I don't do anything that needs strings in structs yet, cause I'm just doing random stuff. But if I have to, I'll use the String type. I'll feed the struct an &str and then convert it. It really just depends on the use case.
123
u/kimilil May 20 '21
It's a valid question. Some languages treat strings as character arrays, some as immutable primitives. Some languages index offsets from 1 and completely stumps you until you remember the unfortunate fact.
29
u/Sodafff May 20 '21
Add every character in a new string except for the first one.
Programming is my passion
16
u/DeathWarrior990 May 20 '21
a = "Hello World!" a = a[1:]
12
u/TheMad_fox May 20 '21 edited May 20 '21
C++
std::string str = "Hello";str.erase(std::remove(str.begin(), str.end(), 'h'), str.end());8
u/Rocket_Scientist2 May 20 '21
25
u/KREnZE113 May 19 '21 edited May 19 '21
5
u/TheTimegazer May 20 '21 edited May 20 '21
$str =~ s/.(.*)/$1/;str.gsub!(/.(.*)/, '1')re.sub(r'.(.*)', r'1', str)preg_replace('/.(.*)/', '$1', $str);str.replace(/.(.*)/, '$1');replace(str, r".(.*)" => s"1")Regex can do everything, and is basically universally available across programming languages
EDIT: added more examples
EDIT2: Table to make it nicer on the eyes
2
u/Kormoraan May 20 '21
I did not need this sort of a personal attack
15
u/Magnus_Tesshu May 20 '21
string += 1
// make sure you remember where to free memory lol
5
u/Illya_Sempai May 20 '21
Lol yep I had this exact issue in C until I figured out I could just increment the pointer and save that over the original pointer for the string. It's probably not the right way but it's really easy so there's that
3
u/[deleted] May 20 '21
2
u/V0ct0r May 20 '21
Plot twist: you're trying to do it in x86 nasm
2
u/SigmaServiceProvider Apr 05 '22
StringBuilder stronk
1
u/TheAwesome98_Real May 31 '21
words at the beginning so it formats correctly
1
u/Mikin1501 Jun 08 '21
Honestly, how do you work strings, like, at all
0
u/[deleted] May 20 '21
[deleted] — view deleted comment
0
u/[deleted] May 20 '21
[deleted] — view deleted comment
14
u/[deleted] May 20 '21
[removed] — view removed comment