How do I find the character of a substring in the middle of a string?
i shared the steps to reproduce error and sample code an addition text file
iPhone XR
i shared the steps to reproduce error and sample code an addition text file
iPhone XR
VikingOSX wrote:
I remember, as a member of a development team, knowing that my next performance review would be based on how well I met my forecasted deliverables within a 15-minute window of when I said they would be done. No pressure there…
I too have quarter-hour timecard stories. I was overly candid with the accountants and auditors on related issues of business practices too, and was later proven correct. Learned much about business in that role, too. But that and other stories all for another time.
I believe that macOS has become so unnecessarily complicated that polish is too costly in terms of schedule or available resource planning.
That varies. For enterprise apps, UI work is often nearly unnecessary. Unfortunately. For other apps, UIs matter.
Most of the just-posted Swift code is deliberately duplicated, or is the unnecessary-to-the-goal B64 code needed to post this Swift code here.
The APIs are quite good, particularly compared with what I’m accustomed to else-platform. That B64 code is much more concise. UTF-8 handling is far better, too. It’s likely also possible to be more concise, too.
My usual gripe for these how-does-the-API-work cases is with the Apple doc. The Apple doc really needs work. There’s a whole layer of introductory- and jargon- and orientation-focused doc that’s just entirely missing, while the reference doc also has its issues.
Times change, too. The scale and scope and expectations of ~all apps has increased substantially over my experience here, but that’s been a consistent trend over the years. Some folks posting here and now programming in Swift have used punched cards and paper tape, the first time ‘round. Who knows, maybe AI eventually becomes less over-hyped, less of a glorified ⌘C⌘V, less of a copyright-risking boondoggle, and more useful?
There is very little non-trivial code I’ve ever written that I’ve later looked back at and not thought of improvements.
But back to another puddle of networking code, another parser, and another state machine…
You might want to ask in the developers forum.
Hi folks,
I got the solution, i used them to get the rage on var s.range(of: ss1, options: .diacriticInsensitive) it seems to work.
The following matches a substring in a string using Tamil:
VikingOSX wrote:
The following matches a substring in a string using Tamil:
But that's also using NSString. It looks like native Swift strings can only do "hasPrefix" and "hasSuffix".
Can somebody base64-encode the Tamil text strings and post it, so I have the right text for further testing?
base64 —-input=infile —-output=outfile
base64 —-decode —-input-infile —-output=outfile
the original "s" string: 4K6F4K6y4K6w4K+N4K6V4K6z4K+N
The first "ss" string: 4K6V4K6z4K+N
The second "ss" string: 4K6F4K6y4K6w4K+N
This thread popped to the top of the local Xcode queue, long after anyone is probably still interested in a reply here, but it was still an interesting tour through indexing a string in Swift, and particularly when character widths can vary.
Base64 shenanigans are due to forum character set posting limits. Y'all can omit all that.
import Foundation
extension String {
// https://stackoverflow.com/a/46969102
func base64Encoded() -> String? {
return data(using: .utf8)?.base64EncodedString()
}
func base64Decoded() -> String? {
guard let data = Data(base64Encoded: self) else { return nil }
return String(data: data, encoding: .utf8)
}
}
let randomString = "😀"
let tamilStringB64 = "4K6F4K614K6w4K+N4K6V4K6z4K+N" // B64 encoded Tamil strings
let tamilSubstringB64 = "4K6V4K6z4K+N" // (due to forum posting limits)
let tamilString = tamilStringB64.base64Decoded()!
let tamilSubstring = tamilSubstringB64.base64Decoded()!
print("Starting with: \(tamilString) and \(tamilSubstring)")
// test with a match
if tamilString.contains(tamilSubstring) {
print("\(tamilSubstring) was found in \(tamilString)")
} else {
print("\(tamilSubstring) was not found in \(tamilString)")
}
// test without a match
if tamilString.contains(randomString) {
print("\(randomString) was found in \(tamilString)")
} else {
print("\(randomString) was not found in \(tamilString)")
}
// use a string index to locate the substring
if let range: Range<String.Index> = tamilString.range(of: tamilSubstring) {
let index: Int = tamilString.distance(from: tamilString.startIndex, to: range.lowerBound)
print("Index of \(tamilSubstring) in \(tamilString) is: ", index)
}
else {
print("\(tamilSubstring) was not found in \(tamilString)")
}
// attempt to locate a non-matching string
if let range: Range<String.Index> = tamilString.range(of: randomString) {
let index: Int = tamilString.distance(from: tamilString.startIndex, to: range.lowerBound)
print("Index of \(randomString) in \(tamilString): ", index)
}
else {
print("\(randomString) was not found in \(tamilString)")
}
Results:
Version info:
$ swiftc -version
swift-driver version: 1.87.3 Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: x86_64-apple-macosx13.0
$
How do I find the character of a substring in the middle of a string?