Best way to manipulate and compare strings
Posted
by vtortola
on Stack Overflow
See other posts from Stack Overflow
or by vtortola
Published on 2010-04-08T17:56:00Z
Indexed on
2010/04/08
18:03 UTC
Read the original article
Hit count: 485
I'm developing a REST service, so a request could be something like this:
/Data/Main/Table=Customers/
I need to get the segments one by one, and for each segment I will decide wich object I'm going to use, after I'll pass to that object the rest of the query so it can decide what to do next. Basically, the REST query is a path on a tree :P
This imply lots String operations (depending on the query complexity), but StringBuilder is useful just for concatenations and remove, you cannot perform a search with IndexOf or similar.
I've developed this class that fulfill my requirement, but the problem is that is manipulating Strings, so every time I get one segment ... I'll create extra Strings because String is an inmutable data type:
public class RESTQueryParser
{
String _query;
public RESTQueryParser(String query)
{
_query = query;
}
public String GetNext()
{
String result = String.Empty;
Int32 startPosition = _query.StartsWith("/", StringComparison.InvariantCultureIgnoreCase) ? 1 : 0;
Int32 i = _query.IndexOf("/", startPosition, StringComparison.InvariantCultureIgnoreCase) - 1;
if (!String.IsNullOrEmpty(_query))
{
if (i < 0)
{
result = _query.Substring(startPosition, _query.Length - 1);
_query = String.Empty;
}
else
{
result = _query.Substring(startPosition, i);
_query = _query.Remove(0, i + 1);
}
}
return result;
}
}
The server should support a lot of calls, and the queries could be huge, so this is gonna be a very repetitive task. I don't really know how big is the impact on the memory and the performance, I've just readed about it in some books.
Should I implement a class that manage a Char[] instead Strings and implement the methods that I want? Or should be ok with this one? Regular expressions maybe?
© Stack Overflow or respective owner