how to read string line by line in j2me - java-me

how can i read one line from string ??? i have string like this :
hello ; boy ; sun
welcome;google;pink
how can i read first line only ?
thanks

String originalString="hello ; boy ; sun\nwelcome;google;pink";
String firstLine=originalString.substring(0,originalString.indexOf('\n'));

be aware that some applications actually use \r\n instead of \n to indicate new lines

See this previous question, but I like this solution better:
final InputStreamReader r = new InputStreamReader( new ByteArrayReader( string.getBytes() ) );

Related

Split string in multiples lines swift

I'm trying to split a string in multiple lines (such as the text of a song); I'm working in the localizable.strings.
I'd like to do something like this:
"TITLE_SONG01" = "It's my life";
"SUBTITLE_SONG01" = "";
"TEXT_SONG01" = "It's my life" +
"is now or never";
but it doesn't work because data isn't in the correct format. Can anyone help me?
You can do that by inserting "\n" where you want the new line to start. For example:
print("Hello\nWorld")
Which will output the Hello on one line and World on another like this:
Hello
World

Loading String value using ParseConfig from Parse.com with new line character

i have a problem to load a String value using ParseConfig from Parse.com with new line character.
The new line character is ignored, is that normal or i'm doing something wrong?
There is any other solution to get a text with new line character without using html code and uiwebview?
This question is old, but I'm posting here for posterity as I had the same issue.
On Parse, use another character such as <br> where you want a new line in your String.
Then, in Swift or Objective C, replace <br> with \n, which is recognized by Swift:
Swift:
// Assume you've loaded a String from Parse, called 'rawStringFromServer'
let newString = rawStringFromServer.replacingOccurrences(of: "<br>", with: "\n")
myUILabel.text = newString

Add new character in between a string builder string

I am building a new string using string builder. But now if want to add new characters in between already existing characters in the stringbuilder. How do i do it?
Example code:
StringBuilder sbr = new StringBuilder(" ");
sbr.append(1);
sbr.append(" ");
sbr.append(2);
sbr.append(" ");
sbr.append("3");
sbr.append(" ");
Now the string looks like 1 2 3
I want to add a new string after the number two. Can anyone please guide me how to do that?
Use the following to insert the character at position 3
sbr.insert(2, "<new charactor>");

How to read null delimited records in Groovy

I'd like to read multiline records that are terminated by null records, like so :
<MARQUE><AR_CP>N3</AR_CP>
<Classif>07</Classif>
<RegnT>03</RegnT>\0<MARQUE><AR_CP>O1</AR_CP>
<AR_PC>M5W 1C8</AR_PC>
<Classif>07</Classif>
<RegnT>03</RegnT>\0<MARQUE><AR_CP>Q1</AR_CP>
<Classif>07</Classif>
<RegnT>03</RegnT>
...
I would like to read the file, one record at a time. In perl I would use the "input record separator" $/ but I can't seem to find an equivalent in Groovy. Note that the file is too large to be slurped, and then simply split.
Is there an elegant way to do this in Groovy, or do I have to read byte per byte and "manually" break on '\0'?
Thank you!
You could try using Scanner like this:
new File( '/path/to/file.txt' ).withReader { r ->
new Scanner( r ).with { scanner ->
scanner.useDelimiter( ~/\00/ )
scanner.eachWithIndex { record, idx ->
println "Record $idx ${record.split('\n').join()}"
}
}
}

what's the best way to hardcode a multiple-line string?

In unit test I would like to hard code a block of lines as a string.
In C# I would do
var sb = new StringBuilder();
sb.AppendLine("myline1");
sb.AppendLine("myline2");
sb.AppendLine("myline3");
Since I converted to F# I tried to minimize the usage of .Net method by using bprintf instead, but somehow there is no bprintfn support which seems strange to me.
It is tedious to add \r\n at the end of each line manually.
Or is there any better way than StringBuilder?
Little known feature: you can indeed indent string content - by ending each line with a backslash. Leading spaces on the following line are stripped:
let poem = "The lesser world was daubed\n\
By a colorist of modest skill\n\
A master limned you in the finest inks\n\
And with a fresh-cut quill.\n"
You will still need to include \n or \n\r at line ends though (as done in the example above), if you want these embedded in your final string.
Edit to answer #MiloDCs question:
To use with sprintf:
let buildPoem character =
sprintf "The lesser world was daubed\n\
By a colorist of modest skill\n\
A master limned %s in the finest inks\n\
And with a fresh-cut quill.\n" character
buildPoem "you"
buildPoem "her"
buildPoem "him"
If you are under F# 3.0, triple-quoted strings may be the answer:
let x = """
myline1
myline2
myline3"""
I'm surprised nobody has mentioned this:
[ "My first line"
"second line"
"another line" ]
|> String.concat "\n"
You can create directly multi-line string literals in F#:
let multiLineStr =
"myline1
myline2
myline3"
and C#:
var multiLineStr =
#"myline1
myline2
myline3";
I think there is no problem with using StringBuilder in F# as you did.
There is a function fprintfn in Printf module, so you can use it with a StringWriter object:
let sw = new StringWriter()
fprintfn sw "myline1"
fprintfn sw "myline2"
fprintfn sw "myline3"
sw.ToString()
I like fprintf and fprintfn since they are flexible. You can write to console output by supplying stdout instead.
You could define your own bprintfn function:
let bprintfn bldr fmt =
(bldr, fmt) ||> Printf.kbprintf bldr.AppendLine
Or, to define a multi-line literal you could use triple-quotes as bytebuster suggested or a "verbatim literal," which begins with # (see Strings on MSDN).
I'm out of touch with F#, but you might be able to do adapt my normal approach:
['line1', 'line2', 'line3'].join('\n'); //javascript
StringUtils.join(Arrays.asList("line1", "line2", "line3"), "\n")); // java, using Apache Commons Lang

Resources