Does anyone no of any tutorials about string parsing. I couldn't find any good ones.
God bless.
:cool:
Short summary of practical options for modifying the string in your example (as described). is correct that Java String objects are immutable, so use a mutable builder for many edits. is also right that regular expressions can make pattern-based replacements simple.
A direct, safe approach for one-off insert/delete is StringBuilder. Find the * index, delete or replace that character, then insert the new text:
String s = "hello*world";
int idx = s.indexOf('*');
if (idx != -1) {
StringBuilder sb = new StringBuilder(s);
sb.deleteCharAt(idx); // remove the '*' if you don't want it kept
sb.insert(idx, "REPLACEMENT"); // insert whatever you need
s = sb.toString();
} If you need to replace based on a pattern (for example replace the first * or many matches), use regex. Remember to escape special characters and to use Matcher.quoteReplacement() when the replacement might contain $ or \:
String s = "hello*world";
String replacement = "REPLACEMENT";
s = s.replaceFirst("\\*", java.util.regex.Matcher.quoteReplacement(replacement)); Notes and tips
indexOf results for -1 before using the index. StringBuilder to avoid creating lots of temporary String objects. StringTokenizer exists but is considered a legacy API; prefer String.split, Scanner, or Pattern/Matcher for modern tokenizing needs. See the Java docs for String, StringBuilder, StringTokenizer, and the regex tutorial for details: String (immutable) StringBuilder docs StringTokenizer (legacy) Java regex tutorial.Jump to Post— server_crash 64I would just use StringTokenizer. You could easily find a good tutorial on that and it's better than using indexOf and simple math to try and parse everything.
Jump to Post— server_crash 64Tell me what you're situation is first. There are some things better than others, but it all depends on what you're doing.
If you're just talking general stuff here, then you must first know that Strings are immutable. That means there's really no way to modify the original string.
…
I would just use StringTokenizer. You could easily find a good tutorial on that and it's better than using indexOf and simple math to try and parse everything.
Thanx that's great dude.
http://www.csc.liv.ac.uk/~frans/COMP101/AdditionalStuff/tokenizing1.html#introduction
Tell me, do you know of any good tutorials for strings manipulation, such as insertion, deletion etc?
Tell me what you're situation is first. There are some things better than others, but it all depends on what you're doing.
If you're just talking general stuff here, then you must first know that Strings are immutable. That means there's really no way to modify the original string.
If you wanted to insert a character at the beginning:
String original = "string";
String modified = "a" + original; of course you can just modify the 'original' variable, but it's the same thing as the 'modified' variable because Strings are immutable. I would personally recommend using two variables instead of reassigning and making that one object lost in space.
>If you're just talking general stuff here, then you must first know that Strings are immutable. That means there's really no way to modify the original string.
Holy smokes! I just assumed the "java string" would be the same as the std::string (c++).
>Tell me what you're situation is first. There are some things better than others, but it all depends on what you're doing.
It's easter so we get a break from school. I was just doing a little project to keep myself entertained along with the rest of my assignments. :-).
Anyway what I need to do is take a string and manipulate it. Like I have...
string = " jfkdsliejsl*jfkeols"; And it goes through and finds a '*' then inserts "another_string";
Or erases part of the string at the position I tell it. ThanQ.
It sounds to me that Java Regular Expresions would do what you're looking to do.
Use the replaceAll(String regex, String replacement) or replaceFirst(String regex, String replacement) methods on String.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.